feat: r96util probe media before importing
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
kieran 2025-02-12 14:52:06 +00:00
parent 317b0708e0
commit 6998f0ffac
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941

View File

@ -5,6 +5,7 @@ use indicatif::{ProgressBar, ProgressStyle};
use log::{error, info}; use log::{error, info};
use route96::db::{Database, FileUpload}; use route96::db::{Database, FileUpload};
use route96::filesystem::{FileStore, FileSystemResult}; use route96::filesystem::{FileStore, FileSystemResult};
use route96::processing::probe_file;
use route96::settings::Settings; use route96::settings::Settings;
use std::future::Future; use std::future::Future;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -36,6 +37,8 @@ enum Commands {
Import { Import {
#[arg(long)] #[arg(long)]
from: PathBuf, from: PathBuf,
#[arg(long, default_missing_value = "true", num_args = 0..=1)]
probe_media: Option<bool>,
}, },
/// Import files from filesystem into database /// Import files from filesystem into database
@ -94,7 +97,7 @@ async fn main() -> Result<(), Error> {
}) })
.await?; .await?;
} }
Commands::Import { from } => { Commands::Import { from, probe_media } => {
let fs = FileStore::new(settings.clone()); let fs = FileStore::new(settings.clone());
let db = Database::new(&settings.database).await?; let db = Database::new(&settings.database).await?;
db.migrate().await?; db.migrate().await?;
@ -106,6 +109,16 @@ async fn main() -> Result<(), Error> {
let mime = infer::get_from_path(&entry)? let mime = infer::get_from_path(&entry)?
.map(|m| m.mime_type()) .map(|m| m.mime_type())
.unwrap_or("application/octet-stream"); .unwrap_or("application/octet-stream");
// test media is not corrupt
if probe_media.unwrap_or(true)
&& (mime.starts_with("image/") || mime.starts_with("video/"))
&& probe_file(&entry).is_err()
{
p.set_message(format!("Skipping media invalid file: {}", &entry.display()));
return Ok(());
}
let file = tokio::fs::File::open(&entry).await?; let file = tokio::fs::File::open(&entry).await?;
let dst = fs.put(file, mime, false).await?; let dst = fs.put(file, mime, false).await?;
match dst { match dst {