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

Allow disabling gif/mp4

This commit is contained in:
Aode (lion) 2022-03-29 11:04:56 -05:00
parent dd9ef89709
commit 99d042f36e
4 changed files with 47 additions and 16 deletions

View file

@ -90,6 +90,9 @@ pub(crate) enum UploadError {
#[error("Unsupported image format")] #[error("Unsupported image format")]
UnsupportedFormat, UnsupportedFormat,
#[error("Gif uploads are not enabled")]
SilentVideoDisabled,
#[error("Invalid media dimensions")] #[error("Invalid media dimensions")]
Dimensions, Dimensions,
@ -143,7 +146,9 @@ impl ResponseError for Error {
UploadError::DuplicateAlias UploadError::DuplicateAlias
| UploadError::Limit(_) | UploadError::Limit(_)
| UploadError::NoFiles | UploadError::NoFiles
| UploadError::Upload(_), | UploadError::Upload(_)
| UploadError::UnsupportedFormat
| UploadError::SilentVideoDisabled,
) => StatusCode::BAD_REQUEST, ) => StatusCode::BAD_REQUEST,
Some( Some(
UploadError::Sled(crate::repo::sled::SledError::Missing) UploadError::Sled(crate::repo::sled::SledError::Missing)

View file

@ -212,7 +212,10 @@ async fn download<S: Store>(
futures_util::pin_mut!(stream); futures_util::pin_mut!(stream);
let permit = PROCESS_SEMAPHORE.acquire().await?; let permit = PROCESS_SEMAPHORE.acquire().await?;
let session = manager.session((**store).clone()).upload(stream).await?; let session = manager
.session((**store).clone())
.upload(CONFIG.media.enable_silent_video, stream)
.await?;
let alias = session.alias().unwrap().to_owned(); let alias = session.alias().unwrap().to_owned();
drop(permit); drop(permit);
let delete_token = session.delete_token().await?; let delete_token = session.delete_token().await?;
@ -658,7 +661,10 @@ async fn launch<S: Store + Clone + 'static>(
let res = manager let res = manager
.session(store) .session(store)
.upload(map_error::map_crate_error(stream)) .upload(
CONFIG.media.enable_silent_video,
map_error::map_crate_error(stream),
)
.await; .await;
drop(permit); drop(permit);
@ -694,6 +700,7 @@ async fn launch<S: Store + Clone + 'static>(
.import( .import(
filename, filename,
validate_imports, validate_imports,
CONFIG.media.enable_silent_video,
map_error::map_crate_error(stream), map_error::map_crate_error(stream),
) )
.await; .await;

View file

@ -113,6 +113,7 @@ impl<S: Store> UploadManagerSession<S> {
mut self, mut self,
alias: String, alias: String,
validate: bool, validate: bool,
enable_silent_video: bool,
mut stream: impl Stream<Item = Result<web::Bytes, Error>> + Unpin, mut stream: impl Stream<Item = Result<web::Bytes, Error>> + Unpin,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
let mut bytes_mut = actix_web::web::BytesMut::new(); let mut bytes_mut = actix_web::web::BytesMut::new();
@ -127,6 +128,7 @@ impl<S: Store> UploadManagerSession<S> {
let (_, validated_reader) = crate::validate::validate_image_bytes( let (_, validated_reader) = crate::validate::validate_image_bytes(
bytes_mut.freeze(), bytes_mut.freeze(),
self.manager.inner.format, self.manager.inner.format,
enable_silent_video,
validate, validate,
) )
.await?; .await?;
@ -150,6 +152,7 @@ impl<S: Store> UploadManagerSession<S> {
#[instrument(skip(self, stream))] #[instrument(skip(self, stream))]
pub(crate) async fn upload( pub(crate) async fn upload(
mut self, mut self,
enable_silent_video: bool,
mut stream: impl Stream<Item = Result<web::Bytes, Error>> + Unpin, mut stream: impl Stream<Item = Result<web::Bytes, Error>> + Unpin,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
let mut bytes_mut = actix_web::web::BytesMut::new(); let mut bytes_mut = actix_web::web::BytesMut::new();
@ -164,6 +167,7 @@ impl<S: Store> UploadManagerSession<S> {
let (input_type, validated_reader) = crate::validate::validate_image_bytes( let (input_type, validated_reader) = crate::validate::validate_image_bytes(
bytes_mut.freeze(), bytes_mut.freeze(),
self.manager.inner.format, self.manager.inner.format,
enable_silent_video,
true, true,
) )
.await?; .await?;

View file

@ -1,5 +1,9 @@
use crate::{ use crate::{
config::ImageFormat, either::Either, error::Error, ffmpeg::InputFormat, magick::ValidInputType, config::ImageFormat,
either::Either,
error::{Error, UploadError},
ffmpeg::InputFormat,
magick::ValidInputType,
}; };
use actix_web::web::Bytes; use actix_web::web::Bytes;
use tokio::io::AsyncRead; use tokio::io::AsyncRead;
@ -36,6 +40,7 @@ impl AsyncRead for UnvalidatedBytes {
pub(crate) async fn validate_image_bytes( pub(crate) async fn validate_image_bytes(
bytes: Bytes, bytes: Bytes,
prescribed_format: Option<ImageFormat>, prescribed_format: Option<ImageFormat>,
enable_silent_video: bool,
validate: bool, validate: bool,
) -> Result<(ValidInputType, impl AsyncRead + Unpin), Error> { ) -> Result<(ValidInputType, impl AsyncRead + Unpin), Error> {
let input_type = crate::magick::input_type_bytes(bytes.clone()).await?; let input_type = crate::magick::input_type_bytes(bytes.clone()).await?;
@ -45,18 +50,28 @@ pub(crate) async fn validate_image_bytes(
} }
match (prescribed_format, input_type) { match (prescribed_format, input_type) {
(_, ValidInputType::Gif) => Ok(( (_, ValidInputType::Gif) => {
if !enable_silent_video {
return Err(UploadError::SilentVideoDisabled.into());
}
Ok((
ValidInputType::Mp4, ValidInputType::Mp4,
Either::right(Either::left( Either::right(Either::left(
crate::ffmpeg::to_mp4_bytes(bytes, InputFormat::Gif).await?, crate::ffmpeg::to_mp4_bytes(bytes, InputFormat::Gif).await?,
)), )),
)), ))
(_, ValidInputType::Mp4) => Ok(( }
(_, ValidInputType::Mp4) => {
if !enable_silent_video {
return Err(UploadError::SilentVideoDisabled.into());
}
Ok((
ValidInputType::Mp4, ValidInputType::Mp4,
Either::right(Either::left( Either::right(Either::left(
crate::ffmpeg::to_mp4_bytes(bytes, InputFormat::Mp4).await?, crate::ffmpeg::to_mp4_bytes(bytes, InputFormat::Mp4).await?,
)), )),
)), ))
}
(Some(ImageFormat::Jpeg) | None, ValidInputType::Jpeg) => Ok(( (Some(ImageFormat::Jpeg) | None, ValidInputType::Jpeg) => Ok((
ValidInputType::Jpeg, ValidInputType::Jpeg,
Either::right(Either::right(Either::left( Either::right(Either::right(Either::left(