add initial route/model support for uploading new files
This commit is contained in:
parent
456309ddf7
commit
514593101b
|
@ -1,6 +1,7 @@
|
||||||
(ns blarg.models.files
|
(ns blarg.models.files
|
||||||
(:use [blarg.models.db]
|
(:use [blarg.models.db]
|
||||||
[blarg.datetime])
|
[blarg.datetime]
|
||||||
|
[blarg.util])
|
||||||
(:require [com.ashafa.clutch :as couch]
|
(:require [com.ashafa.clutch :as couch]
|
||||||
[clojure.java.io :as io]))
|
[clojure.java.io :as io]))
|
||||||
|
|
||||||
|
@ -35,3 +36,15 @@
|
||||||
(->view-keys
|
(->view-keys
|
||||||
(couch/with-db files
|
(couch/with-db files
|
||||||
(couch/get-view "files" "listPaths" {:group true}))))
|
(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)))))
|
||||||
|
|
|
@ -14,6 +14,16 @@
|
||||||
:files (files/list-files path)
|
:files (files/list-files path)
|
||||||
:tree (files/get-tree)}))
|
: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]
|
(defn get-file [path]
|
||||||
(if-let [file (files/get-file path)]
|
(if-let [file (files/get-file path)]
|
||||||
(resp/content-type (:content_type file) (:data file))
|
(resp/content-type (:content_type file) (:data file))
|
||||||
|
@ -22,4 +32,5 @@
|
||||||
(defroutes files-routes
|
(defroutes files-routes
|
||||||
(restricted GET "/listfiles" [] (list-files "/"))
|
(restricted GET "/listfiles" [] (list-files "/"))
|
||||||
(restricted GET "/listfiles/*" [*] (list-files (ensure-prefix * "/")))
|
(restricted GET "/listfiles/*" [*] (list-files (ensure-prefix * "/")))
|
||||||
|
(restricted POST "/uploadfile" [path file] (handle-new-file path file))
|
||||||
(GET "/files/*" [*] (get-file (ensure-prefix * "/"))))
|
(GET "/files/*" [*] (get-file (ensure-prefix * "/"))))
|
||||||
|
|
Reference in a new issue