add: ability to manage uploads

This commit is contained in:
trisua 2025-05-11 15:20:15 -04:00
parent 6fabb38c10
commit eb95be0f38
11 changed files with 234 additions and 48 deletions

View file

@ -1,5 +1,7 @@
use super::*;
use crate::cache::Cache;
use crate::model::auth::User;
use crate::model::permissions::FinePermission;
use crate::model::{Error, Result, uploads::MediaUpload};
use crate::{auto_method, execute, get, query_row, query_rows, params};
@ -50,6 +52,37 @@ impl DataManager {
Ok(res.unwrap())
}
/// Get all uploads by their owner (paginated).
///
/// # Arguments
/// * `owner` - the ID of the owner of the upload
/// * `batch` - the limit of items in each page
/// * `page` - the page number
pub async fn get_uploads_by_owner(
&self,
owner: usize,
batch: usize,
page: usize,
) -> Result<Vec<MediaUpload>> {
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_rows!(
&conn,
"SELECT * FROM uploads WHERE owner = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
&[&(owner as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_upload_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("upload".to_string()));
}
Ok(res.unwrap())
}
/// Create a new upload in the database.
///
/// # Arguments
@ -109,4 +142,31 @@ impl DataManager {
// return
Ok(())
}
pub async fn delete_upload_checked(&self, id: usize, user: &User) -> Result<()> {
let upload = self.get_upload_by_id(id).await?;
// check user permission
if user.id != upload.owner && !user.permissions.check(FinePermission::MANAGE_UPLOADS) {
return Err(Error::NotAllowed);
}
// delete file
upload.remove(&self.0)?;
// ...
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(&conn, "DELETE FROM uploads WHERE id = $1", &[&(id as i64)]);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
self.2.remove(format!("atto.upload:{}", id)).await;
Ok(())
}
}