Skip to content

Instantly share code, notes, and snippets.

@jcromartie
Created November 22, 2011 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcromartie/dda2e42a45226972aee5 to your computer and use it in GitHub Desktop.
Save jcromartie/dda2e42a45226972aee5 to your computer and use it in GitHub Desktop.
(defn store
"Stores the given input (file, stream, etc.) as a document. Returns the new document ID."
[input]
(let [temp-file (file (File/createTempFile "anybillfs-store" nil))]
(with-open [in (input-stream input)
out (output-stream temp-file)
digest-stream (DigestOutputStream. out (MessageDigest/getInstance "SHA-1"))]
(copy in out)
(let [id (hexbytes (.digest (.getMessageDigest digest-stream)))
dest-file (file-for-id id)]
(make-parents dest-file)
(assert (true? (.renameTo temp-file dest-file)))
id))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment