Created
May 16, 2010 18:45
-
-
Save LauJensen/403077 to your computer and use it in GitHub Desktop.
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
(ns bestinclass.io | |
(:use compojure) | |
(:import | |
(java.io File) | |
(org.apache.commons.fileupload.servlet ServletFileUpload) | |
(org.apache.commons.fileupload.disk DiskFileItemFactory))) | |
(def *max_size* (* 30 1024 1024)) ; 30 Megabytes | |
(defn upload-file | |
[request base-path] | |
(if (ServletFileUpload/isMultipartContent request) | |
(let [uploader (ServletFileUpload. (DiskFileItemFactory.)) | |
items (.parseRequest uploader request)] | |
(doseq [item items] | |
(when (and (not (.isFormField item)) | |
(> *max_size* (.getSize item))) | |
(try | |
(let [file (File. (str base-path (.getName item)))] | |
(.write item file)) | |
(catch Exception e (println "\nGot exception: " e)))))))) | |
(defroutes r | |
(POST "/upload/" (upload-file (:servlet-request request) "public/uploads/"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment