2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-12-22 11:21:24 +00:00
pict-rs/src/ffmpeg.rs

99 lines
2.5 KiB
Rust
Raw Normal View History

use std::ffi::OsString;
use futures_core::Future;
use crate::{error_code::ErrorCode, process::ProcessError, store::StoreError, tmp_file::TmpDir};
2023-07-10 20:29:41 +00:00
#[derive(Debug, thiserror::Error)]
pub(crate) enum FfMpegError {
#[error("Error in ffmpeg process")]
Process(#[source] ProcessError),
#[error("Error writing bytes")]
Write(#[source] std::io::Error),
#[error("Invalid output format")]
Json(#[source] serde_json::Error),
#[error("Error creating parent directory")]
CreateDir(#[source] crate::store::file_store::FileError),
#[error("Error reading file to stream")]
ReadFile(#[source] crate::store::file_store::FileError),
#[error("Error opening file")]
OpenFile(#[source] std::io::Error),
2023-12-23 17:58:20 +00:00
#[error("Error cleaning up after command")]
Cleanup(#[source] std::io::Error),
2023-07-10 20:29:41 +00:00
#[error("Error in store")]
Store(#[source] StoreError),
#[error("Invalid media file provided")]
CommandFailed(ProcessError),
2023-07-10 20:29:41 +00:00
}
impl From<ProcessError> for FfMpegError {
fn from(value: ProcessError) -> Self {
match value {
e @ ProcessError::Status(_, _) => Self::CommandFailed(e),
otherwise => Self::Process(otherwise),
}
}
}
2023-07-10 20:29:41 +00:00
impl FfMpegError {
2023-09-02 01:50:10 +00:00
pub(crate) const fn error_code(&self) -> ErrorCode {
match self {
Self::CommandFailed(_) => ErrorCode::COMMAND_FAILURE,
Self::Store(s) => s.error_code(),
Self::Process(e) => e.error_code(),
2023-12-23 03:00:37 +00:00
Self::Write(_)
2023-09-02 01:50:10 +00:00
| Self::Json(_)
| Self::CreateDir(_)
| Self::ReadFile(_)
| Self::OpenFile(_)
| Self::Cleanup(_) => ErrorCode::COMMAND_ERROR,
2023-09-02 01:50:10 +00:00
}
}
2023-07-10 20:29:41 +00:00
pub(crate) fn is_client_error(&self) -> bool {
// Failing validation or ffmpeg bailing probably means bad input
2023-08-05 21:18:06 +00:00
matches!(
self,
Self::CommandFailed(_) | Self::Process(ProcessError::Timeout(_))
)
2023-07-10 20:29:41 +00:00
}
pub(crate) fn is_not_found(&self) -> bool {
if let Self::Store(e) = self {
return e.is_not_found();
}
false
}
}
pub(crate) async fn with_file<F, Fut>(
tmp: &TmpDir,
ext: Option<&str>,
f: F,
) -> Result<Fut::Output, FfMpegError>
where
F: FnOnce(OsString) -> Fut,
Fut: Future,
{
let file = tmp.tmp_file(ext);
crate::store::file_store::safe_create_parent(&file)
.await
.map_err(FfMpegError::CreateDir)?;
let res = (f)(file.as_os_str().to_os_string()).await;
file.cleanup().await.map_err(FfMpegError::Cleanup)?;
Ok(res)
}