From ff600235a684e5543ef3a39745fc2a45dee0d3b0 Mon Sep 17 00:00:00 2001 From: MedzikUser Date: Tue, 3 May 2022 11:26:29 +0200 Subject: [PATCH] server (/fs/list): add file modified --- Cargo.lock | 1 + server/Cargo.toml | 1 + server/src/fs/list.rs | 40 ++++++++++++++++++++++++++++++++-------- types/src/fs/list.rs | 1 + 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 12d1862..49e22ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -633,6 +633,7 @@ dependencies = [ "axum-auth", "base64", "byte-unit", + "chrono", "homedisk-database", "homedisk-types", "hyper", diff --git a/server/Cargo.toml b/server/Cargo.toml index f05e827..59c3e70 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -17,3 +17,4 @@ axum-auth = "0.2.0" jsonwebtoken = "8.1.0" base64 = "0.13.0" byte-unit = "4.0.14" +chrono = "0.4.19" diff --git a/server/src/fs/list.rs b/server/src/fs/list.rs index b3ee952..c912faa 100644 --- a/server/src/fs/list.rs +++ b/server/src/fs/list.rs @@ -1,10 +1,12 @@ use std::path::PathBuf; +use std::time::SystemTime; use std::{fs, io}; use crate::fs::validate_path; use axum::{extract::rejection::JsonRejection, Extension, Json}; use axum_auth::AuthBearer; use byte_unit::Byte; +use chrono::{DateTime, NaiveDateTime, Utc}; use homedisk_database::Database; use homedisk_types::fs::list::DirInfo; use homedisk_types::{ @@ -65,19 +67,41 @@ pub async fn handle( .map_err(|err| ServerError::FsError(FsError::UnknowError(err.to_string())))?; let name = f.path().display().to_string().replace(&path, ""); - let file_size = Byte::from_bytes(metadata.len().into()).get_appropriate_unit(true); if metadata.is_dir() { - dirs.push(DirInfo { - name, - size: Byte::from_bytes(dir_size(f.path().display().to_string()).unwrap() as u128) - .get_appropriate_unit(true) - .to_string(), - }) + let size = Byte::from_bytes(dir_size(f.path().display().to_string()).unwrap() as u128) + .get_appropriate_unit(true) + .to_string(); + + dirs.push(DirInfo { name, size }) } else { + let size = Byte::from_bytes(metadata.len().into()) + .get_appropriate_unit(true) + .to_string(); + + let elapsed = metadata.modified().unwrap().elapsed().unwrap(); + + let seconds = elapsed.as_secs(); + let minutes = seconds / 60; + let hours = minutes / 60; + let days = hours / 24; + + let modified; + + if days > 1 { + modified = format!("{} day(s)", days) + } else if hours > 1 { + modified = format!("{} hour(s)", hours) + } else if minutes > 1 { + modified = format!("{} minute(s)", minutes) + } else { + modified = format!("{} second(s)", seconds) + } + files.push(FileInfo { name, - size: file_size.to_string(), + size, + modified, }) } } diff --git a/types/src/fs/list.rs b/types/src/fs/list.rs index 8055908..3ac1fb5 100644 --- a/types/src/fs/list.rs +++ b/types/src/fs/list.rs @@ -15,6 +15,7 @@ pub struct Response { pub struct FileInfo { pub name: String, pub size: String, + pub modified: String, } #[derive(Debug, Serialize, Deserialize, Clone)]