Keep thumbnails in their relative directories

This commit is contained in:
Cameron Cordes
2020-07-21 20:03:21 -04:00
parent 7dbd93cedc
commit f63dd9cb05
2 changed files with 23 additions and 12 deletions

View File

@@ -32,4 +32,5 @@ fn is_image_or_video(path: &Path) -> bool {
|| extension == "jpeg"
|| extension == "rs"
|| extension == "mp4"
|| extension == "mov"
}

View File

@@ -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::<Vec<Result<_, _>>>()
.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);