Selaa lähdekoodia

Add --delete flag to delete local file after upload

timvisee 5 vuotta sitten
vanhempi
commit
0337632175
3 muutettua tiedostoa jossa 40 lisäystä ja 0 poistoa
  1. 28 0
      src/action/upload.rs
  2. 5 0
      src/cmd/matcher/upload.rs
  3. 7 0
      src/cmd/subcmd/upload.rs

+ 28 - 0
src/action/upload.rs

@@ -1,4 +1,5 @@
 use std::env::current_dir;
+use std::fs;
 #[cfg(feature = "archive")]
 use std::io::Error as IoError;
 use std::path::Path;
@@ -474,6 +475,29 @@ impl<'a> Upload<'a> {
             }
         }
 
+        // Delete local files after uploading
+        if matcher_upload.delete() {
+            for path in &paths {
+                if path.is_file() {
+                    if let Err(err) = fs::remove_file(path) {
+                        print_error(
+                            Error::Delete(err)
+                                .context("failed to delete local file after upload, ignoring")
+                                .compat(),
+                        );
+                    }
+                } else {
+                    if let Err(err) = fs::remove_dir_all(path) {
+                        print_error(
+                            Error::Delete(err)
+                                .context("failed to delete local directory after upload, ignoring")
+                                .compat(),
+                        );
+                    }
+                }
+            }
+        }
+
         Ok(())
     }
 }
@@ -561,6 +585,10 @@ pub enum Error {
     /// An error occurred while uploading the file.
     #[fail(display = "")]
     Upload(#[cause] UploadError),
+
+    /// An error occurred while deleting a local file after upload.
+    #[fail(display = "failed to delete local file")]
+    Delete(#[cause] IoError),
 }
 
 impl From<VersionError> for Error {

+ 5 - 0
src/cmd/matcher/upload.rs

@@ -119,6 +119,11 @@ impl<'a: 'b, 'b> UploadMatcher<'a> {
         self.matches.is_present("open") || env_var_present("FFSEND_OPEN")
     }
 
+    /// Check whether to to delete local files after uploading.
+    pub fn delete(&self) -> bool {
+        self.matches.is_present("delete")
+    }
+
     /// Check whether to copy the file URL in the user's clipboard, get the copy mode.
     #[cfg(feature = "clipboard")]
     pub fn copy(&self) -> Option<CopyMode> {

+ 7 - 0
src/cmd/subcmd/upload.rs

@@ -40,6 +40,13 @@ impl CmdUpload {
                     .long("open")
                     .short("o")
                     .help("Open the share link in your browser"),
+            )
+            .arg(
+                Arg::with_name("delete")
+                    .long("delete")
+                    .alias("rm")
+                    .short("D")
+                    .help("Delete local file after upload"),
             );
 
         // Optional archive support