From 6071c257b9b77422556e040c1318bc3c0ae2b6ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sn=C3=ADda=C5=88ov=C3=BD=20Mistr?= Date: Sun, 3 Mar 2024 22:35:43 +0100 Subject: [PATCH] =?UTF-8?q?Pokus=20o=20opravu=20soubor=C5=AF=20v=20systemd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/files.rs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/files.rs b/src/files.rs index 2835c0b..a3b9c98 100755 --- a/src/files.rs +++ b/src/files.rs @@ -1,9 +1,8 @@ -use std::process::Command; - use actix_multipart::form::tempfile::TempFile; use chrono::Utc; +use std::process::Command; use tokio::{ - fs::{remove_file, rename}, + fs::{copy, remove_file}, task::spawn_blocking, }; @@ -67,8 +66,6 @@ impl File { let timestamp = Utc::now().timestamp_micros(); let format = format.to_owned(); - let new_name = format!("{timestamp}.{format}"); - let (thumb_format, thumb_name) = if thumb { let format = if video { "png".into() } else { format.clone() }; @@ -77,15 +74,15 @@ impl File { (None, None) }; - rename(temp_file.file.path(), format!("/tmp/{new_name}")).await?; + let path = temp_file.file.path().to_string_lossy().to_string(); let (width, height) = if video { - process_video(cfg, original_name.clone(), new_name.clone(), thumb_name).await? + process_video(cfg, original_name.clone(), path.clone(), thumb_name).await? } else { - process_image(cfg, original_name.clone(), new_name.clone(), thumb_name).await? + process_image(cfg, original_name.clone(), path.clone(), thumb_name).await? }; - rename(format!("/tmp/{new_name}"), format!("./uploads/{new_name}")).await?; + copy(path, format!("./uploads/{timestamp}.{format}")).await?; let file = File { original_name, @@ -134,14 +131,14 @@ impl File { async fn process_image( cfg: &Cfg, original_name: String, - new_name: String, + path: String, thumb_name: Option, ) -> Result<(u32, u32), NekrochanError> { - let new_name_ = new_name.clone(); + let path_ = path.clone(); let identify_out = spawn_blocking(move || { Command::new("identify") - .args(["-format", "%wx%h", &format!("/tmp/{new_name_}[0]")]) + .args(["-format", "%wx%h", &format!("{path_}[0]")]) .output() }) .await??; @@ -181,7 +178,7 @@ async fn process_image( let output = spawn_blocking(move || { Command::new("convert") - .arg(&format!("/tmp/{new_name}")) + .arg(path) .arg("-coalesce") .arg("-thumbnail") .arg(&format!("{thumb_size}x{thumb_size}>")) @@ -205,10 +202,10 @@ async fn process_image( async fn process_video( cfg: &Cfg, original_name: String, - new_name: String, + path: String, thumb_name: Option, ) -> Result<(u32, u32), NekrochanError> { - let new_name_ = new_name.clone(); + let path_ = path.clone(); let ffprobe_out = spawn_blocking(move || { Command::new("ffprobe") @@ -221,7 +218,7 @@ async fn process_video( "stream=width,height", "-of", "csv=s=x:p=0", - &format!("/tmp/{new_name_}"), + &path_, ]) .output() }) @@ -271,7 +268,7 @@ async fn process_video( Command::new("ffmpeg") .args([ "-i", - &format!("/tmp/{new_name}"), + &path, "-ss", "00:00:00.50", "-vframes",