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 thumbhash: Option<Vec<u8>>,
49 animated: Option<bool>,
50 },
51 /// File is a video with specific dimensions
52 Video { width: isize, height: isize },
53 /// File is audio
54 Audio,
55 }
56);
57
58impl FileHash {
59 /// Create a file from a file hash
60 pub fn into_file(
61 &self,
62 id: String,
63 tag: String,
64 filename: String,
65 uploader_id: String,
66 ) -> File {
67 File {
68 id,
69 tag,
70 filename,
71 hash: Some(self.id.clone()),
72
73 uploaded_at: Some(Timestamp::now_utc()),
74 uploader_id: Some(uploader_id),
75
76 used_for: None,
77
78 deleted: None,
79 reported: None,
80
81 // TODO: remove this data
82 metadata: self.metadata.clone(),
83 content_type: self.content_type.clone(),
84 size: self.size,
85
86 // TODO: superseded by "used_for"
87 message_id: None,
88 object_id: None,
89 server_id: None,
90 user_id: None,
91 }
92 }
93}