Media deletion

This commit is contained in:
Bat 2018-09-02 22:10:15 +01:00
parent f44b6fffa3
commit 99fdb8e680
4 changed files with 16 additions and 2 deletions

View File

@ -1,11 +1,12 @@
use diesel::{self, PgConnection, QueryDsl, ExpressionMethods, RunQueryDsl};
use serde_json;
use std::fs;
use ap_url;
use instance::Instance;
use schema::medias;
#[derive(Queryable, Serialize)]
#[derive(Identifiable, Queryable, Serialize)]
pub struct Media {
pub id: i32,
pub file_path: String,
@ -59,4 +60,9 @@ impl Media {
pub fn url(&self, conn: &PgConnection) -> String {
ap_url(format!("{}/static/{}", Instance::get_local(conn).unwrap().public_domain, self.file_path))
}
pub fn delete(&self, conn: &PgConnection) {
fs::remove_file(self.file_path.as_str()).expect("Couldn't delete media from disk");
diesel::delete(self).execute(conn).expect("Couldn't remove media from DB");
}
}

View File

@ -67,6 +67,7 @@ fn main() {
routes::medias::new,
routes::medias::upload,
routes::medias::details,
routes::medias::delete,
routes::medias::static_files,
routes::notifications::paginated_notifications,

View File

@ -97,6 +97,13 @@ fn details(id: i32, user: User, conn: DbConn) -> Template {
}))
}
#[get("/medias/<id>/delete")]
fn delete(id: i32, _user: User, conn: DbConn) -> Redirect {
let media = Media::get(&*conn, id).expect("Media to delete not found");
media.delete(&*conn);
Redirect::to(uri!(list))
}
#[get("/static/media/<file..>", rank = 1)]
fn static_files(file: PathBuf) -> Option<NamedFile> {
NamedFile::open(Path::new("media/").join(file)).ok()

View File

@ -720,7 +720,7 @@ figure {
}
figure > * {
width: 100%;
max-width: 100%;
}
figcaption {