2020-06-11 16:46:00 +00:00
|
|
|
use crate::validate::GifError;
|
2021-04-17 19:59:42 +00:00
|
|
|
use actix_web::{dev::Body, http::StatusCode, BaseHttpResponse, ResponseError};
|
2020-06-06 21:41:17 +00:00
|
|
|
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
2020-06-11 16:46:00 +00:00
|
|
|
pub(crate) enum UploadError {
|
2020-06-06 21:41:17 +00:00
|
|
|
#[error("Couln't upload file, {0}")]
|
|
|
|
Upload(String),
|
|
|
|
|
|
|
|
#[error("Error in DB, {0}")]
|
|
|
|
Db(#[from] sled::Error),
|
|
|
|
|
|
|
|
#[error("Error parsing string, {0}")]
|
|
|
|
ParseString(#[from] std::string::FromUtf8Error),
|
|
|
|
|
2021-01-14 01:08:04 +00:00
|
|
|
#[error("Error parsing request, {0}")]
|
|
|
|
ParseReq(String),
|
|
|
|
|
2020-06-07 00:29:15 +00:00
|
|
|
#[error("Error interacting with filesystem, {0}")]
|
|
|
|
Io(#[from] std::io::Error),
|
|
|
|
|
2021-01-19 01:54:39 +00:00
|
|
|
#[error("Error in filesyste, {0}")]
|
|
|
|
Fs(#[from] actix_fs::Error),
|
|
|
|
|
2020-06-06 21:41:17 +00:00
|
|
|
#[error("Panic in blocking operation")]
|
|
|
|
Canceled,
|
|
|
|
|
|
|
|
#[error("No files present in upload")]
|
|
|
|
NoFiles,
|
|
|
|
|
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
|
|
|
|
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}")]
|
2021-03-10 02:51:03 +00:00
|
|
|
Payload(#[from] awc::error::PayloadError),
|
2020-06-07 15:59:58 +00:00
|
|
|
|
|
|
|
#[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-09 23:39:49 +00:00
|
|
|
|
|
|
|
#[error("Error converting Path to String")]
|
|
|
|
Path,
|
2020-06-11 00:26:54 +00:00
|
|
|
|
|
|
|
#[error("Tried to save an image with an already-taken name")]
|
|
|
|
DuplicateAlias,
|
2020-06-11 16:46:00 +00:00
|
|
|
|
|
|
|
#[error("Error validating Gif file, {0}")]
|
|
|
|
Gif(#[from] GifError),
|
2020-06-15 02:41:45 +00:00
|
|
|
|
2020-06-24 16:58:46 +00:00
|
|
|
#[error("Error transcoding, {0}")]
|
|
|
|
Transcode(crate::validate::transcode::Error),
|
|
|
|
|
2020-06-15 02:41:45 +00:00
|
|
|
#[error("Tried to create file, but file already exists")]
|
|
|
|
FileExists,
|
|
|
|
|
|
|
|
#[error("File metadata could not be parsed, {0}")]
|
|
|
|
Validate(#[from] rexiv2::Rexiv2Error),
|
2020-06-16 20:55:24 +00:00
|
|
|
|
|
|
|
#[error("Error in MagickWand, {0}")]
|
|
|
|
Wand(String),
|
2020-12-10 05:05:04 +00:00
|
|
|
|
|
|
|
#[error("{0}")]
|
|
|
|
Json(#[from] serde_json::Error),
|
2021-01-14 13:52:11 +00:00
|
|
|
|
|
|
|
#[error("Range header not satisfiable")]
|
|
|
|
Range,
|
2020-06-07 15:59:58 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 02:51:03 +00:00
|
|
|
impl From<awc::error::SendRequestError> for UploadError {
|
|
|
|
fn from(e: awc::error::SendRequestError) -> Self {
|
2020-06-07 15:59:58 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
impl From<actix_web::error::BlockingError> for UploadError {
|
|
|
|
fn from(_: actix_web::error::BlockingError) -> Self {
|
|
|
|
UploadError::Canceled
|
2020-06-06 21:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResponseError for UploadError {
|
|
|
|
fn status_code(&self) -> StatusCode {
|
|
|
|
match self {
|
2020-06-11 16:46:00 +00:00
|
|
|
UploadError::Gif(_)
|
|
|
|
| UploadError::DuplicateAlias
|
2020-06-11 00:26:54 +00:00
|
|
|
| UploadError::NoFiles
|
2021-01-14 01:08:04 +00:00
|
|
|
| UploadError::Upload(_)
|
|
|
|
| UploadError::ParseReq(_) => 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,
|
2021-01-14 13:52:11 +00:00
|
|
|
UploadError::Range => StatusCode::RANGE_NOT_SATISFIABLE,
|
2020-06-06 21:41:17 +00:00
|
|
|
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-17 19:59:42 +00:00
|
|
|
fn error_response(&self) -> BaseHttpResponse<Body> {
|
|
|
|
BaseHttpResponse::build(self.status_code())
|
|
|
|
.content_type("application/json")
|
|
|
|
.body(
|
|
|
|
serde_json::to_string(&serde_json::json!({ "msg": self.to_string() }))
|
|
|
|
.unwrap_or(r#"{"msg":"Internal Server Error"}"#.to_string()),
|
|
|
|
)
|
2020-06-06 21:41:17 +00:00
|
|
|
}
|
|
|
|
}
|