From f63dd9cb05e3ff0408a9bc4397c0be639c9a1ceb Mon Sep 17 00:00:00 2001 From: Cameron Cordes Date: Tue, 21 Jul 2020 20:03:21 -0400 Subject: [PATCH] Keep thumbnails in their relative directories --- src/files.rs | 1 + src/main.rs | 34 ++++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/files.rs b/src/files.rs index 67a0493..55e60d5 100644 --- a/src/files.rs +++ b/src/files.rs @@ -32,4 +32,5 @@ fn is_image_or_video(path: &Path) -> bool { || extension == "jpeg" || extension == "rs" || extension == "mp4" + || extension == "mov" } diff --git a/src/main.rs b/src/main.rs index 619bfe8..505fc60 100644 --- a/src/main.rs +++ b/src/main.rs @@ -115,7 +115,10 @@ async fn get_image( if let Some(path) = is_valid_path(&req.path) { if req.size.is_some() { let thumbs = dotenv::var("THUMBNAILS").unwrap(); - let thumb_path = Path::new(&thumbs).join(path.file_name().unwrap()); + let relative_path = path + .strip_prefix(dotenv::var("BASE_PATH").unwrap()) + .expect("Error stripping prefix"); + let thumb_path = Path::new(&thumbs).join(relative_path); println!("{:?}", thumb_path); if let Ok(file) = NamedFile::open(&thumb_path) { @@ -124,7 +127,7 @@ async fn get_image( HttpResponse::NotFound().finish() } } else if let Ok(file) = NamedFile::open(path) { - file.into_response(&request).unwrap() + file.into_response(&request).unwrap() } else { HttpResponse::NotFound().finish() } @@ -191,19 +194,23 @@ async fn create_thumbnails() { let images = PathBuf::from(dotenv::var("BASE_PATH").unwrap()); - walkdir::WalkDir::new(images) + walkdir::WalkDir::new(&images) .into_iter() .collect::>>() .into_par_iter() .filter_map(|entry| entry.ok()) .filter(|entry| { println!("{:?}", entry.path()); - if let Some(ext) = entry.path().extension().and_then(|ext| { - ext.to_str().map(|ext| ext.to_lowercase()) - }) { - if ext == "mp4" { - let thumb = Path::new(thumbnail_directory).join(entry.path().file_name().unwrap()); - generate_video_thumbnail(entry.path(), &thumb); + if let Some(ext) = entry + .path() + .extension() + .and_then(|ext| ext.to_str().map(|ext| ext.to_lowercase())) + { + if ext == "mp4" || ext == "mov" { + let relative_path = &entry.path().strip_prefix(&images).unwrap(); + let thumb_path = Path::new(thumbnail_directory).join(relative_path); + std::fs::create_dir_all(&thumb_path.parent().unwrap()).expect("Error creating directory"); + generate_video_thumbnail(entry.path(), &thumb_path); false } else { ext == "jpg" || ext == "jpeg" || ext == "png" @@ -217,9 +224,12 @@ async fn create_thumbnails() { .map(|(img, path)| (img.unwrap(), path)) .map(|(image, path)| (image.thumbnail(200, 200), path)) .map(|(image, path)| { - let thumb = Path::new(thumbnail_directory).join(path.file_name().unwrap()); - println!("{:?}", thumb); - image.save(thumb).expect("Failure saving thumbnail"); + let relative_path = &path.strip_prefix(&images).unwrap(); + let thumb_path = Path::new(thumbnail_directory).join(relative_path); + std::fs::create_dir_all(&thumb_path.parent().unwrap()) + .expect("There was an issue creating directory"); + println!("{:?}", thumb_path); + image.save(thumb_path).expect("Failure saving thumbnail"); }) .for_each(drop);