hydrus-api 0.10.2

A rust wrapper for the hydrus client api
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use crate::api_core::common::{
    FileIdentifier, FileRecord, FileSelection, FileServiceSelection, ServiceIdentifier,
};
use crate::api_core::endpoints::adding_tags::{AddTagsRequestBuilder, TagAction};
use crate::api_core::endpoints::searching_and_fetching_files::{FileFullMetadata, FullMetadata};
use crate::error::{Error, Result};
use crate::utils::tag_list_to_string_list;
use crate::wrapper::builders::delete_files_builder::DeleteFilesBuilder;
use crate::wrapper::builders::notes_builder::AddNotesBuilder;

use crate::wrapper::tag::Tag;
use crate::Client;
use chrono::{NaiveDateTime, TimeZone, Utc};
use mime::Mime;
use std::collections::HashMap;

#[derive(Clone, Debug, PartialOrd, PartialEq)]
pub enum FileStatus {
    ReadyForImport,
    InDatabase,
    Deleted,
    Unknown,
}

impl Eq for FileStatus {}

impl From<u8> for FileStatus {
    fn from(v: u8) -> FileStatus {
        match v {
            3 => FileStatus::Deleted,
            0 => FileStatus::ReadyForImport,
            _ => FileStatus::InDatabase,
        }
    }
}

#[derive(Clone)]
pub struct HydrusFile {
    pub(crate) client: Client,
    pub id: FileIdentifier,
    pub status: FileStatus,
    pub(crate) metadata: Option<FileFullMetadata>,
}

impl HydrusFile {
    pub(crate) fn from_hash<S: ToString>(client: Client, hash: S) -> Self {
        Self {
            client,
            id: FileIdentifier::Hash(hash.to_string()),
            status: FileStatus::Unknown,
            metadata: None,
        }
    }

    pub(crate) fn from_raw_status_and_hash<S: ToString>(
        client: Client,
        status: u8,
        hash: S,
    ) -> Self {
        Self {
            client,
            id: FileIdentifier::Hash(hash.to_string()),
            status: status.into(),
            metadata: None,
        }
    }

    pub(crate) fn from_metadata(client: Client, metadata: FileFullMetadata) -> Self {
        let status = if metadata.is_trashed {
            FileStatus::Deleted
        } else {
            FileStatus::InDatabase
        };

        Self {
            client,
            id: FileIdentifier::Hash(metadata.basic_metadata.identifiers.hash.clone()),
            status,
            metadata: Some(metadata),
        }
    }

    /// Deletes the internally stored metadata about the file retrieves it again
    pub async fn update(&mut self) -> Result<()> {
        self.metadata = None;
        self.metadata().await?;
        Ok(())
    }

    /// Returns the hash of the file
    /// if the file identifier is an id it calls hydrus to resolve the file
    pub async fn hash(&mut self) -> Result<String> {
        match &self.id {
            FileIdentifier::ID(_) => {
                let metadata = self.metadata().await?;
                Ok(metadata.basic_metadata.identifiers.hash.clone())
            }
            FileIdentifier::Hash(hash) => Ok(hash.clone()),
        }
    }

    /// Returns the file size in bytes
    pub async fn size(&mut self) -> Result<Option<u64>> {
        let metadata = self.metadata().await?;

        Ok(metadata.basic_metadata.size.clone())
    }

    /// Returns the mime of the file
    pub async fn mime(&mut self) -> Result<Mime> {
        let metadata = self.metadata().await?;
        let mime = metadata
            .basic_metadata
            .mime
            .as_str()
            .parse()
            .map_err(|_| Error::InvalidMime(metadata.basic_metadata.mime.clone()))?;

        Ok(mime)
    }

    /// Return the file extension
    pub async fn ext(&mut self) -> Result<String> {
        let metadata = self.metadata().await?;

        Ok(metadata.basic_metadata.ext.clone())
    }

    /// Returns the dimensions of the file in pixels
    pub async fn dimensions(&mut self) -> Result<Option<(u32, u32)>> {
        let metadata = self.metadata().await?;
        if let (Some(width), Some(height)) = (
            &metadata.basic_metadata.width,
            &metadata.basic_metadata.height,
        ) {
            Ok(Some((*width, *height)))
        } else {
            Ok(None)
        }
    }

    /// Returns the duration of the file in seconds if it's a video
    pub async fn duration(&mut self) -> Result<Option<u64>> {
        let metadata = self.metadata().await?;

        Ok(metadata.basic_metadata.duration.clone())
    }

    /// Returns the number of frames of the file if it's a video
    pub async fn num_frames(&mut self) -> Result<Option<u64>> {
        let metadata = self.metadata().await?;

        Ok(metadata.basic_metadata.num_frames.clone())
    }

    /// Returns if the file has audio
    pub async fn has_audio(&mut self) -> Result<bool> {
        let metadata = self.metadata().await?;

        Ok(metadata.basic_metadata.has_audio.unwrap_or(false))
    }

    /// Returns if the file is currently in the inbox
    pub async fn in_inbox(&mut self) -> Result<bool> {
        let metadata = self.metadata().await?;

        Ok(metadata.is_inbox)
    }

    /// Returns if the file is stored locally
    pub async fn stored_locally(&mut self) -> Result<bool> {
        let metadata = self.metadata().await?;

        Ok(metadata.is_local)
    }

    /// Returns if the file has been moved to the trash
    pub async fn moved_to_trashed(&mut self) -> Result<bool> {
        let metadata = self.metadata().await?;

        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
            .basic_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)
    }

    /// Returns the time the file was deleted for a specified file service
    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)
    }

    /// Creates a request builder to delete the file
    pub fn delete(&mut self) -> DeleteFilesBuilder {
        self.metadata = None;
        DeleteFilesBuilder::new(self.client.clone()).add_file(self.id.clone())
    }

    /// Undeletes the file for the given service or all services
    /// if `FileServiceSelection::none` is passed
    pub async fn undelete(&mut self, service_selection: FileServiceSelection) -> Result<()> {
        let hash = self.hash().await?;
        self.metadata = None;
        self.client
            .undelete_files(FileSelection::by_hash(hash), service_selection)
            .await
    }

    /// Archives the file in all passed file services or all configured services
    /// if no selection is passed
    pub async fn archive(&mut self, service_selection: FileServiceSelection) -> Result<()> {
        let hash = self.hash().await?;
        self.metadata = None;
        self.client
            .archive_files(FileSelection::by_hash(hash), service_selection)
            .await
    }

    /// Unarchives the file for the given services
    pub async fn unarchive(&mut self, service_selection: FileServiceSelection) -> Result<()> {
        let hash = self.hash().await?;
        self.metadata = None;
        self.client
            .unarchive_files(FileSelection::by_hash(hash), service_selection)
            .await
    }

    /// Associates the file with a list of urls
    pub async fn associate_urls(&mut self, urls: Vec<String>) -> Result<()> {
        let hash = self.hash().await?;
        self.client.associate_urls(urls, vec![hash]).await
    }

    /// Disassociates the file with a list of urls
    pub async fn disassociate_urls(&mut self, urls: Vec<String>) -> Result<()> {
        let hash = self.hash().await?;
        self.client.disassociate_urls(urls, vec![hash]).await
    }

    /// Returns map mapping lists of tags to services.
    ///
    /// Deprecation: Use [HydrusFile::services_with_tags] instead.
    #[deprecated(note = "Deprecated in the official API. Use services_with_tags instead.")]
    pub async fn service_names_with_tags(
        &mut self,
    ) -> Result<HashMap<ServiceIdentifier, Vec<Tag>>> {
        let metadata = self.metadata().await?;
        let mut tag_mappings = HashMap::new();

        for (service, service_tags) in &metadata.tags {
            let mut tag_list = Vec::new();

            for (_, tags) in &service_tags.storage_tags {
                tag_list.append(&mut tags.into_iter().map(|t| t.into()).collect())
            }
            tag_mappings.insert(ServiceIdentifier::Key(service.clone()), tag_list);
        }

        Ok(tag_mappings)
    }

    /// Returns a mapping with service ids mapped to tags
    pub async fn services_with_tags(&mut self) -> Result<HashMap<ServiceIdentifier, Vec<Tag>>> {
        let metadata = self.metadata().await?;
        let mut tag_mappings = HashMap::new();

        for (service, service_tags) in &metadata.tags {
            let mut tag_list = Vec::new();

            for (_, tags) in &service_tags.storage_tags {
                tag_list.append(&mut tags.into_iter().map(|t| t.into()).collect())
            }
            tag_mappings.insert(ServiceIdentifier::Key(service.clone()), tag_list);
        }

        Ok(tag_mappings)
    }

    /// Returns a list of all tags assigned to the file
    pub async fn tags(&mut self) -> Result<Vec<Tag>> {
        let mut tag_list = Vec::new();
        let tag_mappings = self.services_with_tags().await?;

        for (_, mut tags) in tag_mappings {
            tag_list.append(&mut tags);
        }

        Ok(tag_list)
    }

    /// Adds tags for a specific service to the file
    pub async fn add_tags(&mut self, service_key: String, tags: Vec<Tag>) -> Result<()> {
        let hash = self.hash().await?;
        let request = AddTagsRequestBuilder::default()
            .add_hash(hash)
            .add_tags(service_key, tag_list_to_string_list(tags))
            .build();

        self.client.add_tags(request).await
    }

    /// Allows modification of tags by using the defined tag actions
    pub async fn modify_tags(
        &mut self,
        service_key: String,
        action: TagAction,
        tags: Vec<Tag>,
    ) -> Result<()> {
        let hash = self.hash().await?;
        let mut reqwest = AddTagsRequestBuilder::default().add_hash(hash);

        for tag in tags {
            reqwest =
                reqwest.add_tag_with_action(service_key.clone(), tag.to_string(), action.clone());
        }

        self.client.add_tags(reqwest.build()).await
    }

    /// Creates a builder to add notes to the file
    pub fn add_notes(&self) -> AddNotesBuilder {
        AddNotesBuilder::new(self.client.clone(), self.id.clone())
    }

    /// Deletes a single note from the file
    pub async fn delete_note<S1: ToString>(&self, name: S1) -> Result<()> {
        self.client
            .delete_notes(self.id.clone(), vec![name.to_string()])
            .await
    }

    /// Deletes multiple notes from the file
    pub async fn delete_notes<I: IntoIterator<Item = S>, S: ToString>(
        &self,
        names: I,
    ) -> Result<()> {
        let names = names.into_iter().map(|n: S| n.to_string()).collect();
        self.client.delete_notes(self.id.clone(), names).await
    }

    /// Retrieves the file record bytes
    pub async fn retrieve(&self) -> Result<FileRecord> {
        self.client.get_file(self.id.clone()).await
    }

    /// Returns the metadata for the given file
    /// if there's already known metadata about the file it uses that
    async fn metadata(&mut self) -> Result<&FileFullMetadata> {
        if self.metadata.is_none() {
            let metadata = self
                .client
                .get_file_metadata_by_identifier::<FullMetadata>(self.id.clone())
                .await?;
            self.status = if metadata.is_trashed {
                FileStatus::Deleted
            } else {
                FileStatus::InDatabase
            };
            self.metadata = Some(metadata);
        }

        Ok(self.metadata.as_ref().unwrap())
    }
}