From 514593101b638634a5a56cb4db6aae767147e483 Mon Sep 17 00:00:00 2001 From: gered Date: Wed, 22 May 2013 09:23:11 -0400 Subject: [PATCH] add initial route/model support for uploading new files --- src/blarg/models/files.clj | 17 +++++++++++++++-- src/blarg/routes/files.clj | 11 +++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/blarg/models/files.clj b/src/blarg/models/files.clj index 3839f2d..4879b12 100644 --- a/src/blarg/models/files.clj +++ b/src/blarg/models/files.clj @@ -1,6 +1,7 @@ (ns blarg.models.files (:use [blarg.models.db] - [blarg.datetime]) + [blarg.datetime] + [blarg.util]) (:require [com.ashafa.clutch :as couch] [clojure.java.io :as io])) @@ -34,4 +35,16 @@ (defn get-tree [] (->view-keys (couch/with-db files - (couch/get-view "files" "listPaths" {:group true})))) \ No newline at end of file + (couch/get-view "files" "listPaths" {:group true})))) + +(defn add-file [path filename file content-type] + (let [proper-path (ensure-prefix path "/") + id (str proper-path filename)] + (if-let [doc (couch/with-db files + (couch/put-document {:_id id + :filename filename + :path proper-path + :last_modified_at (get-timestamp) + :published true}))] + (couch/with-db files + (couch/put-attachment doc file :filename filename :mime-type content-type))))) diff --git a/src/blarg/routes/files.clj b/src/blarg/routes/files.clj index 6101986..fc16199 100644 --- a/src/blarg/routes/files.clj +++ b/src/blarg/routes/files.clj @@ -14,6 +14,16 @@ :files (files/list-files path) :tree (files/get-tree)})) +(defn handle-new-file [path file] + (println "path: " path ", file: " file) + (if-let [newfile (files/add-file + (ensure-prefix path "/") + (:filename file) + (:tempfile file) + (:content-type file))] + (resp/redirect (str "/listfiles" (ensure-prefix path "/"))) + (throw (Exception. "Error uploading file")))) + (defn get-file [path] (if-let [file (files/get-file path)] (resp/content-type (:content_type file) (:data file)) @@ -22,4 +32,5 @@ (defroutes files-routes (restricted GET "/listfiles" [] (list-files "/")) (restricted GET "/listfiles/*" [*] (list-files (ensure-prefix * "/"))) + (restricted POST "/uploadfile" [path file] (handle-new-file path file)) (GET "/files/*" [*] (get-file (ensure-prefix * "/"))))