@@ -177,13 +177,55 @@ pub fn get_extractions(client: &Client, api_token: &String) -> Result<Extraction
177
177
Ok ( response)
178
178
}
179
179
180
- // Downloads a file or folder
180
+ struct ReplaceChar < ' a > {
181
+ from : & ' a str ,
182
+ to : & ' a str ,
183
+ }
184
+
185
+ /// Replaces illegal characters in a file name
186
+ fn replace_illegal_chars ( name : & str ) -> String {
187
+ let mut name: String = name. to_owned ( ) ;
188
+
189
+ const ILLEGAL_CHARS : [ ReplaceChar < ' _ > ; 7 ] = [
190
+ ReplaceChar { from : "<" , to : "" } ,
191
+ ReplaceChar { from : ">" , to : "" } ,
192
+ ReplaceChar {
193
+ from : ":" ,
194
+ to : " - " ,
195
+ } ,
196
+ ReplaceChar {
197
+ from : "\" " ,
198
+ to : "\' " ,
199
+ } ,
200
+ ReplaceChar { from : "|" , to : "" } ,
201
+ ReplaceChar { from : "?" , to : "" } ,
202
+ ReplaceChar { from : "*" , to : "" } ,
203
+ ] ;
204
+
205
+ for replacement in ILLEGAL_CHARS {
206
+ name = name. replace ( replacement. from , replacement. to ) ;
207
+ }
208
+
209
+ name
210
+ }
211
+
212
+ /// Downloads a file or folder
213
+ ///
214
+ /// # Arguments
215
+ ///
216
+ /// * `client` - The reqwest client
217
+ /// * `api_token` - The user's API token
218
+ /// * `file_id` - The ID of the file or folder to download
219
+ /// * `recursive` - Recursively download the folder
220
+ /// * `path` - The path to save the file or folder to
221
+ /// * `no_replace` - Do not replace illegal characters in the file name
181
222
pub fn download (
182
223
client : & Client ,
183
224
api_token : & String ,
184
225
file_id : i64 ,
185
226
recursive : bool ,
186
227
path : Option < & String > ,
228
+ no_replace : bool ,
187
229
) -> Result < ( ) , Error > {
188
230
let files: FilesResponse =
189
231
put:: files:: list ( client, api_token, file_id) . expect ( "querying files" ) ;
@@ -194,16 +236,27 @@ pub fn download(
194
236
match recursive {
195
237
true => {
196
238
// Recursively download the folder
197
- let directory_path: String = match path {
239
+ let mut directory_path: String = match path {
198
240
Some ( p) => format ! ( "{}/{}" , p, files. parent. name) , // Use the provided path if there is one
199
241
None => format ! ( "./{}" , files. parent. name) ,
200
242
} ;
201
243
244
+ if !no_replace {
245
+ directory_path = replace_illegal_chars ( & directory_path) ;
246
+ }
247
+
202
248
fs:: create_dir_all ( directory_path. clone ( ) ) . expect ( "creating directory" ) ;
203
249
204
250
for file in files. files {
205
- download ( client, api_token, file. id , true , Some ( & directory_path) )
206
- . expect ( "downloading file recursively" ) ;
251
+ download (
252
+ client,
253
+ api_token,
254
+ file. id ,
255
+ true ,
256
+ Some ( & directory_path) ,
257
+ no_replace,
258
+ )
259
+ . expect ( "downloading file recursively" ) ;
207
260
}
208
261
}
209
262
false => {
@@ -215,11 +268,15 @@ pub fn download(
215
268
216
269
println ! ( "ZIP created!" ) ;
217
270
218
- let output_path: String = match path {
271
+ let mut output_path: String = match path {
219
272
Some ( p) => format ! ( "{}/{}.zip" , p, files. parent. name) ,
220
273
None => format ! ( "./{}.zip" , files. parent. name) ,
221
274
} ;
222
275
276
+ if !no_replace {
277
+ output_path = replace_illegal_chars ( & output_path) ;
278
+ }
279
+
223
280
println ! ( "Downloading: {}" , files. parent. name) ;
224
281
println ! ( "Saving to: {}\n " , output_path) ;
225
282
@@ -245,11 +302,15 @@ pub fn download(
245
302
let url_response: UrlResponse =
246
303
put:: files:: url ( client, api_token, file_id) . expect ( "creating download URL" ) ;
247
304
248
- let output_path: String = match path {
305
+ let mut output_path: String = match path {
249
306
Some ( p) => format ! ( "{}/{}" , p, files. parent. name) ,
250
307
None => format ! ( "./{}" , files. parent. name) ,
251
308
} ;
252
309
310
+ if !no_replace {
311
+ output_path = replace_illegal_chars ( & output_path) ;
312
+ }
313
+
253
314
println ! ( "Downloading: {}" , files. parent. name) ;
254
315
println ! ( "Saving to: {}\n " , output_path) ;
255
316
0 commit comments