Commits

Alvaro Videla  committed 066a0d1

improves code organization

  • Participants
  • Parent commits be8e6c2

Comments (0)

Files changed (1)

File src/resizer/resizer.clj

 (defn read-file [filename]
   (gfs/find-one {:filename filename}))
 
-(defn file-input-stream [filename]
-  (. (read-file filename) getInputStream))
+(defn file-input-stream [file]
+  (. file getInputStream))
 
 (defn image-from-input-stream [filename]
-  (. ImageIO read (file-input-stream filename)))
+  (. ImageIO read (file-input-stream (read-file filename))))
 
-(defn buffimg-to-bytearray [img]
+(defn buffimg-to-bytearray [img format]
   (with-open [baos  (ByteArrayOutputStream.)]
-      (ImageIO/write img "jpg" baos)
-      (.flush baos)
-      (.toByteArray baos)))
+    (ImageIO/write img format baos)
+    (.flush baos)
+    (.toByteArray baos)))
 
-(defn save-file [file-buffer fname fmetadata fctype]
-  (store-file (make-input-file file-buffer)
-              (filename (cs/join ["small_" fname]))
-              (metadata fmetadata)
-              (content-type fctype)))
-
-(defn resize-image [original-image w h]
+(defn resize-image
+  "Resizes image: based on this blog post: http://www.mkyong.com/java/how-to-resize-an-image-in-java/"
+  [original-image w h]
   (let [buff-img (BufferedImage. w h (get-type original-image))
         g (.createGraphics buff-img)]
     (.drawImage g original-image 0 0 w h nil)
     (.dispose g)
     buff-img))
 
-;; fetches image from gridfs
-;; resizes image
-;; stores new image into gridfs with the small_ prefix
-(defn process-image [filename]
+(defn save-file [file-buffer fname fmetadata fctype]
+  (store-file (make-input-file file-buffer)
+              (filename (cs/join ["small_" fname]))
+              (metadata fmetadata)
+              (content-type fctype)))
+
+(defn process-image
+  "- Fetches image from gridfs and converts it to an ImageIO
+   - Resizes the image.
+   - Stores new image into gridfs with the small_ prefix"
+  [filename]
   (let [original-image (image-from-input-stream filename)
         resized-image (resize-image original-image width height)]
-    (save-file (buffimg-to-bytearray resized-image) filename {:format "jpg"} "image/jpg")))
+    ;; TODO: enable other mime types
+    (save-file (buffimg-to-bytearray resized-image "jpg") filename {:format "jpg"} "image/jpg")))