From b7f902e3dd0c5b8c000742e0dd67964feb5400c1 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 27 Feb 2022 15:21:53 +0100 Subject: [PATCH] Add support for file service metadata timestamps Signed-off-by: trivernis --- src/api_core/common.rs | 20 +++++++ src/wrapper/hydrus_file.rs | 56 +++++++++++++++++++ .../test_searching_and_fetching_files.rs | 6 +- tests/wrapper/test_files.rs | 3 + 4 files changed, 82 insertions(+), 3 deletions(-) diff --git a/src/api_core/common.rs b/src/api_core/common.rs index 9fc631a..56668fa 100644 --- a/src/api_core/common.rs +++ b/src/api_core/common.rs @@ -45,6 +45,8 @@ pub struct FileMetadataInfo { pub width: Option, pub height: Option, pub duration: Option, + pub time_modified: Option, + pub file_services: FileMetadataServices, pub has_audio: Option, pub num_frames: Option, pub num_words: Option, @@ -54,6 +56,7 @@ pub struct FileMetadataInfo { pub known_urls: Vec, pub service_names_to_statuses_to_tags: HashMap>>, pub service_names_to_statuses_to_display_tags: HashMap>>, + pub service_keys_to_statuses_to_display_tags: HashMap>>, } #[derive(Clone, Debug)] @@ -74,6 +77,23 @@ pub struct FileRecord { pub mime_type: String, } +#[derive(Clone, Default, Debug, Deserialize)] +pub struct FileMetadataServices { + pub current: HashMap, + pub deleted: HashMap, +} + +#[derive(Clone, Debug, Deserialize)] +pub struct FileMetadataServiceCurrent { + pub time_imported: u64, +} + +#[derive(Clone, Debug, Deserialize)] +pub struct FileMetadataServiceDeleted { + pub time_deleted: u64, + pub time_imported: u64, +} + #[derive(Clone, Debug, Deserialize)] pub struct PageInformation { pub name: String, diff --git a/src/wrapper/hydrus_file.rs b/src/wrapper/hydrus_file.rs index fd9c8ca..1e612db 100644 --- a/src/wrapper/hydrus_file.rs +++ b/src/wrapper/hydrus_file.rs @@ -5,6 +5,7 @@ use crate::utils::tag_list_to_string_list; use crate::wrapper::service::ServiceName; use crate::wrapper::tag::Tag; use crate::Client; +use chrono::{NaiveDateTime, TimeZone, Utc}; use mime::Mime; use std::collections::HashMap; @@ -171,6 +172,61 @@ impl HydrusFile { Ok(metadata.is_trashed) } + /// Returns all urls associated with the file + pub async fn urls(&mut self) -> Result<&Vec> { + let metadata = self.metadata().await?; + + Ok(&metadata.known_urls) + } + + /// Returns the modified time of the file + pub async fn time_modified(&mut self) -> Result> { + let metadata = self.metadata().await?; + let naive_time_modified = metadata + .time_modified + .map(|m| Utc.timestamp_millis(m as i64).naive_utc()); + + Ok(naive_time_modified) + } + + /// Returns the imported time of the file for a given file service key + pub async fn time_imported>( + &mut self, + service_key: S, + ) -> Result> { + let metadata = self.metadata().await?; + let naive_time_imported = metadata + .file_services + .current + .get(service_key.as_ref()) + .map(|s| s.time_imported) + .or_else(|| { + metadata + .file_services + .deleted + .get(service_key.as_ref()) + .map(|s| s.time_imported) + }) + .map(|millis| Utc.timestamp_millis(millis as i64).naive_utc()); + + Ok(naive_time_imported) + } + + pub async fn time_deleted>( + &mut self, + service_key: S, + ) -> Result> { + let metadata = self.metadata().await?; + let naive_time_deleted = metadata + .file_services + .deleted + .get(service_key.as_ref()) + .map(|service| service.time_deleted) + .map(|millis| Utc.timestamp_millis(millis as i64).naive_utc()); + + Ok(naive_time_deleted) + } + /// Associates the file with a list of urls pub async fn associate_urls(&mut self, urls: Vec) -> Result<()> { let hash = self.hash().await?; diff --git a/tests/client/test_searching_and_fetching_files.rs b/tests/client/test_searching_and_fetching_files.rs index 7d42220..dde77ad 100644 --- a/tests/client/test_searching_and_fetching_files.rs +++ b/tests/client/test_searching_and_fetching_files.rs @@ -44,13 +44,13 @@ async fn is_searches_file_hashes() { #[tokio::test] async fn it_fetches_file_metadata() { let client = common::get_client(); - let response = client + client .get_file_metadata( vec![], vec!["0000000000000000000000000000000000000000000000000000000000000000".to_string()], ) - .await; - assert!(response.is_ok()); // Even if the file doesn't exist it still returns some information about it + .await + .unwrap(); } #[tokio::test] diff --git a/tests/wrapper/test_files.rs b/tests/wrapper/test_files.rs index b4a7947..f464526 100644 --- a/tests/wrapper/test_files.rs +++ b/tests/wrapper/test_files.rs @@ -87,4 +87,7 @@ async fn it_retrieves_metadata() { assert!(file.dimensions().await.unwrap().is_some()); assert!(file.stored_locally().await.unwrap()); assert!(file.duration().await.unwrap().is_none()); + assert!(file.time_modified().await.is_ok()); + assert!(file.time_deleted("000").await.is_ok()); + assert!(file.time_imported("000").await.is_ok()); }