Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 0565522

Browse files
open-schnickGitea
authored andcommitted
Implemented check if the path contains rclone timestamp
1 parent de925ca commit 0565522

File tree

4 files changed

+124
-11
lines changed

4 files changed

+124
-11
lines changed

Cargo.lock

Lines changed: 42 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ tokio = "1.21.1"
1111
tracing = "0.1.36"
1212
url = "2.3.1"
1313
filefighter-api = { path = "../api" }
14+
chrono = "0.4.22"

server/src/backend/storage_backend.rs

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::{metadata::InodeMetaData, utils::validate_and_normalize_path};
22
use crate::auth::user::FileFighterUser;
33
use async_trait::async_trait;
4+
use chrono::NaiveDateTime;
45
use filefighter_api::ffs_api::{
56
endpoints::{
67
create_directory, delete_inode, download_file, get_contents_of_folder, get_inode,
@@ -14,7 +15,7 @@ use libunftp::storage::{
1415
};
1516
use std::{
1617
fmt::Debug,
17-
path::{Path, PathBuf},
18+
path::{Component, Path, PathBuf},
1819
};
1920
use tokio::io::AsyncRead;
2021
use tracing::{debug, error, instrument, warn};
@@ -33,18 +34,31 @@ impl StorageBackend<FileFighterUser> for FileFighter {
3334
FEATURE_RESTART
3435
}
3536

37+
/// Endpoint to request Metadata for a inode
38+
///
39+
/// # Rclone
40+
/// In some cases the path consists of /<date>/ /acutal_path.
41+
/// This means that rclone wants to update the modification date of that inode at the path
3642
#[instrument(skip(self), level = "debug")]
3743
async fn metadata<P: AsRef<Path> + Send + Debug>(
3844
&self,
3945
user: &FileFighterUser,
4046
path: P,
4147
) -> Result<Self::Metadata> {
42-
let path = validate_and_normalize_path(path)?;
43-
let inode = get_inode(&self.api_config, &path, &user.token)
44-
.await
45-
.map_err(transform_to_ftp_error)?;
48+
let path = path.as_ref();
49+
50+
todo!()
51+
// // rclone wants to update time
52+
// if path_contains_rclone_modification_date(path)? {
53+
// } else {
54+
// // regular metadata request
55+
// let path = validate_and_normalize_path(path)?;
56+
// let inode = get_inode(&self.api_config, &path, &user.token)
57+
// .await
58+
// .map_err(transform_to_ftp_error)?;
4659

47-
Ok(InodeMetaData::from(&inode, user.id))
60+
// Ok(InodeMetaData::from(&inode, user.id))
61+
// }
4862
}
4963

5064
#[instrument(skip(self), level = "debug")]
@@ -259,3 +273,63 @@ fn transform_to_ftp_error(error: ApiError) -> Error {
259273
}
260274
}
261275
}
276+
277+
// IDEA: check if rclone does try to update the root folder
278+
fn path_contains_rclone_modification_date(path: &Path) -> Option<(NaiveDateTime, PathBuf)> {
279+
let mut components: Vec<Component> = path.components().collect();
280+
281+
// needs to be at least / and date<whitespace>
282+
if components.len() < 2 {
283+
return None;
284+
}
285+
286+
// does exist
287+
let mut timestamp_component = components[1].as_os_str().to_str()?.to_owned();
288+
289+
#[allow(clippy::unwrap_used)]
290+
// if root path does not end with whitespace
291+
if timestamp_component.is_empty() || timestamp_component.pop().unwrap() != ' ' {
292+
return None;
293+
}
294+
295+
// the rest of the root folder needs to be in this format
296+
// yyyymmddhhmmss
297+
let parsed_time = NaiveDateTime::parse_from_str(&timestamp_component, "%Y%m%d%H%M%S").ok()?;
298+
299+
// remove the timestamp component
300+
components.remove(1);
301+
302+
Some((parsed_time, components.iter().collect()))
303+
}
304+
305+
#[cfg(test)]
306+
mod tests {
307+
use super::path_contains_rclone_modification_date;
308+
use chrono::NaiveDateTime;
309+
use std::{path::PathBuf, str::FromStr};
310+
311+
#[test]
312+
fn timestamp_parsing_works() {
313+
let result = NaiveDateTime::parse_from_str("20221003093709", "%Y%m%d%H%M%S").unwrap();
314+
let resulting_string = result.to_string();
315+
assert_eq!("2022-10-03 09:37:09", resulting_string)
316+
}
317+
318+
#[test]
319+
fn path_contains_rclone_modification_date_works() {
320+
let path = PathBuf::from_str("/20221003093709 /Home/School").unwrap();
321+
322+
let option = path_contains_rclone_modification_date(&path);
323+
324+
match option {
325+
Some(result) => {
326+
assert_eq!(
327+
NaiveDateTime::parse_from_str("20221003093709", "%Y%m%d%H%M%S").unwrap(),
328+
result.0
329+
);
330+
assert_eq!(PathBuf::from_str("/Home/School").unwrap(), result.1);
331+
}
332+
None => panic!("Expected some value here."),
333+
}
334+
}
335+
}

server/src/backend/utils_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[cfg(test)]
22
mod path_normalize_tests {
3-
use crate::utils::validate_and_normalize_path;
3+
use crate::backend::utils::validate_and_normalize_path;
44

55
fn validation_works(before: &str, after: &str) {
66
println!("Works: --- {} ------------", before);

0 commit comments

Comments
 (0)