tetratto_core/database/
uploads.rs

1use oiseau::cache::Cache;
2use crate::model::auth::User;
3use crate::model::permissions::FinePermission;
4use crate::model::{Error, Result, uploads::MediaUpload};
5use crate::{auto_method, DataManager};
6
7use oiseau::PostgresRow;
8
9use oiseau::{execute, get, query_rows, params};
10
11impl DataManager {
12    /// Get a [`MediaUpload`] from an SQL row.
13    pub(crate) fn get_upload_from_row(x: &PostgresRow) -> MediaUpload {
14        MediaUpload {
15            id: get!(x->0(i64)) as usize,
16            created: get!(x->1(i64)) as usize,
17            owner: get!(x->2(i64)) as usize,
18            what: serde_json::from_str(&get!(x->3(String))).unwrap(),
19            alt: get!(x->4(String)),
20        }
21    }
22
23    auto_method!(get_upload_by_id(usize as i64)@get_upload_from_row -> "SELECT * FROM uploads WHERE id = $1" --name="upload" --returns=MediaUpload --cache-key-tmpl="atto.upload:{}");
24
25    /// Get all uploads (paginated).
26    ///
27    /// # Arguments
28    /// * `batch` - the limit of items in each page
29    /// * `page` - the page number
30    pub async fn get_uploads(&self, batch: usize, page: usize) -> Result<Vec<MediaUpload>> {
31        let conn = match self.0.connect().await {
32            Ok(c) => c,
33            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
34        };
35
36        let res = query_rows!(
37            &conn,
38            "SELECT * FROM uploads ORDER BY created DESC LIMIT $1 OFFSET $2",
39            &[&(batch as i64), &((page * batch) as i64)],
40            |x| { Self::get_upload_from_row(x) }
41        );
42
43        if res.is_err() {
44            return Err(Error::GeneralNotFound("upload".to_string()));
45        }
46
47        Ok(res.unwrap())
48    }
49
50    /// Get all uploads by their owner (paginated).
51    ///
52    /// # Arguments
53    /// * `owner` - the ID of the owner of the upload
54    /// * `batch` - the limit of items in each page
55    /// * `page` - the page number
56    pub async fn get_uploads_by_owner(
57        &self,
58        owner: usize,
59        batch: usize,
60        page: usize,
61    ) -> Result<Vec<MediaUpload>> {
62        let conn = match self.0.connect().await {
63            Ok(c) => c,
64            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
65        };
66
67        let res = query_rows!(
68            &conn,
69            "SELECT * FROM uploads WHERE owner = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
70            &[&(owner as i64), &(batch as i64), &((page * batch) as i64)],
71            |x| { Self::get_upload_from_row(x) }
72        );
73
74        if res.is_err() {
75            return Err(Error::GeneralNotFound("upload".to_string()));
76        }
77
78        Ok(res.unwrap())
79    }
80
81    /// Get all uploads by their owner.
82    ///
83    /// # Arguments
84    /// * `owner` - the ID of the owner of the upload
85    pub async fn get_uploads_by_owner_all(&self, owner: usize) -> Result<Vec<MediaUpload>> {
86        let conn = match self.0.connect().await {
87            Ok(c) => c,
88            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
89        };
90
91        let res = query_rows!(
92            &conn,
93            "SELECT * FROM uploads WHERE owner = $1 ORDER BY created DESC",
94            &[&(owner as i64)],
95            |x| { Self::get_upload_from_row(x) }
96        );
97
98        if res.is_err() {
99            return Err(Error::GeneralNotFound("upload".to_string()));
100        }
101
102        Ok(res.unwrap())
103    }
104
105    /// Create a new upload in the database.
106    ///
107    /// # Arguments
108    /// * `data` - a mock [`MediaUpload`] object to insert
109    pub async fn create_upload(&self, data: MediaUpload) -> Result<MediaUpload> {
110        let conn = match self.0.connect().await {
111            Ok(c) => c,
112            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
113        };
114
115        let res = execute!(
116            &conn,
117            "INSERT INTO uploads VALUES ($1, $2, $3, $4, $5)",
118            params![
119                &(data.id as i64),
120                &(data.created as i64),
121                &(data.owner as i64),
122                &serde_json::to_string(&data.what).unwrap().as_str(),
123                &data.alt,
124            ]
125        );
126
127        if let Err(e) = res {
128            return Err(Error::DatabaseError(e.to_string()));
129        }
130
131        // return
132        Ok(data)
133    }
134
135    pub async fn delete_upload(&self, id: usize) -> Result<()> {
136        // if !user.permissions.check(FinePermission::MANAGE_UPLOADS) {
137        //     return Err(Error::NotAllowed);
138        // }
139
140        // delete file
141        // it's most important that the file gets off the file system first, even
142        // if there's an issue in the database
143        //
144        // the actual file takes up much more space than the database entry.
145        let upload = self.get_upload_by_id(id).await?;
146        upload.remove(&self.0.0)?;
147
148        // delete from database
149        let conn = match self.0.connect().await {
150            Ok(c) => c,
151            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
152        };
153
154        let res = execute!(&conn, "DELETE FROM uploads WHERE id = $1", &[&(id as i64)]);
155
156        if let Err(e) = res {
157            return Err(Error::DatabaseError(e.to_string()));
158        }
159
160        self.0.1.remove(format!("atto.upload:{}", id)).await;
161
162        // return
163        Ok(())
164    }
165
166    pub async fn delete_upload_checked(&self, id: usize, user: &User) -> Result<()> {
167        let upload = self.get_upload_by_id(id).await?;
168
169        // check user permission
170        if user.id != upload.owner && !user.permissions.check(FinePermission::MANAGE_UPLOADS) {
171            return Err(Error::NotAllowed);
172        }
173
174        // delete file
175        upload.remove(&self.0.0)?;
176
177        // ...
178        let conn = match self.0.connect().await {
179            Ok(c) => c,
180            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
181        };
182
183        let res = execute!(&conn, "DELETE FROM uploads WHERE id = $1", &[&(id as i64)]);
184
185        if let Err(e) = res {
186            return Err(Error::DatabaseError(e.to_string()));
187        }
188
189        self.0.1.remove(format!("atto.upload:{}", id)).await;
190        Ok(())
191    }
192
193    auto_method!(update_upload_alt(&str)@get_upload_by_id:FinePermission::MANAGE_UPLOADS; -> "UPDATE uploads SET alt = $1 WHERE id = $2" --cache-key-tmpl="atto.upload:{}");
194}