2021-10-23 19:14:12 +00:00
|
|
|
use crate::{
|
|
|
|
error::{Error, UploadError},
|
2022-09-26 01:39:09 +00:00
|
|
|
magick::{Details, ValidInputType},
|
2021-10-23 19:14:12 +00:00
|
|
|
process::Process,
|
|
|
|
store::Store,
|
|
|
|
};
|
2021-09-04 00:53:53 +00:00
|
|
|
use actix_web::web::Bytes;
|
2022-09-26 00:34:51 +00:00
|
|
|
use tokio::io::{AsyncRead, AsyncReadExt};
|
2021-09-14 01:22:42 +00:00
|
|
|
use tracing::instrument;
|
2021-08-26 02:46:11 +00:00
|
|
|
|
2022-09-26 00:34:51 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2021-08-31 02:19:47 +00:00
|
|
|
pub(crate) enum InputFormat {
|
|
|
|
Gif,
|
|
|
|
Mp4,
|
2022-09-26 00:34:51 +00:00
|
|
|
Webm,
|
2021-08-31 02:19:47 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 00:34:51 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2021-08-28 22:15:14 +00:00
|
|
|
pub(crate) enum ThumbnailFormat {
|
|
|
|
Jpeg,
|
2021-08-31 02:19:47 +00:00
|
|
|
// Webp,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InputFormat {
|
2022-09-26 00:34:51 +00:00
|
|
|
fn to_ext(self) -> &'static str {
|
2021-08-31 02:19:47 +00:00
|
|
|
match self {
|
2021-10-23 19:14:12 +00:00
|
|
|
InputFormat::Gif => ".gif",
|
|
|
|
InputFormat::Mp4 => ".mp4",
|
2022-09-26 00:34:51 +00:00
|
|
|
InputFormat::Webm => ".webm",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 01:39:09 +00:00
|
|
|
fn to_mime(self) -> mime::Mime {
|
2022-09-26 00:34:51 +00:00
|
|
|
match self {
|
2022-09-26 01:39:09 +00:00
|
|
|
Self::Gif => mime::IMAGE_GIF,
|
|
|
|
Self::Mp4 => crate::magick::video_mp4(),
|
|
|
|
Self::Webm => crate::magick::video_webm(),
|
2021-08-31 02:19:47 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-28 22:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ThumbnailFormat {
|
2022-09-26 00:34:51 +00:00
|
|
|
fn as_codec(self) -> &'static str {
|
2021-08-28 22:15:14 +00:00
|
|
|
match self {
|
|
|
|
ThumbnailFormat::Jpeg => "mjpeg",
|
2021-08-31 02:19:47 +00:00
|
|
|
// ThumbnailFormat::Webp => "webp",
|
2021-08-28 22:15:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 00:34:51 +00:00
|
|
|
fn to_ext(self) -> &'static str {
|
2021-10-23 19:14:12 +00:00
|
|
|
match self {
|
|
|
|
ThumbnailFormat::Jpeg => ".jpeg",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 00:34:51 +00:00
|
|
|
fn as_format(self) -> &'static str {
|
2021-08-28 22:15:14 +00:00
|
|
|
match self {
|
2022-03-27 01:45:12 +00:00
|
|
|
ThumbnailFormat::Jpeg => "image2",
|
2021-08-31 02:19:47 +00:00
|
|
|
// ThumbnailFormat::Webp => "webp",
|
2021-08-28 22:15:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 00:34:51 +00:00
|
|
|
const FORMAT_MAPPINGS: &[(&str, InputFormat)] = &[
|
|
|
|
("gif", InputFormat::Gif),
|
|
|
|
("mp4", InputFormat::Mp4),
|
|
|
|
("webm", InputFormat::Webm),
|
|
|
|
];
|
|
|
|
|
2022-09-26 01:39:09 +00:00
|
|
|
pub(crate) async fn input_type_bytes(input: Bytes) -> Result<Option<ValidInputType>, Error> {
|
|
|
|
if let Some(details) = details_bytes(input).await? {
|
|
|
|
return Ok(Some(details.validate_input()?));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn details_store<S: Store>(
|
|
|
|
store: &S,
|
|
|
|
identifier: &S::Identifier,
|
|
|
|
) -> Result<Option<Details>, Error> {
|
|
|
|
let input_file = crate::tmp_file::tmp_file(None);
|
|
|
|
let input_file_str = input_file.to_str().ok_or(UploadError::Path)?;
|
|
|
|
crate::store::file_store::safe_create_parent(&input_file).await?;
|
|
|
|
|
|
|
|
let mut tmp_one = crate::file::File::create(&input_file).await?;
|
|
|
|
tmp_one
|
|
|
|
.write_from_stream(store.to_stream(&identifier, None, None).await?)
|
|
|
|
.await?;
|
|
|
|
tmp_one.close().await?;
|
|
|
|
|
|
|
|
let process = Process::run(
|
|
|
|
"ffprobe",
|
|
|
|
&[
|
|
|
|
"-v",
|
|
|
|
"quiet",
|
|
|
|
"-select_streams",
|
|
|
|
"v:0",
|
|
|
|
"-count_frames",
|
|
|
|
"-show_entries",
|
|
|
|
"stream=width,height,nb_read_frames:format=format_name",
|
|
|
|
"-of",
|
|
|
|
"default=noprint_wrappers=1:nokey=1",
|
|
|
|
input_file_str,
|
|
|
|
],
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut output = Vec::new();
|
|
|
|
process.read().read_to_end(&mut output).await?;
|
|
|
|
let output = String::from_utf8_lossy(&output);
|
|
|
|
tokio::fs::remove_file(input_file_str).await?;
|
|
|
|
|
|
|
|
parse_details(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn details_bytes(input: Bytes) -> Result<Option<Details>, Error> {
|
2022-09-26 00:34:51 +00:00
|
|
|
let input_file = crate::tmp_file::tmp_file(None);
|
|
|
|
let input_file_str = input_file.to_str().ok_or(UploadError::Path)?;
|
|
|
|
crate::store::file_store::safe_create_parent(&input_file).await?;
|
|
|
|
|
|
|
|
let mut tmp_one = crate::file::File::create(&input_file).await?;
|
|
|
|
tmp_one.write_from_bytes(input).await?;
|
|
|
|
tmp_one.close().await?;
|
|
|
|
|
2022-09-26 01:39:09 +00:00
|
|
|
let process = Process::run(
|
|
|
|
"ffprobe",
|
|
|
|
&[
|
2022-09-26 00:34:51 +00:00
|
|
|
"-v",
|
|
|
|
"quiet",
|
2022-09-26 01:39:09 +00:00
|
|
|
"-select_streams",
|
|
|
|
"v:0",
|
|
|
|
"-count_frames",
|
2022-09-26 00:34:51 +00:00
|
|
|
"-show_entries",
|
2022-09-26 01:39:09 +00:00
|
|
|
"stream=width,height,nb_read_frames:format=format_name",
|
2022-09-26 00:34:51 +00:00
|
|
|
"-of",
|
|
|
|
"default=noprint_wrappers=1:nokey=1",
|
|
|
|
input_file_str,
|
2022-09-26 01:39:09 +00:00
|
|
|
],
|
|
|
|
)?;
|
2022-09-26 00:34:51 +00:00
|
|
|
|
|
|
|
let mut output = Vec::new();
|
|
|
|
process.read().read_to_end(&mut output).await?;
|
2022-09-26 01:39:09 +00:00
|
|
|
let output = String::from_utf8_lossy(&output);
|
|
|
|
tokio::fs::remove_file(input_file_str).await?;
|
|
|
|
|
|
|
|
parse_details(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_details(output: std::borrow::Cow<'_, str>) -> Result<Option<Details>, Error> {
|
|
|
|
tracing::info!("OUTPUT: {}", output);
|
|
|
|
|
|
|
|
let mut lines = output.lines();
|
|
|
|
|
|
|
|
let width = match lines.next() {
|
|
|
|
Some(line) => line,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
2022-09-26 00:34:51 +00:00
|
|
|
|
2022-09-26 01:39:09 +00:00
|
|
|
let height = match lines.next() {
|
|
|
|
Some(line) => line,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
|
|
|
|
let frames = match lines.next() {
|
|
|
|
Some(line) => line,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
|
|
|
|
let formats = match lines.next() {
|
|
|
|
Some(line) => line,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
2022-09-26 00:34:51 +00:00
|
|
|
|
|
|
|
for (k, v) in FORMAT_MAPPINGS {
|
|
|
|
if formats.contains(k) {
|
2022-09-26 01:39:09 +00:00
|
|
|
return Ok(Some(parse_details_inner(width, height, frames, *v)?));
|
2022-09-26 00:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2022-09-26 01:39:09 +00:00
|
|
|
fn parse_details_inner(
|
|
|
|
width: &str,
|
|
|
|
height: &str,
|
|
|
|
frames: &str,
|
|
|
|
format: InputFormat,
|
|
|
|
) -> Result<Details, Error> {
|
|
|
|
let width = width.parse().map_err(|_| UploadError::UnsupportedFormat)?;
|
|
|
|
let height = height.parse().map_err(|_| UploadError::UnsupportedFormat)?;
|
|
|
|
let frames = frames.parse().map_err(|_| UploadError::UnsupportedFormat)?;
|
|
|
|
|
|
|
|
Ok(Details {
|
|
|
|
mime_type: format.to_mime(),
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
frames: Some(frames),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-07 02:40:49 +00:00
|
|
|
#[tracing::instrument(name = "Convert to Mp4", skip(input))]
|
2021-10-23 19:14:12 +00:00
|
|
|
pub(crate) async fn to_mp4_bytes(
|
2021-09-04 00:53:53 +00:00
|
|
|
input: Bytes,
|
|
|
|
input_format: InputFormat,
|
2022-09-25 23:16:37 +00:00
|
|
|
permit_audio: bool,
|
2021-10-23 19:14:12 +00:00
|
|
|
) -> Result<impl AsyncRead + Unpin, Error> {
|
|
|
|
let input_file = crate::tmp_file::tmp_file(Some(input_format.to_ext()));
|
|
|
|
let input_file_str = input_file.to_str().ok_or(UploadError::Path)?;
|
|
|
|
crate::store::file_store::safe_create_parent(&input_file).await?;
|
|
|
|
|
|
|
|
let output_file = crate::tmp_file::tmp_file(Some(".mp4"));
|
|
|
|
let output_file_str = output_file.to_str().ok_or(UploadError::Path)?;
|
|
|
|
crate::store::file_store::safe_create_parent(&output_file).await?;
|
|
|
|
|
|
|
|
let mut tmp_one = crate::file::File::create(&input_file).await?;
|
|
|
|
tmp_one.write_from_bytes(input).await?;
|
|
|
|
tmp_one.close().await?;
|
|
|
|
|
2022-09-25 23:16:37 +00:00
|
|
|
let process = if permit_audio {
|
2022-09-26 01:39:09 +00:00
|
|
|
Process::run(
|
|
|
|
"ffmpeg",
|
|
|
|
&[
|
|
|
|
"-i",
|
|
|
|
input_file_str,
|
|
|
|
"-pix_fmt",
|
|
|
|
"yuv420p",
|
|
|
|
"-vf",
|
|
|
|
"scale=trunc(iw/2)*2:trunc(ih/2)*2",
|
|
|
|
"-c:a",
|
|
|
|
"aac",
|
|
|
|
"-c:v",
|
|
|
|
"h264",
|
|
|
|
"-f",
|
|
|
|
"mp4",
|
|
|
|
output_file_str,
|
|
|
|
],
|
|
|
|
)?
|
2022-09-25 23:16:37 +00:00
|
|
|
} else {
|
2022-09-26 01:39:09 +00:00
|
|
|
Process::run(
|
|
|
|
"ffmpeg",
|
|
|
|
&[
|
|
|
|
"-i",
|
|
|
|
input_file_str,
|
|
|
|
"-pix_fmt",
|
|
|
|
"yuv420p",
|
|
|
|
"-vf",
|
|
|
|
"scale=trunc(iw/2)*2:trunc(ih/2)*2",
|
|
|
|
"-an",
|
|
|
|
"-c:v",
|
|
|
|
"h264",
|
|
|
|
"-f",
|
|
|
|
"mp4",
|
|
|
|
output_file_str,
|
|
|
|
],
|
|
|
|
)?
|
2022-09-25 23:16:37 +00:00
|
|
|
};
|
2021-09-04 00:53:53 +00:00
|
|
|
|
2021-10-23 19:14:12 +00:00
|
|
|
process.wait().await?;
|
|
|
|
tokio::fs::remove_file(input_file).await?;
|
|
|
|
|
|
|
|
let tmp_two = crate::file::File::open(&output_file).await?;
|
|
|
|
let stream = tmp_two.read_to_stream(None, None).await?;
|
|
|
|
let reader = tokio_util::io::StreamReader::new(stream);
|
|
|
|
let clean_reader = crate::tmp_file::cleanup_tmpfile(reader, output_file);
|
|
|
|
|
|
|
|
Ok(Box::pin(clean_reader))
|
2021-09-04 00:53:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 04:48:56 +00:00
|
|
|
#[instrument(name = "Create video thumbnail")]
|
|
|
|
pub(crate) async fn thumbnail<S: Store>(
|
|
|
|
store: S,
|
|
|
|
from: S::Identifier,
|
|
|
|
input_format: InputFormat,
|
2021-08-28 22:15:14 +00:00
|
|
|
format: ThumbnailFormat,
|
2022-03-27 01:45:12 +00:00
|
|
|
) -> Result<impl AsyncRead + Unpin, Error> {
|
2021-10-23 19:14:12 +00:00
|
|
|
let input_file = crate::tmp_file::tmp_file(Some(input_format.to_ext()));
|
|
|
|
let input_file_str = input_file.to_str().ok_or(UploadError::Path)?;
|
|
|
|
crate::store::file_store::safe_create_parent(&input_file).await?;
|
|
|
|
|
|
|
|
let output_file = crate::tmp_file::tmp_file(Some(format.to_ext()));
|
|
|
|
let output_file_str = output_file.to_str().ok_or(UploadError::Path)?;
|
|
|
|
crate::store::file_store::safe_create_parent(&output_file).await?;
|
|
|
|
|
|
|
|
let mut tmp_one = crate::file::File::create(&input_file).await?;
|
|
|
|
tmp_one
|
|
|
|
.write_from_stream(store.to_stream(&from, None, None).await?)
|
|
|
|
.await?;
|
|
|
|
tmp_one.close().await?;
|
|
|
|
|
2021-10-23 04:48:56 +00:00
|
|
|
let process = Process::run(
|
|
|
|
"ffmpeg",
|
|
|
|
&[
|
|
|
|
"-i",
|
2021-10-28 04:06:03 +00:00
|
|
|
input_file_str,
|
2021-10-23 04:48:56 +00:00
|
|
|
"-vframes",
|
|
|
|
"1",
|
|
|
|
"-codec",
|
|
|
|
format.as_codec(),
|
|
|
|
"-f",
|
|
|
|
format.as_format(),
|
2021-10-28 04:06:03 +00:00
|
|
|
output_file_str,
|
2021-10-23 04:48:56 +00:00
|
|
|
],
|
|
|
|
)?;
|
2021-08-26 02:46:11 +00:00
|
|
|
|
2021-10-23 19:14:12 +00:00
|
|
|
process.wait().await?;
|
|
|
|
tokio::fs::remove_file(input_file).await?;
|
|
|
|
|
|
|
|
let tmp_two = crate::file::File::open(&output_file).await?;
|
|
|
|
let stream = tmp_two.read_to_stream(None, None).await?;
|
|
|
|
let reader = tokio_util::io::StreamReader::new(stream);
|
|
|
|
let clean_reader = crate::tmp_file::cleanup_tmpfile(reader, output_file);
|
|
|
|
|
|
|
|
Ok(Box::pin(clean_reader))
|
2021-08-26 02:46:11 +00:00
|
|
|
}
|