2020-06-06 21:41:17 +00:00
|
|
|
use actix_web::{http::StatusCode, HttpResponse, ResponseError};
|
|
|
|
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum UploadError {
|
|
|
|
#[error("Invalid content type provided, {0}")]
|
|
|
|
ContentType(mime::Mime),
|
|
|
|
|
|
|
|
#[error("Couln't upload file, {0}")]
|
|
|
|
Upload(String),
|
|
|
|
|
|
|
|
#[error("Couldn't save file, {0}")]
|
|
|
|
Save(#[from] actix_fs::Error),
|
|
|
|
|
|
|
|
#[error("Error in DB, {0}")]
|
|
|
|
Db(#[from] sled::Error),
|
|
|
|
|
|
|
|
#[error("Error parsing string, {0}")]
|
|
|
|
ParseString(#[from] std::string::FromUtf8Error),
|
|
|
|
|
|
|
|
#[error("Error processing image, {0}")]
|
|
|
|
Image(#[from] image::error::ImageError),
|
|
|
|
|
2020-06-07 00:29:15 +00:00
|
|
|
#[error("Error interacting with filesystem, {0}")]
|
|
|
|
Io(#[from] std::io::Error),
|
|
|
|
|
2020-06-06 21:41:17 +00:00
|
|
|
#[error("Panic in blocking operation")]
|
|
|
|
Canceled,
|
|
|
|
|
|
|
|
#[error("No files present in upload")]
|
|
|
|
NoFiles,
|
|
|
|
|
|
|
|
#[error("Uploaded image could not be served, extension is missing")]
|
|
|
|
MissingExtension,
|
2020-06-06 22:43:33 +00:00
|
|
|
|
|
|
|
#[error("Requested a file that doesn't exist")]
|
|
|
|
MissingAlias,
|
|
|
|
|
|
|
|
#[error("Alias directed to missing file")]
|
|
|
|
MissingFile,
|
2020-06-07 00:29:15 +00:00
|
|
|
|
|
|
|
#[error("Provided token did not match expected token")]
|
|
|
|
InvalidToken,
|
2020-06-07 01:44:26 +00:00
|
|
|
|
|
|
|
#[error("Uploaded content could not be validated as an image")]
|
|
|
|
InvalidImage(image::error::ImageError),
|
2020-06-07 15:59:58 +00:00
|
|
|
|
|
|
|
#[error("Unsupported image format")]
|
|
|
|
UnsupportedFormat,
|
|
|
|
|
|
|
|
#[error("Unable to download image, bad response {0}")]
|
|
|
|
Download(actix_web::http::StatusCode),
|
|
|
|
|
|
|
|
#[error("Unable to download image, {0}")]
|
|
|
|
Payload(#[from] actix_web::client::PayloadError),
|
|
|
|
|
|
|
|
#[error("Unable to send request, {0}")]
|
|
|
|
SendRequest(String),
|
2020-06-07 17:51:45 +00:00
|
|
|
|
|
|
|
#[error("No filename provided in request")]
|
|
|
|
MissingFilename,
|
2020-06-07 15:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<actix_web::client::SendRequestError> for UploadError {
|
|
|
|
fn from(e: actix_web::client::SendRequestError) -> Self {
|
|
|
|
UploadError::SendRequest(e.to_string())
|
|
|
|
}
|
2020-06-07 00:29:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<sled::transaction::TransactionError<UploadError>> for UploadError {
|
|
|
|
fn from(e: sled::transaction::TransactionError<UploadError>) -> Self {
|
|
|
|
match e {
|
|
|
|
sled::transaction::TransactionError::Abort(t) => t,
|
|
|
|
sled::transaction::TransactionError::Storage(e) => e.into(),
|
|
|
|
}
|
|
|
|
}
|
2020-06-06 21:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<actix_form_data::Error> for UploadError {
|
|
|
|
fn from(e: actix_form_data::Error) -> Self {
|
|
|
|
UploadError::Upload(e.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> From<actix_web::error::BlockingError<T>> for UploadError
|
|
|
|
where
|
|
|
|
T: Into<UploadError> + std::fmt::Debug,
|
|
|
|
{
|
|
|
|
fn from(e: actix_web::error::BlockingError<T>) -> Self {
|
|
|
|
match e {
|
|
|
|
actix_web::error::BlockingError::Error(e) => e.into(),
|
|
|
|
_ => UploadError::Canceled,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResponseError for UploadError {
|
|
|
|
fn status_code(&self) -> StatusCode {
|
|
|
|
match self {
|
|
|
|
UploadError::NoFiles | UploadError::ContentType(_) | UploadError::Upload(_) => {
|
|
|
|
StatusCode::BAD_REQUEST
|
|
|
|
}
|
2020-06-07 17:51:45 +00:00
|
|
|
UploadError::MissingAlias | UploadError::MissingFilename => StatusCode::NOT_FOUND,
|
2020-06-07 00:29:15 +00:00
|
|
|
UploadError::InvalidToken => StatusCode::FORBIDDEN,
|
2020-06-06 21:41:17 +00:00
|
|
|
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn error_response(&self) -> HttpResponse {
|
|
|
|
HttpResponse::build(self.status_code()).json(serde_json::json!({ "msg": self.to_string() }))
|
|
|
|
}
|
|
|
|
}
|