Created
August 31, 2012 00:26
-
-
Save craigbro/3546421 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(declare ^{:dynamic true} *request-hooks*) | |
(defn add-request-hook [fun] | |
(swap! *request-hooks* conj fun)) | |
(defn request-hooks-handler | |
"Middleware that lets you set up a hook that is called on exit" | |
[handler] | |
(fn [request] | |
(binding [*request-hooks* (atom [])] | |
(let [name (gensym)] | |
(try | |
(info "Entering a request hooks handler: " name " hooks: ") | |
(handler request) | |
(finally | |
(info "Running request hooks: " name " hooks: " *request-hooks*) | |
(map apply *request-hooks*))))))) | |
(defn threatbrain-file-upload-store [item] | |
(let [temp-file (utils/make-temp-file "file-upload-")] | |
(io/copy (:stream item) temp-file) | |
(if true | |
(do | |
(info "Adding request hook to delete uploaded file: " temp-file) | |
(when *request-hooks* | |
(add-request-hook | |
(fn [] | |
(info "Deleting temp file: " temp-file) | |
(.delete temp-file))))) | |
(warn "Request Hooks not enabled, cannot register uploaded files for deletion")) | |
(-> (select-keys item [:filename :content-type]) | |
(assoc :tempfile temp-file | |
:sha256 (utils/file-sha256 temp-file) | |
:size (.length temp-file))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment