2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-12-22 19:31:35 +00:00

Replace UnsupportedFormat error with more helpful errors

This commit is contained in:
asonix 2023-07-09 15:07:49 -05:00
parent 74f4423d7b
commit 7dadbcf3ed
4 changed files with 17 additions and 9 deletions

View file

@ -90,8 +90,8 @@ pub(crate) enum UploadError {
#[error("Provided token did not match expected token")] #[error("Provided token did not match expected token")]
InvalidToken, InvalidToken,
#[error("Unsupported image format")] #[error("Process endpoint was called with invalid extension")]
UnsupportedFormat, UnsupportedProcessExtension,
#[error("Gif uploads are not enabled")] #[error("Gif uploads are not enabled")]
SilentVideoDisabled, SilentVideoDisabled,
@ -168,11 +168,11 @@ impl ResponseError for Error {
| UploadError::Limit(_) | UploadError::Limit(_)
| UploadError::NoFiles | UploadError::NoFiles
| UploadError::Upload(_) | UploadError::Upload(_)
| UploadError::UnsupportedFormat
| UploadError::Store(crate::store::StoreError::Repo( | UploadError::Store(crate::store::StoreError::Repo(
crate::repo::RepoError::AlreadyClaimed, crate::repo::RepoError::AlreadyClaimed,
)) ))
| UploadError::Repo(crate::repo::RepoError::AlreadyClaimed) | UploadError::Repo(crate::repo::RepoError::AlreadyClaimed)
| UploadError::UnsupportedProcessExtension
| UploadError::SilentVideoDisabled, | UploadError::SilentVideoDisabled,
) => StatusCode::BAD_REQUEST, ) => StatusCode::BAD_REQUEST,
Some(UploadError::MissingAlias) => StatusCode::NOT_FOUND, Some(UploadError::MissingAlias) => StatusCode::NOT_FOUND,

View file

@ -4,7 +4,7 @@ mod tests;
use crate::{ use crate::{
config::{AudioCodec, ImageFormat, MediaConfiguration, VideoCodec}, config::{AudioCodec, ImageFormat, MediaConfiguration, VideoCodec},
error::{Error, UploadError}, error::{Error, UploadError},
magick::{Details, ValidInputType}, magick::{Details, ParseDetailsError, ValidInputType},
process::Process, process::Process,
store::{Store, StoreError}, store::{Store, StoreError},
}; };
@ -498,7 +498,8 @@ fn parse_details(output: DetailsOutput) -> Result<Option<Details>, Error> {
stream.height, stream.height,
stream.nb_read_frames.as_deref(), stream.nb_read_frames.as_deref(),
*v, *v,
); )
.map_err(Error::from);
} }
} }
@ -510,9 +511,13 @@ fn parse_details_inner(
height: usize, height: usize,
frames: Option<&str>, frames: Option<&str>,
format: VideoFormat, format: VideoFormat,
) -> Result<Option<Details>, Error> { ) -> Result<Option<Details>, ParseDetailsError> {
let frames = frames let frames = frames
.map(|frames| frames.parse().map_err(|_| UploadError::UnsupportedFormat)) .map(|frames| {
frames
.parse()
.map_err(|_| ParseDetailsError::ParseFrames(String::from(frames)))
})
.transpose()? .transpose()?
.unwrap_or(1); .unwrap_or(1);

View file

@ -562,7 +562,7 @@ fn prepare_process(
let format = ext let format = ext
.parse::<ImageFormat>() .parse::<ImageFormat>()
.map_err(|_| UploadError::UnsupportedFormat)?; .map_err(|_| UploadError::UnsupportedProcessExtension)?;
let ext = format.to_string(); let ext = format.to_string();

View file

@ -272,6 +272,9 @@ pub(crate) enum ParseDetailsError {
#[error("Format is unsupported: {0}")] #[error("Format is unsupported: {0}")]
Unsupported(String), Unsupported(String),
#[error("Could not parse frame count from {0}")]
ParseFrames(String),
} }
fn parse_details(details_output: Vec<DetailsOutput>) -> Result<Details, Error> { fn parse_details(details_output: Vec<DetailsOutput>) -> Result<Details, Error> {
@ -297,7 +300,7 @@ fn parse_details(details_output: Vec<DetailsOutput>) -> Result<Details, Error> {
if !details_output if !details_output
.iter() .iter()
.all(|details| &details.image.format == format) .all(|details| details.image.format == format)
{ {
return Err(ParseDetailsError::MixedFormats.into()); return Err(ParseDetailsError::MixedFormats.into());
} }