Keep thumbnails in their relative directories
This commit is contained in:
@@ -32,4 +32,5 @@ fn is_image_or_video(path: &Path) -> bool {
|
|||||||
|| extension == "jpeg"
|
|| extension == "jpeg"
|
||||||
|| extension == "rs"
|
|| extension == "rs"
|
||||||
|| extension == "mp4"
|
|| extension == "mp4"
|
||||||
|
|| extension == "mov"
|
||||||
}
|
}
|
||||||
|
|||||||
34
src/main.rs
34
src/main.rs
@@ -115,7 +115,10 @@ async fn get_image(
|
|||||||
if let Some(path) = is_valid_path(&req.path) {
|
if let Some(path) = is_valid_path(&req.path) {
|
||||||
if req.size.is_some() {
|
if req.size.is_some() {
|
||||||
let thumbs = dotenv::var("THUMBNAILS").unwrap();
|
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);
|
println!("{:?}", thumb_path);
|
||||||
if let Ok(file) = NamedFile::open(&thumb_path) {
|
if let Ok(file) = NamedFile::open(&thumb_path) {
|
||||||
@@ -124,7 +127,7 @@ async fn get_image(
|
|||||||
HttpResponse::NotFound().finish()
|
HttpResponse::NotFound().finish()
|
||||||
}
|
}
|
||||||
} else if let Ok(file) = NamedFile::open(path) {
|
} else if let Ok(file) = NamedFile::open(path) {
|
||||||
file.into_response(&request).unwrap()
|
file.into_response(&request).unwrap()
|
||||||
} else {
|
} else {
|
||||||
HttpResponse::NotFound().finish()
|
HttpResponse::NotFound().finish()
|
||||||
}
|
}
|
||||||
@@ -191,19 +194,23 @@ async fn create_thumbnails() {
|
|||||||
|
|
||||||
let images = PathBuf::from(dotenv::var("BASE_PATH").unwrap());
|
let images = PathBuf::from(dotenv::var("BASE_PATH").unwrap());
|
||||||
|
|
||||||
walkdir::WalkDir::new(images)
|
walkdir::WalkDir::new(&images)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect::<Vec<Result<_, _>>>()
|
.collect::<Vec<Result<_, _>>>()
|
||||||
.into_par_iter()
|
.into_par_iter()
|
||||||
.filter_map(|entry| entry.ok())
|
.filter_map(|entry| entry.ok())
|
||||||
.filter(|entry| {
|
.filter(|entry| {
|
||||||
println!("{:?}", entry.path());
|
println!("{:?}", entry.path());
|
||||||
if let Some(ext) = entry.path().extension().and_then(|ext| {
|
if let Some(ext) = entry
|
||||||
ext.to_str().map(|ext| ext.to_lowercase())
|
.path()
|
||||||
}) {
|
.extension()
|
||||||
if ext == "mp4" {
|
.and_then(|ext| ext.to_str().map(|ext| ext.to_lowercase()))
|
||||||
let thumb = Path::new(thumbnail_directory).join(entry.path().file_name().unwrap());
|
{
|
||||||
generate_video_thumbnail(entry.path(), &thumb);
|
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
|
false
|
||||||
} else {
|
} else {
|
||||||
ext == "jpg" || ext == "jpeg" || ext == "png"
|
ext == "jpg" || ext == "jpeg" || ext == "png"
|
||||||
@@ -217,9 +224,12 @@ async fn create_thumbnails() {
|
|||||||
.map(|(img, path)| (img.unwrap(), path))
|
.map(|(img, path)| (img.unwrap(), path))
|
||||||
.map(|(image, path)| (image.thumbnail(200, 200), path))
|
.map(|(image, path)| (image.thumbnail(200, 200), path))
|
||||||
.map(|(image, path)| {
|
.map(|(image, path)| {
|
||||||
let thumb = Path::new(thumbnail_directory).join(path.file_name().unwrap());
|
let relative_path = &path.strip_prefix(&images).unwrap();
|
||||||
println!("{:?}", thumb);
|
let thumb_path = Path::new(thumbnail_directory).join(relative_path);
|
||||||
image.save(thumb).expect("Failure saving thumbnail");
|
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);
|
.for_each(drop);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user