Avoid panics (#392)
- Use `Result` as much as possible - Display errors instead of panicking TODO (maybe in another PR? this one is already quite big): - Find a way to merge Ructe/ErrorPage types, so that we can have routes returning `Result<X, ErrorPage>` instead of panicking when we have an `Error` - Display more details about the error, to make it easier to debug (sorry, this isn't going to be fun to review, the diff is huge, but it is always the same changes)
This commit is contained in:
+24
-27
@@ -5,14 +5,15 @@ use rocket_i18n::I18n;
|
||||
use std::fs;
|
||||
use plume_models::{db_conn::DbConn, medias::*, users::User};
|
||||
use template_utils::Ructe;
|
||||
use routes::errors::ErrorPage;
|
||||
|
||||
#[get("/medias")]
|
||||
pub fn list(user: User, conn: DbConn, intl: I18n) -> Ructe {
|
||||
let medias = Media::for_user(&*conn, user.id);
|
||||
render!(medias::index(
|
||||
pub fn list(user: User, conn: DbConn, intl: I18n) -> Result<Ructe, ErrorPage> {
|
||||
let medias = Media::for_user(&*conn, user.id)?;
|
||||
Ok(render!(medias::index(
|
||||
&(&*conn, &intl.catalog, Some(user)),
|
||||
medias
|
||||
))
|
||||
)))
|
||||
}
|
||||
|
||||
#[get("/medias/new")]
|
||||
@@ -39,69 +40,65 @@ pub fn upload(user: User, data: Data, ct: &ContentType, conn: DbConn) -> Result<
|
||||
let dest = format!("static/media/{}.{}", GUID::rand().to_string(), ext);
|
||||
|
||||
match fields[&"file".to_string()][0].data {
|
||||
SavedData::Bytes(ref bytes) => fs::write(&dest, bytes).expect("media::upload: Couldn't save upload"),
|
||||
SavedData::File(ref path, _) => {fs::copy(path, &dest).expect("media::upload: Couldn't copy upload");},
|
||||
SavedData::Bytes(ref bytes) => fs::write(&dest, bytes).map_err(|_| status::BadRequest(Some("Couldn't save upload")))?,
|
||||
SavedData::File(ref path, _) => {fs::copy(path, &dest).map_err(|_| status::BadRequest(Some("Couldn't copy upload")))?;},
|
||||
_ => {
|
||||
println!("not a file");
|
||||
return Ok(Redirect::to(uri!(new)));
|
||||
}
|
||||
}
|
||||
|
||||
let has_cw = !read(&fields[&"cw".to_string()][0].data).is_empty();
|
||||
let has_cw = !read(&fields[&"cw".to_string()][0].data).map(|cw| cw.is_empty()).unwrap_or(false);
|
||||
let media = Media::insert(&*conn, NewMedia {
|
||||
file_path: dest,
|
||||
alt_text: read(&fields[&"alt".to_string()][0].data),
|
||||
alt_text: read(&fields[&"alt".to_string()][0].data)?,
|
||||
is_remote: false,
|
||||
remote_url: None,
|
||||
sensitive: has_cw,
|
||||
content_warning: if has_cw {
|
||||
Some(read(&fields[&"cw".to_string()][0].data))
|
||||
Some(read(&fields[&"cw".to_string()][0].data)?)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
owner_id: user.id
|
||||
});
|
||||
println!("ok");
|
||||
}).map_err(|_| status::BadRequest(Some("Error while saving media")))?;
|
||||
Ok(Redirect::to(uri!(details: id = media.id)))
|
||||
},
|
||||
SaveResult::Partial(_, _) | SaveResult::Error(_) => {
|
||||
println!("partial err");
|
||||
Ok(Redirect::to(uri!(new)))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("not form data");
|
||||
Ok(Redirect::to(uri!(new)))
|
||||
}
|
||||
}
|
||||
|
||||
fn read(data: &SavedData) -> String {
|
||||
fn read(data: &SavedData) -> Result<String, status::BadRequest<&'static str>> {
|
||||
if let SavedData::Text(s) = data {
|
||||
s.clone()
|
||||
Ok(s.clone())
|
||||
} else {
|
||||
panic!("Field is not a string")
|
||||
Err(status::BadRequest(Some("Error while reading data")))
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/medias/<id>")]
|
||||
pub fn details(id: i32, user: User, conn: DbConn, intl: I18n) -> Ructe {
|
||||
let media = Media::get(&*conn, id).expect("Media::details: media not found");
|
||||
render!(medias::details(
|
||||
pub fn details(id: i32, user: User, conn: DbConn, intl: I18n) -> Result<Ructe, ErrorPage> {
|
||||
let media = Media::get(&*conn, id)?;
|
||||
Ok(render!(medias::details(
|
||||
&(&*conn, &intl.catalog, Some(user)),
|
||||
media
|
||||
))
|
||||
)))
|
||||
}
|
||||
|
||||
#[post("/medias/<id>/delete")]
|
||||
pub fn delete(id: i32, _user: User, conn: DbConn) -> Option<Redirect> {
|
||||
pub fn delete(id: i32, _user: User, conn: DbConn) -> Result<Redirect, ErrorPage> {
|
||||
let media = Media::get(&*conn, id)?;
|
||||
media.delete(&*conn);
|
||||
Some(Redirect::to(uri!(list)))
|
||||
media.delete(&*conn)?;
|
||||
Ok(Redirect::to(uri!(list)))
|
||||
}
|
||||
|
||||
#[post("/medias/<id>/avatar")]
|
||||
pub fn set_avatar(id: i32, user: User, conn: DbConn) -> Option<Redirect> {
|
||||
pub fn set_avatar(id: i32, user: User, conn: DbConn) -> Result<Redirect, ErrorPage> {
|
||||
let media = Media::get(&*conn, id)?;
|
||||
user.set_avatar(&*conn, media.id);
|
||||
Some(Redirect::to(uri!(details: id = id)))
|
||||
user.set_avatar(&*conn, media.id)?;
|
||||
Ok(Redirect::to(uri!(details: id = id)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user