From 4b0a030cafeacc1e9087a84b75f0edcf137e81b4 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Wed, 3 May 2017 21:42:12 +0200 Subject: [PATCH] Make image uploading more robust #1 --- src/clj/yenu/helpers/images.clj | 33 +++++++++++++++++++-------------- src/clj/yenu/routes/admin.clj | 2 +- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/clj/yenu/helpers/images.clj b/src/clj/yenu/helpers/images.clj index d8e18cd..be86d4f 100644 --- a/src/clj/yenu/helpers/images.clj +++ b/src/clj/yenu/helpers/images.clj @@ -6,6 +6,7 @@ [image-resizer.resize :refer :all] [image-resizer.scale-methods :refer :all] [clojure.java.io :as io] + [clojure.string :as s] [digest :as digest] [me.raynes.fs :as fs] [clj-exif-orientation.core :as exif] @@ -31,7 +32,7 @@ (str (.lastModified file) "-" (digest/md5 file)))) (defn delete-image! [image-hash] - (run! #(fs/delete (data-path "gallery" % (str image-hash ".png"))) + (run! #(fs/delete (data-path "gallery" % image-hash)) ["normal" "raw" "thumbnails"])) (defn scale-thumbnail [width] @@ -49,29 +50,33 @@ (defn scale-normal [width height] (resize-fn width height ultra-quality)) +(defn remove-metadata [filepath file] + (if (contains? #{".jpg" ".jpeg" ".jpe"} (s/lower-case (fs/extension filepath))) + (let [new-file (exif/without-exif file)] + (if (= (fs/size new-file) 0) + (do + (.delete new-file) + file) + new-file)) + file)) + (defn save [fn params filepath target-filepath] - (let [file (exif/without-exif (io/file filepath))] + (let [file (remove-metadata filepath (io/file filepath))] (as-file ((apply fn params) file) target-filepath :verbatim) - (fs/delete file))) + (if (= (fs/extension file) ".tmp") + (fs/delete file)))) (defn scale-image-and-save [filepath] (let [image-hash (target-image-filename filepath) file-extension (fs/extension filepath) new-file-name (str image-hash file-extension)] (fs/copy filepath (data-path "gallery" "raw" new-file-name)) - (try - (do - (save scale-normal [1024 768] filepath (data-path "gallery" "normal" new-file-name)) - (save scale-thumbnail [250] filepath (data-path "gallery" "thumbnails" new-file-name))) - (catch Exception e (prn e))) + (do + (save scale-normal [1024 768] filepath (data-path "gallery" "normal" new-file-name)) + (save scale-thumbnail [250] filepath (data-path "gallery" "thumbnails" new-file-name))) new-file-name)) -(defn process-image [filepath] - (let [image-hash (scale-image-and-save filepath)] - ;;;(fs/delete filepath) - image-hash)) - (defn process-all-images [] - (pmap process-image (fs/list-dir (data-path "to-process")))) + (pmap scale-image-and-save (fs/list-dir (data-path "to-process")))) diff --git a/src/clj/yenu/routes/admin.clj b/src/clj/yenu/routes/admin.clj index e2bf910..8129e99 100644 --- a/src/clj/yenu/routes/admin.clj +++ b/src/clj/yenu/routes/admin.clj @@ -68,7 +68,7 @@ {:flash (:flash request)}))) (POST "/upload" [file title description tags] (-> (upload-file file) - (images/process-image) + (images/scale-image-and-save) (add-image-to-database title description tags)) (-> (redirect "/upload") (assoc :flash {:message "Upload erfolgreich." :type "success"})))