mirror of
https://github.com/v0l/route96.git
synced 2025-06-16 08:18:50 +00:00
Compress images
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
use std::env::temp_dir;
|
||||
use std::io::{SeekFrom};
|
||||
use std::fs;
|
||||
use std::io::SeekFrom;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::{fs};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::SystemTime;
|
||||
|
||||
use anyhow::Error;
|
||||
use log::info;
|
||||
@ -9,6 +11,7 @@ use sha2::{Digest, Sha256};
|
||||
use tokio::fs::File;
|
||||
use tokio::io::{AsyncRead, AsyncReadExt, AsyncSeekExt};
|
||||
|
||||
use crate::processing::{FileProcessor, MediaProcessor};
|
||||
use crate::settings::Settings;
|
||||
|
||||
#[derive(Clone)]
|
||||
@ -20,12 +23,14 @@ pub struct FileSystemResult {
|
||||
|
||||
pub struct FileStore {
|
||||
path: String,
|
||||
processor: Arc<Mutex<MediaProcessor>>,
|
||||
}
|
||||
|
||||
impl FileStore {
|
||||
pub fn new(settings: Settings) -> Self {
|
||||
Self {
|
||||
path: settings.storage_dir,
|
||||
processor: Arc::new(Mutex::new(MediaProcessor::new())),
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,23 +40,49 @@ impl FileStore {
|
||||
}
|
||||
|
||||
/// Store a new file
|
||||
pub async fn put<TStream>(&self, mut stream: TStream) -> Result<FileSystemResult, Error>
|
||||
where
|
||||
TStream: AsyncRead + Unpin,
|
||||
pub async fn put<TStream>(&self, mut stream: TStream, mime_type: &str) -> Result<FileSystemResult, Error>
|
||||
where
|
||||
TStream: AsyncRead + Unpin,
|
||||
{
|
||||
let random_id = uuid::Uuid::new_v4();
|
||||
let tmp_path = FileStore::map_temp(random_id);
|
||||
|
||||
let mut file = File::options()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.read(true)
|
||||
.open(tmp_path.clone())
|
||||
.await?;
|
||||
let n = tokio::io::copy(&mut stream, &mut file).await?;
|
||||
let (n, hash, tmp_path) = {
|
||||
let tmp_path = FileStore::map_temp(random_id);
|
||||
let mut file = File::options()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.read(true)
|
||||
.open(tmp_path.clone())
|
||||
.await?;
|
||||
tokio::io::copy(&mut stream, &mut file).await?;
|
||||
|
||||
info!("File saved to temp path: {}", tmp_path.to_str().unwrap());
|
||||
let hash = FileStore::hash_file(&mut file).await?;
|
||||
info!("File saved to temp path: {}", tmp_path.to_str().unwrap());
|
||||
|
||||
let start = SystemTime::now();
|
||||
let new_temp = {
|
||||
let mut p_lock = self.processor.lock().expect("asd");
|
||||
p_lock.process_file(tmp_path.clone(), mime_type)?
|
||||
};
|
||||
let old_size = tmp_path.metadata()?.len();
|
||||
let new_size = new_temp.metadata()?.len();
|
||||
info!("Compressed media: ratio={:.2}x, old_size={:.3}kb, new_size={:.3}kb, duration={:.2}ms",
|
||||
old_size as f32 / new_size as f32,
|
||||
old_size as f32 / 1024.0,
|
||||
new_size as f32 / 1024.0,
|
||||
SystemTime::now().duration_since(start).unwrap().as_micros() as f64 / 1000.0
|
||||
);
|
||||
|
||||
let mut file = File::options()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.read(true)
|
||||
.open(new_temp.clone())
|
||||
.await?;
|
||||
let n = file.metadata().await?.len();
|
||||
let hash = FileStore::hash_file(&mut file).await?;
|
||||
fs::remove_file(tmp_path)?;
|
||||
(n, hash, new_temp)
|
||||
};
|
||||
let dst_path = self.map_path(&hash);
|
||||
fs::create_dir_all(dst_path.parent().unwrap())?;
|
||||
if let Err(e) = fs::copy(&tmp_path, &dst_path) {
|
||||
|
Reference in New Issue
Block a user