From 34d724292b66db9a5da28e112568c5e0dc68a153 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Wed, 3 May 2017 22:02:20 +0200 Subject: [PATCH] Prevent uploading non images #1 --- src/clj/yenu/helpers/images.clj | 25 ++++++++++++++++--------- src/clj/yenu/routes/admin.clj | 14 +++++++++----- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/clj/yenu/helpers/images.clj b/src/clj/yenu/helpers/images.clj index be86d4f..7c24855 100644 --- a/src/clj/yenu/helpers/images.clj +++ b/src/clj/yenu/helpers/images.clj @@ -62,21 +62,28 @@ (defn save [fn params filepath target-filepath] (let [file (remove-metadata filepath (io/file filepath))] - (as-file ((apply fn params) file) - target-filepath - :verbatim) - (if (= (fs/extension file) ".tmp") - (fs/delete file)))) + (try + (do + (as-file ((apply fn params) file) + target-filepath + :verbatim) + (if (= (fs/extension file) ".tmp") + (fs/delete file)) + true) + (catch Exception e + (fs/delete file) + false)))) (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)) - (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)) + (if (every? true? + [(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 + false))) (defn process-all-images [] (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 8129e99..21c7fed 100644 --- a/src/clj/yenu/routes/admin.clj +++ b/src/clj/yenu/routes/admin.clj @@ -67,11 +67,15 @@ "admin/upload.html" {:flash (:flash request)}))) (POST "/upload" [file title description tags] - (-> (upload-file file) - (images/scale-image-and-save) - (add-image-to-database title description tags)) - (-> (redirect "/upload") - (assoc :flash {:message "Upload erfolgreich." :type "success"}))) + (let [filename (upload-file file) + imghash (images/scale-image-and-save filename)] + (if imghash + (do + (add-image-to-database imghash title description tags) + (-> (redirect "/upload") + (assoc :flash {:message "Upload erfolgreich." :type "success"}))) + (-> (redirect "/upload") + (assoc :flash {:message "Fehler beim Upload." :type "danger"}))))) (GET "/delete/:id" [id :as request] (delete-image! id request)