Last active
August 29, 2015 14:04
-
-
Save shaunr0b/d612b2c0189334417322 to your computer and use it in GitHub Desktop.
Upload a file to dropbox abstraction
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
(defn save-file | |
"Saves a file to dropbox | |
dropbox component | |
data spittable data to save | |
file-label keyword file label | |
file-ext keyword file extension" | |
[dropbox data file-label file-ext] | |
(let [file-label (name file-label) | |
file-ext (name file-ext) | |
now (java.util.Date.) | |
formatted-date (.format (java.text.SimpleDateFormat. "yyyy-MM-dd--HH-mm-ss") now) | |
new-tempfile #(java.io.File/createTempFile (str (java.util.UUID/randomUUID) file-label) (str "." file-ext)) | |
tempfile (new-tempfile) | |
tempfile-path (.getAbsolutePath tempfile) | |
dropbox-path (str "path/" formatted-date "-" file-label "." file-ext)] | |
(.deleteOnExit tempfile) ; mark file for jvm-deletion on exit | |
(info "Uploading " file-label " to dropbox:" dropbox-path) | |
(spit tempfile data) | |
(try | |
(upload-file dropbox dropbox-path tempfile-path) | |
(catch Throwable t | |
(error "Error uploading to Dropbox: \n " (str t)))) | |
(.delete tempfile))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment