Prevent uploading non images #1
This commit is contained in:
parent
4b0a030caf
commit
34d724292b
2 changed files with 25 additions and 14 deletions
|
@ -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))]
|
||||||
|
(try
|
||||||
|
(do
|
||||||
(as-file ((apply fn params) file)
|
(as-file ((apply fn params) file)
|
||||||
target-filepath
|
target-filepath
|
||||||
:verbatim)
|
:verbatim)
|
||||||
(if (= (fs/extension file) ".tmp")
|
(if (= (fs/extension file) ".tmp")
|
||||||
(fs/delete file))))
|
(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"))))
|
||||||
|
|
|
@ -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
|
||||||
|
(do
|
||||||
|
(add-image-to-database imghash title description tags)
|
||||||
(-> (redirect "/upload")
|
(-> (redirect "/upload")
|
||||||
(assoc :flash {:message "Upload erfolgreich." :type "success"})))
|
(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)
|
||||||
|
|
Loading…
Reference in a new issue