浏览代码

Better format file info, fetch file size with info

timvisee 7 年之前
父节点
当前提交
54bdb922b7
共有 7 个文件被更改,包括 72 次插入24 次删除
  1. 1 1
      Cargo.lock
  2. 41 8
      api/src/action/metadata.rs
  3. 1 1
      cli/Cargo.toml
  4. 27 11
      cli/src/action/info.rs
  5. 0 1
      cli/src/history_tool.rs
  6. 1 1
      cli/src/main.rs
  7. 1 1
      cli/src/util.rs

+ 1 - 1
Cargo.lock

@@ -355,6 +355,7 @@ name = "ffsend"
 version = "0.0.1"
 dependencies = [
  "app_dirs2 2.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "chrono 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "clipboard 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
  "colored 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -369,7 +370,6 @@ dependencies = [
  "rpassword 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde_derive 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)",
- "time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
  "toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
  "version-compare 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
 ]

+ 41 - 8
api/src/action/metadata.rs

@@ -114,14 +114,13 @@ impl<'a> Metadata<'a> {
         let nonce = header_nonce(&response)
             .map_err(|err| MetaError::Nonce(err))?;
 
-        // Parse the metadata response, and decrypt it
-        Ok(MetadataResponse::from(
-            response.json::<RawMetadataResponse>()
-                .map_err(|_| MetaError::Malformed)?
-                .decrypt_metadata(&key)
-                .map_err(|_| MetaError::Decrypt)?,
+        // Parse the metadata response
+        MetadataResponse::from(
+            &response.json::<RawMetadataResponse>()
+                .map_err(|_| MetaError::Malformed)?,
+            &key,
             nonce,
-        ))
+        ).map_err(|_| MetaError::Decrypt)
     }
 }
 
@@ -133,6 +132,9 @@ pub struct RawMetadataResponse {
     /// The encrypted metadata.
     #[serde(rename = "metadata")]
     meta: String,
+
+    /// The file size in bytes.
+    size: u64,
 }
 
 impl RawMetadataResponse {
@@ -162,6 +164,11 @@ impl RawMetadataResponse {
         // Parse the metadata, and return
         Ok(serde_json::from_slice(&meta)?)
     }
+
+    /// Get the file size in bytes.
+    pub fn size(&self) -> u64 {
+        self.size
+    }
 }
 
 /// The decoded and decrypted metadata response, holding all the properties.
@@ -170,24 +177,50 @@ pub struct MetadataResponse {
     /// The actual metadata.
     metadata: MetadataData,
 
+    /// The file size in bytes.
+    size: u64,
+
     /// The metadata nonce.
     nonce: Vec<u8>,
 }
 
 impl<'a> MetadataResponse {
     /// Construct a new response with the given metadata and nonce.
