mirror of
https://github.com/penpot/penpot.git
synced 2026-08-01 19:06:18 +00:00
🐛 Fix image magick info call (#8300)
This commit is contained in:
parent
ccfee34e76
commit
3d20fc508d
@ -35,8 +35,7 @@
|
|||||||
javax.xml.parsers.SAXParserFactory
|
javax.xml.parsers.SAXParserFactory
|
||||||
org.apache.commons.io.IOUtils
|
org.apache.commons.io.IOUtils
|
||||||
org.im4java.core.ConvertCmd
|
org.im4java.core.ConvertCmd
|
||||||
org.im4java.core.IMOperation
|
org.im4java.core.IMOperation))
|
||||||
org.im4java.core.Info))
|
|
||||||
|
|
||||||
(def default-max-file-size
|
(def default-max-file-size
|
||||||
(* 1024 1024 10)) ; 10 MiB
|
(* 1024 1024 10)) ; 10 MiB
|
||||||
@ -224,17 +223,18 @@
|
|||||||
;; If we are processing an animated gif we use the first frame with -scene 0
|
;; If we are processing an animated gif we use the first frame with -scene 0
|
||||||
(let [dim-result (sh/sh "identify" "-format" "%w %h\n" path)
|
(let [dim-result (sh/sh "identify" "-format" "%w %h\n" path)
|
||||||
orient-result (sh/sh "identify" "-format" "%[EXIF:Orientation]\n" path)]
|
orient-result (sh/sh "identify" "-format" "%[EXIF:Orientation]\n" path)]
|
||||||
(if (and (= 0 (:exit dim-result))
|
(when (= 0 (:exit dim-result))
|
||||||
(= 0 (:exit orient-result)))
|
|
||||||
(let [[w h] (-> (:out dim-result)
|
(let [[w h] (-> (:out dim-result)
|
||||||
str/trim
|
str/trim
|
||||||
(clojure.string/split #"\s+")
|
(clojure.string/split #"\s+")
|
||||||
(->> (mapv #(Integer/parseInt %))))
|
(->> (mapv #(Integer/parseInt %))))
|
||||||
orientation (-> orient-result :out str/trim)]
|
orientation-exit (:exit orient-result)
|
||||||
(case orientation
|
orientation (-> orient-result :out str/trim)]
|
||||||
("6" "8") {:width h :height w} ; Rotated 90 or 270 degrees
|
(if (= 0 orientation-exit)
|
||||||
{:width w :height h})) ; Normal or unknown orientation
|
(case orientation
|
||||||
nil)))
|
("6" "8") {:width h :height w} ; Rotated 90 or 270 degrees
|
||||||
|
{:width w :height h}) ; Normal or unknown orientation
|
||||||
|
{:width w :height h}))))) ; If orientation can't be read, use dimensions as-is
|
||||||
|
|
||||||
(defmethod process :info
|
(defmethod process :info
|
||||||
[{:keys [input] :as params}]
|
[{:keys [input] :as params}]
|
||||||
@ -247,26 +247,37 @@
|
|||||||
:hint "uploaded svg does not provides dimensions"))
|
:hint "uploaded svg does not provides dimensions"))
|
||||||
(merge input info {:ts (ct/now) :size (fs/size path)}))
|
(merge input info {:ts (ct/now) :size (fs/size path)}))
|
||||||
|
|
||||||
(let [instance (Info. (str path))
|
(let [path-str (str path)
|
||||||
mtype' (.getProperty instance "Mime type")]
|
identify-res (sh/sh "identify" "-format" "image/%[magick]\n" path-str)
|
||||||
|
;; identify prints one line per frame (animated GIFs, etc.); we take the first one
|
||||||
|
mtype' (if (zero? (:exit identify-res))
|
||||||
|
(-> identify-res
|
||||||
|
:out
|
||||||
|
str/trim
|
||||||
|
(str/split #"\s+" 2)
|
||||||
|
first
|
||||||
|
str/lower)
|
||||||
|
(ex/raise :type :validation
|
||||||
|
:code :invalid-image
|
||||||
|
:hint "invalid image"))
|
||||||
|
{:keys [width height]}
|
||||||
|
(or (get-dimensions-with-orientation path-str)
|
||||||
|
(do
|
||||||
|
(l/warn "Failed to read image dimensions with orientation" {:path path})
|
||||||
|
(ex/raise :type :validation
|
||||||
|
:code :invalid-image
|
||||||
|
:hint "invalid image")))]
|
||||||
(when (and (string? mtype)
|
(when (and (string? mtype)
|
||||||
(not= mtype mtype'))
|
(not= (str/lower mtype) mtype'))
|
||||||
(ex/raise :type :validation
|
(ex/raise :type :validation
|
||||||
:code :media-type-mismatch
|
:code :media-type-mismatch
|
||||||
:hint (str "Seems like you are uploading a file whose content does not match the extension."
|
:hint (str "Seems like you are uploading a file whose content does not match the extension."
|
||||||
"Expected: " mtype ". Got: " mtype')))
|
"Expected: " mtype ". Got: " mtype')))
|
||||||
(let [{:keys [width height]}
|
(assoc input
|
||||||
(or (get-dimensions-with-orientation (str path))
|
:width width
|
||||||
(do
|
:height height
|
||||||
(l/warn "Failed to read image dimensions with orientation; falling back to im4java"
|
:size (fs/size path)
|
||||||
{:path path})
|
:ts (ct/now))))))
|
||||||
{:width (.getPageWidth instance)
|
|
||||||
:height (.getPageHeight instance)}))]
|
|
||||||
(assoc input
|
|
||||||
:width width
|
|
||||||
:height height
|
|
||||||
:size (fs/size path)
|
|
||||||
:ts (ct/now)))))))
|
|
||||||
|
|
||||||
(defmethod process-error org.im4java.core.InfoException
|
(defmethod process-error org.im4java.core.InfoException
|
||||||
[error]
|
[error]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user