Skip to content

Instantly share code, notes, and snippets.

@andrerocker
Last active January 13, 2022 20:36
Show Gist options
  • Save andrerocker/028e3be907c893ad13fd07a4d3e9a8dd to your computer and use it in GitHub Desktop.
Save andrerocker/028e3be907c893ad13fd07a4d3e9a8dd to your computer and use it in GitHub Desktop.
clojure: check if a file is a valid jpg file
;; Using javax.imageio and funcool/cats
;; it's leaking fds, take care!
(defn valid-jpg-image? [image-url]
(let [image-input (-> image-url io/input-stream ImageIO/createImageInputStream)
encoders (-> (ImageIO/getImageReadersByFormatName "jpg") iterator-seq)
image-result (map #(either/try-either (doto % (.setInput image-input) (.read 0))) encoders)]
(->> image-result either/rights not-empty boolean)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment