revolt_database/models/file_hashes/model.rs
1use iso8601_timestamp::Timestamp;
2
3use crate::File;
4
5auto_derived_partial!(
6 /// File hash
7 pub struct FileHash {
8 /// Sha256 hash of the file
9 #[serde(rename = "_id")]
10 pub id: String,
11 /// Sha256 hash of file after it has been processed
12 pub processed_hash: String,
13
14 /// When this file was created in system
15 pub created_at: Timestamp,
16
17 /// The bucket this file is stored in
18 pub bucket_id: String,
19 /// The path at which this file exists in
20 pub path: String,
21 /// Cryptographic nonce used to encrypt this file
22 pub iv: String,
23
24 /// Parsed metadata of this file
25 pub metadata: Metadata,
26 /// Raw content type of this file
27 pub content_type: String,
28 /// Size of this file (in bytes)
29 pub size: isize,
30 },
31 "PartialFileHash"
32);
33
34auto_derived!(
35 /// Metadata associated with a file
36 #[serde(tag = "type")]
37 #[derive(Default)]
38 pub enum Metadata {
39 /// File is just a generic uncategorised file
40 #[default]
41 File,
42 /// File contains textual data and should be displayed as such
43 Text,
44 /// File is an image with specific dimensions
45 Image {
46 width: isize,
47 height: isize,
48 // animated: bool // TODO: https://docs.rs/image/latest/image/trait.AnimationDecoder.html for APNG support
49 },
50 /// File is a video with specific dimensions
51 Video { width: isize, height: isize },
52 /// File is audio
53 Audio,
54 }
55);
56
57impl FileHash {
58 /// Create a file from a file hash
59 pub fn into_file(
60 &self,
61 id: String,
62 tag: String,
63 filename: String,
64 uploader_id: String,
65 ) -> File {
66 File {
67 id,
68 tag,
69 filename,
70 hash: Some(self.id.clone()),
71
72 uploaded_at: Some(Timestamp::now_utc()),
73 uploader_id: Some(uploader_id),
74
75 used_for: None,
76
77 deleted: None,
78 reported: None,
79
80 // TODO: remove this data
81 metadata: self.metadata.clone(),
82 content_type: self.content_type.clone(),
83 size: self.size,
84
85 // TODO: superseded by "used_for"
86 message_id: None,
87 object_id: None,
88 server_id: None,
89 user_id: None,
90 }
91 }
92}