Bläddra i källkod

Pass parameter data to upload action, not raw downloads count

timvisee 7 år sedan
förälder
incheckning
928b3d9beb
3 ändrade filer med 33 tillägg och 16 borttagningar
  1. 7 0
      api/src/action/params.rs
  2. 7 15
      api/src/action/upload.rs
  3. 19 1
      cli/src/action/upload.rs

+ 7 - 0
api/src/action/params.rs

@@ -158,6 +158,13 @@ pub struct ParamsData {
 }
 
 impl ParamsData {
+    /// Construct a new parameters object, that is empty.
+    pub fn new() -> Self {
+        ParamsData {
+            downloads: None,
+        }
+    }
+
     /// Create a new parameters data object, with the given parameters.
     // TODO: the downloads must be between bounds
     pub fn from(downloads: Option<u8>) -> Self {

+ 7 - 15
api/src/action/upload.rs

@@ -64,8 +64,8 @@ pub struct Upload {
     /// An optional password to protect the file with.
     password: Option<String>,
 
-    /// An optional download limit.
-    downloads: Option<u8>,
+    /// Optional file parameters to set.
+    params: Option<ParamsData>,
 }
 
 impl Upload {
@@ -75,14 +75,14 @@ impl Upload {
         path: PathBuf,
         name: Option<String>,
         password: Option<String>,
-        downloads: Option<u8>,
+        params: Option<ParamsData>,
     ) -> Self {
         Self {
             host,
             path,
             name,
             password,
-            downloads,
+            params,
         }
     }
 
@@ -125,20 +125,12 @@ impl Upload {
 
         // Change the password if set
         if let Some(password) = self.password {
-            Password::new(
-                &result,
-                &password,
-                nonce.clone(),
-            ).invoke(client)?;
+            Password::new(&result, &password, nonce.clone()).invoke(client)?;
         }
 
         // Change parameters if set
-        if self.downloads.is_some() {
-            Params::new(
-                &result,
-                ParamsData::from(self.downloads),
-                nonce.clone(),
-            ).invoke(client)?;
+        if let Some(params) = self.params {
+            Params::new(&result, params, nonce.clone()).invoke(client)?;
         }
 
         Ok(result)

+ 19 - 1
cli/src/action/upload.rs

@@ -2,6 +2,7 @@ use std::path::Path;
 use std::sync::{Arc, Mutex};
 
 use failure::{err_msg, Fail};
+use ffsend_api::action::params::ParamsData;
 use ffsend_api::action::upload::Upload as ApiUpload;
 use ffsend_api::reqwest::Client;
 
@@ -38,13 +39,30 @@ impl<'a> Upload<'a> {
         // Create a progress bar reporter
         let bar = Arc::new(Mutex::new(ProgressBar::new_upload()));
 
+        // Build a parameters object to set for the file
+        let params = {
+            // Build an empty parameters object
+            let mut params = ParamsData::new();
+
+            // Set the downloads
+            // TODO: do not unwrap, handle the error
+            params.set_downloads(self.cmd.downloads()).unwrap();
+
+            // Wrap the data in an option if not empty
+            if params.is_empty() {
+                None
+            } else {
+                Some(params)
+            }
+        };
+
         // Execute an upload action
         let file = ApiUpload::new(
             host,
             path,
             self.cmd.name().map(|name| name.to_owned()),
             self.cmd.password(),
-            self.cmd.downloads(),
+            params,
         ).invoke(&client, bar)?;
 
         // Get the download URL, and report it in the console