1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
//! File operations
//!
//! This module provides core file operations including:
//! - Download files
//! - Upload files (using the two-stage upload process)
//! - Update file metadata
//! - Delete files
//!
//! Note: File uploads in Files.com use a two-stage process:
//! 1. Call `FileActionHandler::begin_upload()` to get upload URLs
//! 2. Use this handler's `upload_file()` to complete the upload
use crate::files::FileActionHandler;
use crate::progress::{Progress, ProgressCallback};
use crate::types::FileEntity;
use crate::utils::encode_path;
use crate::{FilesClient, FilesError, Result};
use serde_json::json;
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use walkdir::WalkDir;
/// Default chunk size for streaming operations (64KB)
///
/// This provides a good balance between:
/// - Memory usage per read operation
/// - Number of syscalls
/// - Progress update granularity
const STREAM_CHUNK_SIZE: usize = 65536; // 64KB
/// Handler for file operations
///
/// Provides methods for downloading, uploading, updating, and deleting files.
#[derive(Debug, Clone)]
pub struct FileHandler {
client: FilesClient,
}
impl FileHandler {
/// Creates a new FileHandler
///
/// # Arguments
///
/// * `client` - FilesClient instance
pub fn new(client: FilesClient) -> Self {
Self { client }
}
/// Download a file or get file information
///
/// # Arguments
///
/// * `path` - File path to download
///
/// # Returns
///
/// Returns a `FileEntity` containing file information including a
/// `download_uri` for the actual file download.
///
/// # Examples
///
/// ```rust,no_run
/// use files_sdk::{FilesClient, FileHandler};
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FilesClient::builder()
/// .api_key("your-api-key")
/// .build()?;
///
/// let handler = FileHandler::new(client);
/// let file = handler.download_file("/path/to/file.txt").await?;
///
/// if let Some(uri) = file.download_uri {
/// println!("Download from: {}", uri);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn download_file(&self, path: &str) -> Result<FileEntity> {
let encoded_path = encode_path(path);
let endpoint = format!("/files{}", encoded_path);
let response = self.client.get_raw(&endpoint).await?;
Ok(serde_json::from_value(response)?)
}
/// Download the actual file content as bytes
///
/// Unlike `download_file()` which returns metadata with a download URL,
/// this method fetches and returns the actual file content.
///
/// # Arguments
///
/// * `path` - File path to download
///
/// # Returns
///
/// Returns the file content as a `Vec<u8>`
///
/// # Examples
///
/// ```rust,no_run
/// use files_sdk::{FilesClient, FileHandler};
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FilesClient::builder()
/// .api_key("your-api-key")
/// .build()?;
///
/// let handler = FileHandler::new(client);
/// let content = handler.download_content("/path/to/file.txt").await?;
/// println!("Downloaded {} bytes", content.len());
/// # Ok(())
/// # }
/// ```
pub async fn download_content(&self, path: &str) -> Result<Vec<u8>> {
// First, get the file metadata to obtain the download URI
let file = self.download_file(path).await?;
// Extract the download URI
let download_uri = file.download_uri.ok_or_else(|| {
FilesError::not_found_resource("No download URI available", "file", path)
})?;
// Fetch the actual file content from the download URI
let response = reqwest::get(&download_uri)
.await
.map_err(FilesError::Request)?;
let bytes = response.bytes().await.map_err(FilesError::Request)?;
Ok(bytes.to_vec())
}
/// Download file content and save to a local file
///
/// This is a convenience method that downloads the file content and
/// writes it to the specified local path.
///
/// # Arguments
///
/// * `remote_path` - Path to the file on Files.com
/// * `local_path` - Local filesystem path where the file should be saved
///
/// # Examples
///
/// ```rust,no_run
/// use files_sdk::{FilesClient, FileHandler};
/// use std::path::Path;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FilesClient::builder()
/// .api_key("your-api-key")
/// .build()?;
///
/// let handler = FileHandler::new(client);
/// handler.download_to_file(
/// "/path/to/remote/file.txt",
/// Path::new("./local/file.txt")
/// ).await?;
/// # Ok(())
/// # }
/// ```
pub async fn download_to_file(
&self,
remote_path: &str,
local_path: &std::path::Path,
) -> Result<()> {
let content = self.download_content(remote_path).await?;
std::fs::write(local_path, content)
.map_err(|e| FilesError::IoError(format!("Failed to write file: {}", e)))?;
Ok(())
}
/// Download file content to an async stream
///
/// This method is more memory-efficient than [`download_content()`](Self::download_content) for large files
/// as it streams the data directly to the writer in chunks instead of loading it into memory.
///
/// # Arguments
///
/// * `remote_path` - Path to the file on Files.com
/// * `writer` - An async writer implementing [`tokio::io::AsyncWrite`]
/// * `progress_callback` - Optional callback for progress updates (see [`progress`](crate::progress) module)
///
/// # Examples
///
/// ## Basic streaming download
///
/// ```rust,no_run
/// # use files_sdk::{FilesClient, files::FileHandler};
/// # use tokio::fs::File;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = FilesClient::builder().api_key("key").build()?;
/// let handler = FileHandler::new(client);
///
/// let mut file = File::create("downloaded-large-file.tar.gz").await?;
/// handler.download_stream(
/// "/remote/large-file.tar.gz",
/// &mut file,
/// None
/// ).await?;
/// # Ok(())
/// # }
/// ```
///
/// ## With progress tracking
///
/// ```rust,no_run
/// # use files_sdk::{FilesClient, files::FileHandler};
/// # use files_sdk::progress::{Progress, ProgressCallback, PrintProgressCallback};
/// # use tokio::fs::File;
/// # use std::sync::Arc;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = FilesClient::builder().api_key("key").build()?;
/// let handler = FileHandler::new(client);
/// let callback = Arc::new(PrintProgressCallback);
///
/// let mut file = File::create("large-file.tar.gz").await?;
/// handler.download_stream(
/// "/remote/large-file.tar.gz",
/// &mut file,
/// Some(callback)
/// ).await?;
/// # Ok(())
/// # }
/// ```
///
/// ## Streaming to any AsyncWrite destination
///
/// ```rust,no_run
/// # use files_sdk::{FilesClient, files::FileHandler};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = FilesClient::builder().api_key("key").build()?;
/// let handler = FileHandler::new(client);
///
/// // Download to memory
/// let mut buffer = Vec::new();
/// handler.download_stream(
/// "/remote/file.txt",
/// &mut buffer,
/// None
/// ).await?;
///
/// println!("Downloaded {} bytes", buffer.len());
/// # Ok(())
/// # }
/// ```
pub async fn download_stream<W>(
&self,
remote_path: &str,
writer: &mut W,
progress_callback: Option<Arc<dyn ProgressCallback>>,
) -> Result<()>
where
W: tokio::io::AsyncWrite + Unpin,
{
use tokio::io::AsyncWriteExt;
// First, get the file metadata to obtain the download URI and size
let file = self.download_file(remote_path).await?;
// Extract the download URI
let download_uri = file.download_uri.ok_or_else(|| {
FilesError::not_found_resource("No download URI available", "file", remote_path)
})?;
// Get the total file size for progress tracking
let total_bytes = file
.size
.and_then(|s| if s > 0 { Some(s as u64) } else { None });
// Stream the file content from the download URI
let mut response = reqwest::get(&download_uri)
.await
.map_err(FilesError::Request)?;
let mut bytes_transferred = 0u64;
// Stream chunks to the writer with progress tracking
while let Some(chunk) = response.chunk().await.map_err(FilesError::Request)? {
writer
.write_all(&chunk)
.await
.map_err(|e| FilesError::IoError(format!("Failed to write to stream: {}", e)))?;
bytes_transferred += chunk.len() as u64;
// Report progress
if let Some(ref callback) = progress_callback {
let progress = Progress::new(bytes_transferred, total_bytes);
callback.on_progress(&progress);
}
}
// Flush the writer to ensure all data is written
writer
.flush()
.await
.map_err(|e| FilesError::IoError(format!("Failed to flush stream: {}", e)))?;
Ok(())
}
/// Get file metadata only (no download URL, no logging)
///
/// This is a convenience method that calls `FileActionHandler::get_metadata()`
///
/// # Arguments
///
/// * `path` - File path
pub async fn get_metadata(&self, path: &str) -> Result<FileEntity> {
let file_action = FileActionHandler::new(self.client.clone());
file_action.get_metadata(path).await
}
/// Upload a file (complete two-stage upload process)
///
/// This method handles the complete upload process:
/// 1. Calls begin_upload to get upload URLs
/// 2. Uploads the file data
/// 3. Finalizes the upload
///
/// # Arguments
///
/// * `path` - Destination path for the file
/// * `data` - File contents as bytes
///
/// # Examples
///
/// ```rust,no_run
/// # use files_sdk::{FilesClient, FileHandler};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = FilesClient::builder().api_key("key").build()?;
/// let handler = FileHandler::new(client);
///
/// let data = b"Hello, Files.com!";
/// let file = handler.upload_file("/uploads/test.txt", data).await?;
/// println!("Uploaded: {:?}", file.path);
/// # Ok(())
/// # }
/// ```
pub async fn upload_file(&self, path: &str, data: &[u8]) -> Result<FileEntity> {
// Stage 1: Begin upload
let file_action = FileActionHandler::new(self.client.clone());
let upload_parts = file_action
.begin_upload(path, Some(data.len() as i64), true)
.await?;
if upload_parts.is_empty() {
return Err(crate::FilesError::ApiError {
endpoint: None,
code: 500,
message: "No upload parts returned from begin_upload".to_string(),
});
}
let upload_part = &upload_parts[0];
// Stage 2: Upload file data to the provided URL
// This is an external URL (not Files.com API), typically to cloud storage
let _etag = if let Some(upload_uri) = &upload_part.upload_uri {
let http_client = reqwest::Client::new();
let http_method = upload_part
.http_method
.as_deref()
.unwrap_or("PUT")
.to_uppercase();
let mut request = match http_method.as_str() {
"PUT" => http_client.put(upload_uri),
"POST" => http_client.post(upload_uri),
_ => http_client.put(upload_uri),
};
// Add any custom headers
if let Some(headers) = &upload_part.headers {
for (key, value) in headers {
request = request.header(key, value);
}
}
// Set Content-Length header (required by S3, even for empty files)
let upload_response = request
.header("Content-Length", data.len().to_string())
.body(data.to_vec())
.send()
.await?;
// Extract ETag from response headers
upload_response
.headers()
.get("etag")
.and_then(|v| v.to_str().ok())
.map(|s| s.trim_matches('"').to_string())
} else {
None
};
// Stage 3: Finalize upload with Files.com
let encoded_path = encode_path(path);
let endpoint = format!("/files{}", encoded_path);
// Build the finalization request as form data
let mut form = vec![("action", "end".to_string())];
// Add ref (upload reference) - this is required to identify the upload
if let Some(ref_value) = &upload_part.ref_ {
form.push(("ref", ref_value.clone()));
}
// Note: etags might not be needed when ref is provided
// Commenting out for now to test
// if let Some(etag_value) = etag {
// let part_number = upload_part.part_number.unwrap_or(1);
// form.push(("etags[etag]", etag_value));
// form.push(("etags[part]", part_number.to_string()));
// }
let response = self.client.post_form(&endpoint, &form).await?;
Ok(serde_json::from_value(response)?)
}
/// Upload a file from an async stream
///
/// This method is more memory-efficient than [`upload_file()`](Self::upload_file) for large files
/// as it reads the data in chunks (8KB) instead of loading it entirely into memory.
///
/// # Arguments
///
/// * `path` - Destination path for the file on Files.com
/// * `reader` - An async reader implementing [`tokio::io::AsyncRead`]
/// * `size` - Optional size of the file in bytes (recommended for progress tracking)
/// * `progress_callback` - Optional callback for progress updates (see [`progress`](crate::progress) module)
///
/// # Returns
///
/// Returns a [`FileEntity`] with the uploaded file's metadata.
///
/// # Examples
///
/// ## Basic streaming upload
///
/// ```rust,no_run
/// # use files_sdk::{FilesClient, files::FileHandler};
/// # use tokio::fs::File;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = FilesClient::builder().api_key("key").build()?;
/// let handler = FileHandler::new(client);
///
/// let file = File::open("large-file.tar.gz").await?;
/// let metadata = file.metadata().await?;
/// let size = metadata.len() as i64;
///
/// handler.upload_stream(
/// "/uploads/large-file.tar.gz",
/// file,
/// Some(size),
/// None
/// ).await?;
/// # Ok(())
/// # }
/// ```
///
/// ## With progress tracking
///
/// ```rust,no_run
/// # use files_sdk::{FilesClient, files::FileHandler};
/// # use files_sdk::progress::{Progress, ProgressCallback, PrintProgressCallback};
/// # use tokio::fs::File;
/// # use std::sync::Arc;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = FilesClient::builder().api_key("key").build()?;
/// let handler = FileHandler::new(client);
/// let callback = Arc::new(PrintProgressCallback);
///
/// let file = File::open("large-file.tar.gz").await?;
/// let size = file.metadata().await?.len() as i64;
///
/// handler.upload_stream(
/// "/uploads/large-file.tar.gz",
/// file,
/// Some(size),
/// Some(callback)
/// ).await?;
/// # Ok(())
/// # }
/// ```
///
/// ## Streaming from any AsyncRead source
///
/// ```rust,no_run
/// # use files_sdk::{FilesClient, files::FileHandler};
/// # use std::io::Cursor;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = FilesClient::builder().api_key("key").build()?;
/// let handler = FileHandler::new(client);
///
/// // Upload from memory
/// let data = b"file contents";
/// let cursor = Cursor::new(data.to_vec());
///
/// handler.upload_stream(
/// "/uploads/file.txt",
/// cursor,
/// Some(data.len() as i64),
/// None
/// ).await?;
/// # Ok(())
/// # }
/// ```
pub async fn upload_stream<R>(
&self,
path: &str,
mut reader: R,
size: Option<i64>,
progress_callback: Option<Arc<dyn ProgressCallback>>,
) -> Result<FileEntity>
where
R: tokio::io::AsyncRead + Unpin,
{
use tokio::io::AsyncReadExt;
// Stage 1: Begin upload
let file_action = FileActionHandler::new(self.client.clone());
let upload_parts = file_action.begin_upload(path, size, true).await?;
if upload_parts.is_empty() {
return Err(crate::FilesError::ApiError {
endpoint: None,
code: 500,
message: "No upload parts returned from begin_upload".to_string(),
});
}
let upload_part = &upload_parts[0];
// Stage 2: Stream file data to the provided URL with progress tracking
// Note: Even for empty files (size=0), we must perform the upload stage.
// S3 requires the Content-Length header, and the API tracks whether the upload occurred.
let _etag = if let Some(upload_uri) = upload_part.upload_uri.as_ref() {
// Read the stream into a buffer with progress tracking
// Note: We read in chunks to provide progress updates, but still buffer
// the entire file before upload. This is required by the Files.com API
// which expects the full file in a single PUT/POST request.
// Pre-allocate buffer if size is known for better performance
let mut buffer = if let Some(s) = size {
Vec::with_capacity(s as usize)
} else {
Vec::new()
};
let mut temp_buffer = vec![0u8; STREAM_CHUNK_SIZE];
let total_bytes = size.map(|s| s as u64);
loop {
let bytes_read = reader.read(&mut temp_buffer).await.map_err(|e| {
FilesError::IoError(format!("Failed to read from stream: {}", e))
})?;
if bytes_read == 0 {
break;
}
buffer.extend_from_slice(&temp_buffer[..bytes_read]);
// Report progress
if let Some(ref callback) = progress_callback {
let progress = Progress::new(buffer.len() as u64, total_bytes);
callback.on_progress(&progress);
}
}
let http_client = reqwest::Client::new();
let http_method = upload_part
.http_method
.as_deref()
.unwrap_or("PUT")
.to_uppercase();
let mut request = match http_method.as_str() {
"PUT" => http_client.put(upload_uri),
"POST" => http_client.post(upload_uri),
_ => http_client.put(upload_uri),
};
// Add any custom headers
if let Some(headers) = &upload_part.headers {
for (key, value) in headers {
request = request.header(key, value);
}
}
// Set Content-Length header (required by S3, even for empty files)
let content_length = buffer.len();
let upload_response = request
.header("Content-Length", content_length.to_string())
.body(buffer)
.send()
.await?;
// Extract ETag from response headers
upload_response
.headers()
.get("etag")
.and_then(|v| v.to_str().ok())
.map(|s| s.trim_matches('"').to_string())
} else {
None
};
// Stage 3: Finalize upload with Files.com
let encoded_path = encode_path(path);
let endpoint = format!("/files{}", encoded_path);
let mut form = vec![("action", "end".to_string())];
if let Some(ref_value) = &upload_part.ref_ {
form.push(("ref", ref_value.clone()));
}
let response = self.client.post_form(&endpoint, &form).await?;
Ok(serde_json::from_value(response)?)
}
/// Update file metadata
///
/// # Arguments
///
/// * `path` - File path
/// * `custom_metadata` - Custom metadata key-value pairs (optional)
/// * `provided_mtime` - Custom modification time (optional)
/// * `priority_color` - Priority color (optional)
///
/// # Examples
///
/// ```rust,no_run
/// # use files_sdk::{FilesClient, FileHandler};
/// # use std::collections::HashMap;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = FilesClient::builder().api_key("key").build()?;
/// let handler = FileHandler::new(client);
///
/// let mut metadata = HashMap::new();
/// metadata.insert("category".to_string(), "reports".to_string());
///
/// handler.update_file("/path/to/file.txt", Some(metadata), None, None).await?;
/// # Ok(())
/// # }
/// ```
pub async fn update_file(
&self,
path: &str,
custom_metadata: Option<HashMap<String, String>>,
provided_mtime: Option<String>,
priority_color: Option<String>,
) -> Result<FileEntity> {
let mut body = json!({});
if let Some(metadata) = custom_metadata {
body["custom_metadata"] = json!(metadata);
}
if let Some(mtime) = provided_mtime {
body["provided_mtime"] = json!(mtime);
}
if let Some(color) = priority_color {
body["priority_color"] = json!(color);
}
let encoded_path = encode_path(path);
let endpoint = format!("/files{}", encoded_path);
let response = self.client.patch_raw(&endpoint, body).await?;
Ok(serde_json::from_value(response)?)
}
/// Delete a file
///
/// # Arguments
///
/// * `path` - File path to delete
/// * `recursive` - If path is a folder, delete recursively
///
/// # Examples
///
/// ```rust,no_run
/// # use files_sdk::{FilesClient, FileHandler};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = FilesClient::builder().api_key("key").build()?;
/// let handler = FileHandler::new(client);
/// handler.delete_file("/path/to/file.txt", false).await?;
/// # Ok(())
/// # }
/// ```
pub async fn delete_file(&self, path: &str, recursive: bool) -> Result<()> {
let encoded_path = encode_path(path);
let endpoint = if recursive {
format!("/files{}?recursive=true", encoded_path)
} else {
format!("/files{}", encoded_path)
};
self.client.delete_raw(&endpoint).await?;
Ok(())
}
/// Copy a file
///
/// This is a convenience method that calls `FileActionHandler::copy_file()`
///
/// # Arguments
///
/// * `source` - Source file path
/// * `destination` - Destination path
pub async fn copy_file(&self, source: &str, destination: &str) -> Result<()> {
let file_action = FileActionHandler::new(self.client.clone());
file_action.copy_file(source, destination).await
}
/// Move a file
///
/// This is a convenience method that calls `FileActionHandler::move_file()`
///
/// # Arguments
///
/// * `source` - Source file path
/// * `destination` - Destination path
pub async fn move_file(&self, source: &str, destination: &str) -> Result<()> {
let file_action = FileActionHandler::new(self.client.clone());
file_action.move_file(source, destination).await
}
/// Upload an entire directory recursively
///
/// Walks through a local directory and uploads all files to Files.com,
/// preserving the directory structure.
///
/// # Arguments
///
/// * `local_dir` - Local directory path to upload
/// * `remote_path` - Remote destination path on Files.com
/// * `mkdir_parents` - Create parent directories if they don't exist
///
/// # Returns
///
/// Vector of successfully uploaded remote file paths
///
/// # Errors
///
/// Returns an error if:
/// - Local directory doesn't exist or isn't readable
/// - Path contains invalid UTF-8
/// - Any file upload fails
///
/// # Examples
///
/// ```rust,no_run
/// use files_sdk::{FilesClient, FileHandler};
/// use std::path::Path;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FilesClient::builder().api_key("key").build()?;
/// let handler = FileHandler::new(client);
///
/// let uploaded = handler.upload_directory(
/// Path::new("./local/images"),
/// "/remote/uploads",
/// true // create parent directories
/// ).await?;
///
/// println!("Uploaded {} files", uploaded.len());
/// # Ok(())
/// # }
/// ```
pub async fn upload_directory(
&self,
local_dir: &Path,
remote_path: &str,
mkdir_parents: bool,
) -> Result<Vec<String>> {
let mut uploaded = Vec::new();
for entry in WalkDir::new(local_dir).into_iter().filter_map(|e| e.ok()) {
if entry.file_type().is_file() {
let local_file = entry.path();
// Calculate relative path
let relative = local_file
.strip_prefix(local_dir)
.map_err(|e| FilesError::IoError(format!("Failed to strip prefix: {}", e)))?
.to_str()
.ok_or_else(|| {
FilesError::IoError(format!(
"Invalid UTF-8 in path: {}",
local_file.display()
))
})?;
// Construct remote path (use forward slashes for Files.com)
let remote_file = format!(
"{}/{}",
remote_path.trim_end_matches('/'),
relative.replace('\\', "/")
);
// Read and upload
let data =
std::fs::read(local_file).map_err(|e| FilesError::IoError(e.to_string()))?;
// Upload using the same two-stage process as upload_file
let file_action = FileActionHandler::new(self.client.clone());
let upload_parts = file_action
.begin_upload(&remote_file, Some(data.len() as i64), mkdir_parents)
.await?;
if !upload_parts.is_empty() {
let upload_part = &upload_parts[0];
if let Some(upload_uri) = &upload_part.upload_uri {
let http_client = reqwest::Client::new();
let http_method = upload_part
.http_method
.as_deref()
.unwrap_or("PUT")
.to_uppercase();
let mut request = match http_method.as_str() {
"PUT" => http_client.put(upload_uri),
"POST" => http_client.post(upload_uri),
_ => http_client.put(upload_uri),
};
if let Some(headers) = &upload_part.headers {
for (key, value) in headers {
request = request.header(key, value);
}
}
request.body(data.to_vec()).send().await?;
}
}
uploaded.push(remote_file);
}
}
Ok(uploaded)
}
/// Upload directory with progress callback
///
/// Same as `upload_directory` but calls a progress callback after each file upload.
/// Useful for showing upload progress in UIs or logging.
///
/// # Arguments
///
/// * `local_dir` - Local directory path to upload
/// * `remote_path` - Remote destination path on Files.com
/// * `mkdir_parents` - Create parent directories if they don't exist
/// * `progress` - Callback function called with (current_file_number, total_files)
///
/// # Examples
///
/// ```rust,no_run
/// use files_sdk::{FilesClient, FileHandler};
/// use std::path::Path;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FilesClient::builder().api_key("key").build()?;
/// let handler = FileHandler::new(client);
///
/// handler.upload_directory_with_progress(
/// Path::new("./data"),
/// "/backups",
/// true,
/// |current, total| {
/// println!("Progress: {}/{} ({:.1}%)",
/// current, total, (current as f64 / total as f64) * 100.0);
/// }
/// ).await?;
/// # Ok(())
/// # }
/// ```
pub async fn upload_directory_with_progress<F>(
&self,
local_dir: &Path,
remote_path: &str,
mkdir_parents: bool,
progress: F,
) -> Result<Vec<String>>
where
F: Fn(usize, usize),
{
// Count files first
let total_files = WalkDir::new(local_dir)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.count();
let mut uploaded = Vec::new();
let mut current = 0;
for entry in WalkDir::new(local_dir).into_iter().filter_map(|e| e.ok()) {
if entry.file_type().is_file() {
let local_file = entry.path();
// Calculate relative path
let relative = local_file
.strip_prefix(local_dir)
.map_err(|e| FilesError::IoError(format!("Failed to strip prefix: {}", e)))?
.to_str()
.ok_or_else(|| {
FilesError::IoError(format!(
"Invalid UTF-8 in path: {}",
local_file.display()
))
})?;
// Construct remote path
let remote_file = format!(
"{}/{}",
remote_path.trim_end_matches('/'),
relative.replace('\\', "/")
);
// Read and upload
let data =
std::fs::read(local_file).map_err(|e| FilesError::IoError(e.to_string()))?;
// Upload using the same two-stage process as upload_file
let file_action = FileActionHandler::new(self.client.clone());
let upload_parts = file_action
.begin_upload(&remote_file, Some(data.len() as i64), mkdir_parents)
.await?;
if !upload_parts.is_empty() {
let upload_part = &upload_parts[0];
if let Some(upload_uri) = &upload_part.upload_uri {
let http_client = reqwest::Client::new();
let http_method = upload_part
.http_method
.as_deref()
.unwrap_or("PUT")
.to_uppercase();
let mut request = match http_method.as_str() {
"PUT" => http_client.put(upload_uri),
"POST" => http_client.post(upload_uri),
_ => http_client.put(upload_uri),
};
if let Some(headers) = &upload_part.headers {
for (key, value) in headers {
request = request.header(key, value);
}
}
request.body(data.to_vec()).send().await?;
}
}
uploaded.push(remote_file);
current += 1;
progress(current, total_files);
}
}
Ok(uploaded)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_handler_creation() {
let client = FilesClient::builder().api_key("test-key").build().unwrap();
let _handler = FileHandler::new(client);
}
}