Media deletion
This commit is contained in:
parent
f44b6fffa3
commit
99fdb8e680
@ -1,11 +1,12 @@
|
|||||||
use diesel::{self, PgConnection, QueryDsl, ExpressionMethods, RunQueryDsl};
|
use diesel::{self, PgConnection, QueryDsl, ExpressionMethods, RunQueryDsl};
|
||||||
use serde_json;
|
use serde_json;
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
use ap_url;
|
use ap_url;
|
||||||
use instance::Instance;
|
use instance::Instance;
|
||||||
use schema::medias;
|
use schema::medias;
|
||||||
|
|
||||||
#[derive(Queryable, Serialize)]
|
#[derive(Identifiable, Queryable, Serialize)]
|
||||||
pub struct Media {
|
pub struct Media {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub file_path: String,
|
pub file_path: String,
|
||||||
@ -59,4 +60,9 @@ impl Media {
|
|||||||
pub fn url(&self, conn: &PgConnection) -> String {
|
pub fn url(&self, conn: &PgConnection) -> String {
|
||||||
ap_url(format!("{}/static/{}", Instance::get_local(conn).unwrap().public_domain, self.file_path))
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,6 +67,7 @@ fn main() {
|
|||||||
routes::medias::new,
|
routes::medias::new,
|
||||||
routes::medias::upload,
|
routes::medias::upload,
|
||||||
routes::medias::details,
|
routes::medias::details,
|
||||||
|
routes::medias::delete,
|
||||||
routes::medias::static_files,
|
routes::medias::static_files,
|
||||||
|
|
||||||
routes::notifications::paginated_notifications,
|
routes::notifications::paginated_notifications,
|
||||||
|
@ -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)]
|
#[get("/static/media/<file..>", rank = 1)]
|
||||||
fn static_files(file: PathBuf) -> Option<NamedFile> {
|
fn static_files(file: PathBuf) -> Option<NamedFile> {
|
||||||
NamedFile::open(Path::new("media/").join(file)).ok()
|
NamedFile::open(Path::new("media/").join(file)).ok()
|
||||||
|
@ -720,7 +720,7 @@ figure {
|
|||||||
}
|
}
|
||||||
|
|
||||||
figure > * {
|
figure > * {
|
||||||
width: 100%;
|
max-width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
figcaption {
|
figcaption {
|
||||||
|
Loading…
Reference in New Issue
Block a user