@@ -2,67 +2,128 @@ package internal
2
2
3
3
import (
4
4
"archive/zip"
5
+ "fmt"
5
6
"io"
7
+ "log/slog"
6
8
"os"
7
9
"path/filepath"
8
10
"strings"
11
+
12
+ "github.com/charmbracelet/bubbles/progress"
13
+ "github.com/lithammer/shortuuid"
14
+ "github.com/yorukot/superfile/src/config/icon"
15
+ "github.com/yorukot/superfile/src/internal/common"
9
16
)
10
17
11
18
func zipSources (sources []string , target string ) error {
19
+ id := shortuuid .New ()
20
+ prog := progress .New ()
21
+ prog .PercentageStyle = common .FooterStyle
22
+ var err error = nil
23
+
24
+ for _ , src := range sources {
25
+ if _ , err := os .Stat (src ); os .IsNotExist (err ) {
26
+ return fmt .Errorf ("source path does not exist: %s" , src )
27
+ }
28
+ }
29
+
30
+ totalFiles := 0
31
+ for _ , src := range sources {
32
+ count , err := countFiles (src )
33
+ if err != nil {
34
+ slog .Error ("Error while zip file count files " , "error" , err )
35
+ }
36
+ totalFiles += count
37
+ }
38
+ p := process {
39
+ name : "zip files" ,
40
+ progress : prog ,
41
+ state : inOperation ,
42
+ total : totalFiles ,
43
+ done : 0 ,
44
+ }
45
+ message := channelMessage {
46
+ messageID : id ,
47
+ messageType : sendProcess ,
48
+ processNewState : p ,
49
+ }
50
+
51
+ _ , err = os .Stat (target )
52
+ if err == nil {
53
+ p .name = icon .CompressFile + icon .Space + "File already exist"
54
+ message .processNewState = p
55
+ channel <- message
56
+ return nil
57
+ }
58
+
12
59
f , err := os .Create (target )
13
60
if err != nil {
14
61
return err
15
62
}
16
-
17
63
defer f .Close ()
18
64
writer := zip .NewWriter (f )
19
65
defer writer .Close ()
20
66
21
67
for _ , src := range sources {
22
68
srcParentDir := filepath .Dir (src )
23
69
err = filepath .Walk (src , func (path string , info os.FileInfo , err error ) error {
70
+ p .name = icon .CompressFile + icon .Space + filepath .Base (path )
71
+ if len (channel ) < 5 {
72
+ message .processNewState = p
73
+ channel <- message
74
+ }
24
75
if err != nil {
25
76
return err
26
77
}
27
-
28
78
relPath , err := filepath .Rel (srcParentDir , path )
29
79
if err != nil {
30
80
return err
31
81
}
32
-
33
82
header , err := zip .FileInfoHeader (info )
34
83
if err != nil {
35
84
return err
36
85
}
37
-
38
86
header .Method = zip .Deflate
39
87
header .Name = relPath
40
88
if info .IsDir () {
41
89
header .Name += "/"
42
90
}
43
-
44
- hw , err := writer .CreateHeader (header )
91
+ headerWriter , err := writer .CreateHeader (header )
45
92
if err != nil {
46
93
return err
47
94
}
48
-
49
95
if info .IsDir () {
50
96
return nil
51
97
}
52
-
53
98
file , err := os .Open (path )
54
99
if err != nil {
55
100
return err
56
101
}
57
-
58
102
defer file .Close ()
59
- _ , err = io .Copy (hw , file )
60
- return err
103
+ _ , err = io .Copy (headerWriter , file )
104
+ if err != nil {
105
+ return err
106
+ }
107
+ p .done ++
108
+ if len (channel ) < 5 {
109
+ message .processNewState = p
110
+ channel <- message
111
+ }
112
+ return nil
61
113
})
62
114
if err != nil {
115
+ slog .Error ("Error while zip file" , "error" , err )
116
+ p .state = failure
117
+ message .processNewState = p
118
+ channel <- message
63
119
return err
64
120
}
65
121
}
122
+
123
+ p .state = successful
124
+ p .done = totalFiles
125
+ message .processNewState = p
126
+ channel <- message
66
127
return nil
67
128
}
68
129
0 commit comments