2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-11-10 06:25:00 +00:00

Re-enable skipping validation on import, strip when converting to prescribed format

This commit is contained in:
Aode (Lion) 2021-09-26 11:02:19 -05:00
parent 5bc58f7ce6
commit 23e6657208
3 changed files with 47 additions and 0 deletions

View file

@ -54,6 +54,7 @@ pub(crate) fn convert_bytes_read(
&[ &[
"convert", "convert",
"-", "-",
"-strip",
format!("{}:-", format.to_magick_format()).as_str(), format!("{}:-", format.to_magick_format()).as_str(),
], ],
)?; )?;

View file

@ -703,6 +703,7 @@ impl UploadManagerSession {
let (content_type, validated_reader) = crate::validate::validate_image_bytes( let (content_type, validated_reader) = crate::validate::validate_image_bytes(
bytes_mut.freeze(), bytes_mut.freeze(),
self.manager.inner.format.clone(), self.manager.inner.format.clone(),
validate,
) )
.await?; .await?;
@ -741,6 +742,7 @@ impl UploadManagerSession {
let (content_type, validated_reader) = crate::validate::validate_image_bytes( let (content_type, validated_reader) = crate::validate::validate_image_bytes(
bytes_mut.freeze(), bytes_mut.freeze(),
self.manager.inner.format.clone(), self.manager.inner.format.clone(),
true,
) )
.await?; .await?;

View file

@ -11,13 +11,57 @@ pub(crate) fn video_mp4() -> mime::Mime {
"video/mp4".parse().unwrap() "video/mp4".parse().unwrap()
} }
struct UnvalidatedBytes {
bytes: Bytes,
written: usize,
}
impl UnvalidatedBytes {
fn new(bytes: Bytes) -> Self {
UnvalidatedBytes { bytes, written: 0 }
}
fn boxed(self) -> Box<dyn AsyncRead + Unpin> {
Box::new(self)
}
}
impl AsyncRead for UnvalidatedBytes {
fn poll_read(
mut self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> std::task::Poll<std::io::Result<()>> {
let bytes_to_write = (self.bytes.len() - self.written).min(buf.remaining());
if bytes_to_write > 0 {
let end = self.written + bytes_to_write;
buf.put_slice(&self.bytes[self.written..end]);
self.written = end;
}
std::task::Poll::Ready(Ok(()))
}
}
#[instrument(name = "Validate image", skip(bytes))] #[instrument(name = "Validate image", skip(bytes))]
pub(crate) async fn validate_image_bytes( pub(crate) async fn validate_image_bytes(
bytes: Bytes, bytes: Bytes,
prescribed_format: Option<Format>, prescribed_format: Option<Format>,
validate: bool,
) -> Result<(mime::Mime, Box<dyn AsyncRead + Unpin>), Error> { ) -> Result<(mime::Mime, Box<dyn 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?;
if !validate {
let mime_type = match input_type {
ValidInputType::Gif => video_mp4(),
ValidInputType::Mp4 => mime::IMAGE_GIF,
ValidInputType::Jpeg => mime::IMAGE_JPEG,
ValidInputType::Png => mime::IMAGE_PNG,
ValidInputType::Webp => image_webp(),
};
return Ok((mime_type, UnvalidatedBytes::new(bytes).boxed()));
}
match (prescribed_format, input_type) { match (prescribed_format, input_type) {
(_, ValidInputType::Gif) => Ok(( (_, ValidInputType::Gif) => Ok((
video_mp4(), video_mp4(),