2025-05-05 19:38:01 -04:00
use super ::* ;
use crate ::cache ::Cache ;
use crate ::model ::{ Error , Result , uploads ::MediaUpload } ;
use crate ::{ auto_method , execute , get , query_row , query_rows , params } ;
#[ cfg(feature = " sqlite " ) ]
use rusqlite ::Row ;
#[ cfg(feature = " postgres " ) ]
use tokio_postgres ::Row ;
impl DataManager {
/// Get a [`MediaUpload`] from an SQL row.
pub ( crate ) fn get_upload_from_row (
#[ cfg(feature = " sqlite " ) ] x : & Row < '_ > ,
#[ cfg(feature = " postgres " ) ] x : & Row ,
) -> MediaUpload {
MediaUpload {
id : get ! ( x ->0 ( i64 ) ) as usize ,
created : get ! ( x ->1 ( i64 ) ) as usize ,
owner : get ! ( x ->2 ( i64 ) ) as usize ,
what : serde_json ::from_str ( & get! ( x ->3 ( String ) ) ) . unwrap ( ) ,
}
}
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.uploads:{} " ) ;
/// Get all uploads (paginated).
///
/// # Arguments
/// * `batch` - the limit of items in each page
/// * `page` - the page number
pub async fn get_uploads ( & self , 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 ORDER BY created DESC LIMIT $1 OFFSET $2 " ,
& [ & ( 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
/// * `data` - a mock [`MediaUpload`] object to insert
2025-05-10 21:58:02 -04:00
pub async fn create_upload ( & self , data : MediaUpload ) -> Result < MediaUpload > {
2025-05-05 19:38:01 -04:00
let conn = match self . connect ( ) . await {
Ok ( c ) = > c ,
Err ( e ) = > return Err ( Error ::DatabaseConnection ( e . to_string ( ) ) ) ,
} ;
let res = execute! (
& conn ,
" INSERT INTO uploads VALUES ($1, $2, $3, $4) " ,
params! [
& ( data . id as i64 ) ,
& ( data . created as i64 ) ,
& ( data . owner as i64 ) ,
& serde_json ::to_string ( & data . what ) . unwrap ( ) . as_str ( ) ,
]
) ;
if let Err ( e ) = res {
return Err ( Error ::DatabaseError ( e . to_string ( ) ) ) ;
}
// return
2025-05-10 21:58:02 -04:00
Ok ( data )
2025-05-05 19:38:01 -04:00
}
pub async fn delete_upload ( & self , id : usize ) -> Result < ( ) > {
// if !user.permissions.check(FinePermission::MANAGE_UPLOADS) {
// return Err(Error::NotAllowed);
// }
// delete file
// it's most important that the file gets off the file system first, even
// if there's an issue in the database
//
// the actual file takes up much more space than the database entry.
2025-05-10 21:58:02 -04:00
let upload = self . get_upload_by_id ( id ) . await ? ;
upload . remove ( & self . 0 ) ? ;
2025-05-05 19:38:01 -04:00
// delete from database
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 ;
// return
Ok ( ( ) )
}
}