-    pub fn from(metadata: MetadataData, nonce: Vec<u8>) -> Self {
+    pub fn new(metadata: MetadataData, size: u64, nonce: Vec<u8>) -> Self {
         MetadataResponse {
             metadata,
+            size,
             nonce,
         }
     }
 
+    // Construct a new metadata response from the given raw metadata response,
+    // with an additional key set and nonce.
+    //
+    // This internally decrypts the metadata from the raw response.
+    // An error is returned if decrypting the metadata failed.
+    pub fn from(raw: &RawMetadataResponse, key_set: &KeySet, nonce: Vec<u8>)
+        -> Result<Self, FailureError>
+    {
+        Ok(
+            Self::new(
+                raw.decrypt_metadata(key_set)?,
+                raw.size(),
+                nonce,
+            )
+        )
+    }
+
     /// Get the metadata.
     pub fn metadata(&self) -> &MetadataData {
         &self.metadata
     }
 
+    /// Get the file size in bytes.
+    pub fn size(&self) -> u64 {
+        self.size
+    }
+
     /// Get the nonce.
     pub fn nonce(&self) -> &Vec<u8> {
         &self.nonce

+ 1 - 1
cli/Cargo.toml

@@ -17,6 +17,7 @@ no-color = ["colored/no-color"]
 
 [dependencies]
 app_dirs2 = "2.0"
+chrono = "0.4"
 clap = "2.31"
 clipboard = { version = "0.4", optional = true }
 colored = "1.6"
@@ -31,6 +32,5 @@ prettytable-rs = "0.6"
 rpassword = "2.0"
 serde = "1.0"
 serde_derive = "1.0"
-time = "0.1"
 toml = "0.4"
 version-compare = "0.0.6"

+ 27 - 11
cli/src/action/info.rs

@@ -1,3 +1,4 @@
+use chrono::Duration;
 use clap::ArgMatches;
 use failure::Fail;
 use ffsend_api::action::exists::{
@@ -14,7 +15,6 @@ use ffsend_api::file::remote_file::{
     RemoteFile,
 };
 use ffsend_api::reqwest::Client;
-use time::Duration;
 
 use cmd::matcher::{
     Matcher,
@@ -22,7 +22,13 @@ use cmd::matcher::{
     main::MainMatcher,
 };
 use history_tool;
-use util::{ensure_owner_token, ensure_password, print_error};
+use util::{
+    ensure_owner_token,
+    ensure_password,
+    format_bytes,
+    format_duration,
+    print_error,
+};
 
 /// A file info action.
 pub struct Info<'a> {
@@ -81,24 +87,34 @@ impl<'a> Info<'a> {
             )))
             .ok();
 
+        // Get the TTL duration
+        let ttl_millis = info.ttl_millis() as i64;
+        let ttl = Duration::milliseconds(ttl_millis);
+
         // Update file properties
-        file.set_expire_duration(
-            Duration::milliseconds(info.ttl_millis() as i64),
-        );
+        file.set_expire_duration(ttl);
 
         // Add the file to the history
         history_tool::add(&matcher_main, file.clone(), true);
 
-        // Print the result
+        // Print all file details
         println!("ID: {}", file.id());
         if let Some(metadata) = metadata {
-            println!("File name: {}", metadata.metadata().name());
-            println!("MIME type: {}", metadata.metadata().mime());
+            let size = metadata.size();
+            println!("Name: {}", metadata.metadata().name());
+            if size >= 1024 {
+                println!("Size: {} ({} B)", format_bytes(size), size);
+            } else {
+                println!("Size: {}", format_bytes(size));
+            }
+            println!("MIME: {}", metadata.metadata().mime());
         }
         println!("Downloads: {} of {}", info.download_count(), info.download_limit());
-        println!("TTL: {} ms", info.ttl_millis());
-
-        // TODO: show the file size, fetch TTL from metadata, update in history?
+        if ttl_millis >= 60 * 1000 {
+            println!("Expiry: {} ({}s)", format_duration(&ttl), ttl.num_seconds());
+        } else {
+            println!("Expiry: {}", format_duration(&ttl));
+        }
 
         Ok(())
     }

+ 0 - 1
cli/src/history_tool.rs

@@ -119,7 +119,6 @@ pub fn derive_owner_token(matcher_main: &MainMatcher, file: &mut RemoteFile) ->
     match history.get_file(file) {
         Some(f) if f.has_owner_token() => {
             file.set_owner_token(f.owner_token().cloned());
-            println!("DEBUG: Owner token set from history");
             return true;
         },
         _ => return false,

+ 1 - 1
cli/src/main.rs

@@ -1,3 +1,4 @@
+extern crate chrono;
 #[macro_use]
 extern crate clap;
 #[macro_use]
@@ -12,7 +13,6 @@ extern crate rpassword;
 extern crate serde;
 #[macro_use]
 extern crate serde_derive;
-extern crate time;
 
 mod action;
 mod cmd;

+ 1 - 1
cli/src/util.rs

@@ -15,13 +15,13 @@ use std::io::{
 };
 use std::process::{exit, ExitStatus};
 
+use chrono::Duration;
 #[cfg(feature = "clipboard")]
 use self::clipboard::{ClipboardContext, ClipboardProvider};
 use self::colored::*;
 use failure::{err_msg, Fail};
 use ffsend_api::url::Url;
 use rpassword::prompt_password_stderr;
-use time::Duration;
 
 use cmd::matcher::MainMatcher;