2023-07-10 20:29:41 +00:00
|
|
|
use crate::process::{Process, ProcessError};
|
2021-09-04 00:53:53 +00:00
|
|
|
use actix_web::web::Bytes;
|
2022-10-15 16:13:24 +00:00
|
|
|
use tokio::io::{AsyncRead, AsyncReadExt};
|
|
|
|
|
2023-07-10 20:29:41 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub(crate) enum ExifError {
|
|
|
|
#[error("Error in process")]
|
|
|
|
Process(#[source] ProcessError),
|
|
|
|
|
|
|
|
#[error("Error reading process output")]
|
|
|
|
Read(#[source] std::io::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExifError {
|
|
|
|
pub(crate) fn is_client_error(&self) -> bool {
|
|
|
|
// if exiftool bails we probably have bad input
|
|
|
|
matches!(self, Self::Process(ProcessError::Status(_)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 16:13:24 +00:00
|
|
|
#[tracing::instrument(level = "trace", skip(input))]
|
2023-07-10 20:29:41 +00:00
|
|
|
pub(crate) async fn needs_reorienting(input: Bytes) -> Result<bool, ExifError> {
|
|
|
|
let process =
|
|
|
|
Process::run("exiftool", &["-n", "-Orientation", "-"]).map_err(ExifError::Process)?;
|
2022-10-15 16:13:24 +00:00
|
|
|
let mut reader = process.bytes_read(input);
|
|
|
|
|
|
|
|
let mut buf = String::new();
|
2023-07-10 20:29:41 +00:00
|
|
|
reader
|
|
|
|
.read_to_string(&mut buf)
|
|
|
|
.await
|
|
|
|
.map_err(ExifError::Read)?;
|
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-07-10 20:29:41 +00:00
|
|
|
pub(crate) fn clear_metadata_bytes_read(input: Bytes) -> Result<impl AsyncRead + Unpin, ExifError> {
|
|
|
|
let process =
|
|
|
|
Process::run("exiftool", &["-all=", "-", "-out", "-"]).map_err(ExifError::Process)?;
|
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
|
|
|
}
|