Add support for file service metadata timestamps

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/9/head
trivernis 2 years ago
parent 2b5599b821
commit b7f902e3dd
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -45,6 +45,8 @@ pub struct FileMetadataInfo {
pub width: Option<u32>,
pub height: Option<u32>,
pub duration: Option<u64>,
pub time_modified: Option<u64>,
pub file_services: FileMetadataServices,
pub has_audio: Option<bool>,
pub num_frames: Option<u64>,
pub num_words: Option<u64>,
@ -54,6 +56,7 @@ pub struct FileMetadataInfo {
pub known_urls: Vec<String>,
pub service_names_to_statuses_to_tags: HashMap<String, HashMap<String, Vec<String>>>,
pub service_names_to_statuses_to_display_tags: HashMap<String, HashMap<String, Vec<String>>>,
pub service_keys_to_statuses_to_display_tags: HashMap<String, HashMap<String, Vec<String>>>,
}
#[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<String, FileMetadataServiceCurrent>,
pub deleted: HashMap<String, FileMetadataServiceDeleted>,
}
#[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,

@ -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<String>> {
let metadata = self.metadata().await?;
Ok(&metadata.known_urls)
}
/// Returns the modified time of the file
pub async fn time_modified(&mut self) -> Result<Option<NaiveDateTime>> {
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<S: AsRef<str>>(
&mut self,
service_key: S,
) -> Result<Option<NaiveDateTime>> {
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<S: AsRef<str>>(
&mut self,
service_key: S,
) -> Result<Option<NaiveDateTime>> {
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<String>) -> Result<()> {
let hash = self.hash().await?;

@ -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]

@ -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());
}

Loading…
Cancel
Save