Don’t overwrite the original image with the thumbnail
- ID
07d5d24- date
2024-08-20 12:10:12+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
bf91a28- message
Don't overwrite the original image with the thumbnail- changed files
3 files, 16 additions, 2 deletions
Changed files
README.md (257) → README.md (218)
diff --git a/README.md b/README.md
index 1467d5c..a829860 100644
--- a/README.md
+++ b/README.md
@@ -6,4 +6,3 @@ focusing on a small piece of code makes it better
-> creates thumbnail directory
-> /dev/null
-> thumbnail dir is file?
- -> try to overwrite original file?
src/create_thumbnail.rs (6652) → src/create_thumbnail.rs (6886)
diff --git a/src/create_thumbnail.rs b/src/create_thumbnail.rs
index bf4e1d2..9a4ae53 100644
--- a/src/create_thumbnail.rs
+++ b/src/create_thumbnail.rs
@@ -19,7 +19,13 @@ pub fn create_thumbnail(
let thumbnail_path = out_dir.join(path.file_name().unwrap());
create_parent_directory(&thumbnail_path)?;
- // TODO: Does this check do what I think?
+ // Make sure we don't overwrite the original image with a thumbnail
+ if *path == thumbnail_path {
+ return Err(io::Error::new(
+ io::ErrorKind::InvalidData,
+ "Cannot write thumbnail to the same directory as the original image",
+ ));
+ }
assert!(*path != thumbnail_path);
let (new_width, new_height) = get_thumbnail_dimensions(&path, target)
src/main.rs (6016) → src/main.rs (6384)
diff --git a/src/main.rs b/src/main.rs
index 79388f7..afda6e6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -130,6 +130,15 @@ mod test_cli {
}
#[test]
+ fn it_fails_if_you_try_to_overwrite_the_original_file() {
+ let output = get_failure(&["src/images/noise.jpg", "--width=50", "--out-dir=src/images"]);
+
+ assert_eq!(output.exit_code, 1);
+ assert_eq!(output.stderr, "Cannot write thumbnail to the same directory as the original image\n");
+ assert_eq!(output.stdout, "");
+ }
+
+ #[test]
fn it_prints_the_version() {
let output = get_success(&["--version"]);