Make image uploading more robust #1
This commit is contained in:
parent
0d6662d093
commit
4b0a030caf
2 changed files with 20 additions and 15 deletions
|
@ -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)))
|
||||
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"))))
|
||||
|
|
|
@ -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"})))
|
||||
|
|
Loading…
Reference in a new issue