2023-09-02 01:50:10 +00:00
|
|
|
use crate::{
|
|
|
|
error_code::ErrorCode,
|
2023-12-23 02:52:58 +00:00
|
|
|
process::{Process, ProcessError, ProcessRead},
|
2023-09-02 01:50:10 +00:00
|
|
|
};
|
2021-09-04 00:53:53 +00:00
|
|
|
use actix_web::web::Bytes;
|
2022-10-15 16:13:24 +00:00
|
|
|
|
2023-07-10 20:29:41 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub(crate) enum ExifError {
|
|
|
|
#[error("Error in process")]
|
|
|
|
Process(#[source] ProcessError),
|
|
|
|
|
2023-07-17 18:30:08 +00:00
|
|
|
#[error("Invalid media file provided")]
|
|
|
|
CommandFailed(ProcessError),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ProcessError> for ExifError {
|
|
|
|
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 ExifError {
|
2023-09-02 01:50:10 +00:00
|
|
|
pub(crate) const fn error_code(&self) -> ErrorCode {
|
|
|
|
match self {
|
|
|
|
Self::Process(e) => e.error_code(),
|
|
|
|
Self::CommandFailed(_) => ErrorCode::COMMAND_FAILURE,
|
|
|
|
}
|
|
|
|
}
|
2023-07-10 20:29:41 +00:00
|
|
|
pub(crate) fn is_client_error(&self) -> bool {
|
|
|
|
// if exiftool bails we probably have bad input
|
2023-12-23 03:00:37 +00:00
|
|
|
match self {
|
|
|
|
Self::CommandFailed(_) => true,
|
|
|
|
Self::Process(e) => e.is_client_error(),
|
|
|
|
}
|
2023-07-10 20:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 16:13:24 +00:00
|
|
|
#[tracing::instrument(level = "trace", skip(input))]
|
2023-08-05 17:41:06 +00:00
|
|
|
pub(crate) async fn needs_reorienting(timeout: u64, input: Bytes) -> Result<bool, ExifError> {
|
2023-12-23 02:52:58 +00:00
|
|
|
let buf = Process::run("exiftool", &["-n", "-Orientation", "-"], &[], timeout)?
|
|
|
|
.bytes_read(input)
|
2023-12-23 17:58:20 +00:00
|
|
|
.into_string()
|
2023-12-23 02:52:58 +00:00
|
|
|
.await?;
|
2022-10-15 16:13:24 +00:00
|
|
|
|
|
|
|
Ok(!buf.is_empty())
|
|
|
|
}
|
2021-09-04 00:53:53 +00:00
|
|
|
|
2022-10-02 02:17:18 +00:00
|
|
|
#[tracing::instrument(level = "trace", skip(input))]
|
2023-08-05 17:41:06 +00:00
|
|
|
pub(crate) fn clear_metadata_bytes_read(
|
|
|
|
timeout: u64,
|
|
|
|
input: Bytes,
|
2023-12-23 02:52:58 +00:00
|
|
|
) -> Result<ProcessRead, ExifError> {
|
2023-11-10 00:20:59 +00:00
|
|
|
let process = Process::run("exiftool", &["-all=", "-", "-out", "-"], &[], timeout)?;
|
2021-09-04 00:53:53 +00:00
|
|
|
|
2022-04-07 02:40:49 +00:00
|
|
|
Ok(process.bytes_read(input))
|
2021-09-04 00:53:53 +00:00
|
|
|
}
|