Last active
January 13, 2022 20:36
-
-
Save andrerocker/028e3be907c893ad13fd07a4d3e9a8dd to your computer and use it in GitHub Desktop.
clojure: check if a file is a valid jpg file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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