@@ -20,11 +20,11 @@ import (
20
20
"golang.org/x/crypto/pbkdf2"
21
21
)
22
22
23
- // encrypts `inputPath ` which can be either a file or directory and output into the `outputPath`
24
- // which must be a nonexisting file filepath .
23
+ // encrypts `inputPaths ` which can be either a slice of file or directory paths and outputs into the `outputPath`
24
+ // which must be a nonexisting file path .
25
25
//
26
26
// NOTE: `ctx` context is optional you can pass `nil` and the method will handle it
27
- func (sl * Safelock ) Encrypt (ctx context.Context , inputPath , outputPath , password string ) (err error ) {
27
+ func (sl * Safelock ) Encrypt (ctx context.Context , inputPaths [] string , outputPath , password string ) (err error ) {
28
28
errs := make (chan error )
29
29
signals := sl .getExitSignalsChannel ()
30
30
@@ -45,7 +45,7 @@ func (sl *Safelock) Encrypt(ctx context.Context, inputPath, outputPath, password
45
45
var outputFile * os.File
46
46
47
47
sl .updateStatus ("Validating input and output" , 0.0 )
48
- if err = validateEncryptionPaths (inputPath , outputPath ); err != nil {
48
+ if err = validateEncryptionPaths (inputPaths , outputPath ); err != nil {
49
49
errs <- fmt .Errorf ("invalid encryption input/output paths > %w" , err )
50
50
return
51
51
}
@@ -56,7 +56,7 @@ func (sl *Safelock) Encrypt(ctx context.Context, inputPath, outputPath, password
56
56
}
57
57
58
58
sl .updateStatus ("Creating compressed archive file" , 1.0 )
59
- if archiveFile , err = sl .createArchiveFile (ctx , inputPath ); err != nil {
59
+ if archiveFile , err = sl .createArchiveFile (ctx , inputPaths ); err != nil {
60
60
errs <- fmt .Errorf ("failed to create archive file > %w" , err )
61
61
return
62
62
}
@@ -76,7 +76,6 @@ func (sl *Safelock) Encrypt(ctx context.Context, inputPath, outputPath, password
76
76
77
77
unRegister ()
78
78
sl .updateStatus (fmt .Sprintf ("Encrypted %s" , outputPath ), 100.0 )
79
- sl .StatusObs .Trigger (EventStatusEnd )
80
79
close (signals )
81
80
close (errs )
82
81
}()
@@ -103,15 +102,17 @@ func (sl *Safelock) getExitSignalsChannel() chan os.Signal {
103
102
return signals
104
103
}
105
104
106
- func validateEncryptionPaths (inputPath , outputPath string ) (err error ) {
107
- inputIsFile , inputErrFile := utils .IsValidFile (inputPath )
108
- inputIsDir , inputErrDir := utils .IsValidDir (inputPath )
105
+ func validateEncryptionPaths (inputPath []string , outputPath string ) (err error ) {
106
+ for _ , inputPath := range inputPath {
107
+ inputIsFile , inputErrFile := utils .IsValidFile (inputPath )
108
+ inputIsDir , inputErrDir := utils .IsValidDir (inputPath )
109
109
110
- if ! inputIsFile && ! inputIsDir {
111
- if inputErrFile != nil {
112
- return inputErrFile
113
- } else {
114
- return inputErrDir
110
+ if ! inputIsFile && ! inputIsDir {
111
+ if inputErrFile != nil {
112
+ return inputErrFile
113
+ } else {
114
+ return inputErrDir
115
+ }
115
116
}
116
117
}
117
118
@@ -122,18 +123,23 @@ func validateEncryptionPaths(inputPath, outputPath string) (err error) {
122
123
return
123
124
}
124
125
125
- func (sl * Safelock ) createArchiveFile (ctx context.Context , inputPath string ) (file * utils.RegFile , err error ) {
126
+ func (sl * Safelock ) createArchiveFile (ctx context.Context , inputPaths [] string ) (file * utils.RegFile , err error ) {
126
127
var files []archiver.File
128
+ var filesMap = make (map [string ]string )
129
+
130
+ for _ , input := range inputPaths {
131
+ filesMap [input ] = ""
132
+ }
127
133
128
134
statusCtx , cancelStatus := context .WithCancel (ctx )
129
135
defer cancelStatus ()
130
136
131
- if files , err = archiver .FilesFromDisk (nil , map [ string ] string { inputPath : "" } ); err != nil {
137
+ if files , err = archiver .FilesFromDisk (nil , filesMap ); err != nil {
132
138
err = fmt .Errorf ("failed to list archive files > %w" , err )
133
139
return
134
140
}
135
141
136
- if file , err = sl .Registry .NewFile ("" , " e_output_temp" ); err != nil {
142
+ if file , err = sl .Registry .NewFile ("e_output_temp" ); err != nil {
137
143
err = fmt .Errorf ("failed to create temporary file > %w" , err )
138
144
return
139
145
}
@@ -143,7 +149,7 @@ func (sl *Safelock) createArchiveFile(ctx context.Context, inputPath string) (fi
143
149
Archival : sl .Archival ,
144
150
}
145
151
146
- go sl .updateArchiveFileStatus (statusCtx , inputPath , file .Name (), "Creating" , 1.0 )
152
+ go sl .updateArchiveFileStatus (statusCtx , inputPaths , file .Name (), "Creating" , 1.0 )
147
153
148
154
if err = format .Archive (ctx , file , files ); err != nil {
149
155
err = fmt .Errorf ("failed to create archive file > %w" , err )
@@ -155,13 +161,19 @@ func (sl *Safelock) createArchiveFile(ctx context.Context, inputPath string) (fi
155
161
return
156
162
}
157
163
158
- func (sl * Safelock ) updateArchiveFileStatus (ctx context.Context , inputPath , archivePath , act string , start float64 ) {
164
+ func (sl * Safelock ) updateArchiveFileStatus (
165
+ ctx context.Context ,
166
+ inputPaths []string ,
167
+ archivePath ,
168
+ act string ,
169
+ start float64 ,
170
+ ) {
159
171
for {
160
172
select {
161
173
case <- ctx .Done ():
162
174
return
163
175
default :
164
- if p , err := utils .GetPathsPercent (inputPath , archivePath , start , 30.0 ); err != nil {
176
+ if p , err := utils .GetPathsPercent (inputPaths , archivePath , start , 30.0 ); err != nil {
165
177
return
166
178
} else {
167
179
sl .updateStatus (fmt .Sprintf ("%s compressed archive file" , act ), p )
0 commit comments