Prevent uploading non images #1

This commit is contained in:
Aaron Fischer 2017-05-03 22:02:20 +02:00
parent 4b0a030caf
commit 34d724292b
2 changed files with 25 additions and 14 deletions

View file

@ -62,21 +62,28 @@
(defn save [fn params filepath target-filepath] (defn save [fn params filepath target-filepath]
(let [file (remove-metadata filepath (io/file filepath))] (let [file (remove-metadata filepath (io/file filepath))]
(as-file ((apply fn params) file) (try
target-filepath (do
:verbatim) (as-file ((apply fn params) file)
(if (= (fs/extension file) ".tmp") target-filepath
(fs/delete file)))) :verbatim)
(if (= (fs/extension file) ".tmp")
(fs/delete file))
true)
(catch Exception e
(fs/delete file)
false))))
(defn scale-image-and-save [filepath] (defn scale-image-and-save [filepath]
(let [image-hash (target-image-filename filepath) (let [image-hash (target-image-filename filepath)
file-extension (fs/extension filepath) file-extension (fs/extension filepath)
new-file-name (str image-hash file-extension)] new-file-name (str image-hash file-extension)]
(fs/copy filepath (data-path "gallery" "raw" new-file-name)) (fs/copy filepath (data-path "gallery" "raw" new-file-name))
(do (if (every? true?
(save scale-normal [1024 768] filepath (data-path "gallery" "normal" new-file-name)) [(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))) (save scale-thumbnail [250] filepath (data-path "gallery" "thumbnails" new-file-name))])
new-file-name)) new-file-name
false)))
(defn process-all-images [] (defn process-all-images []
(pmap scale-image-and-save (fs/list-dir (data-path "to-process")))) (pmap scale-image-and-save (fs/list-dir (data-path "to-process"))))

View file

@ -67,11 +67,15 @@
"admin/upload.html" "admin/upload.html"
{:flash (:flash request)}))) {:flash (:flash request)})))
(POST "/upload" [file title description tags] (POST "/upload" [file title description tags]
(-> (upload-file file) (let [filename (upload-file file)
(images/scale-image-and-save) imghash (images/scale-image-and-save filename)]
(add-image-to-database title description tags)) (if imghash
(-> (redirect "/upload") (do
(assoc :flash {:message "Upload erfolgreich." :type "success"}))) (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] (GET "/delete/:id" [id :as request]
(delete-image! id request) (delete-image! id request)