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]
(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"))))

View file

@ -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)