fix up some path handling/fixing

This commit is contained in:
Gered 2013-05-22 22:33:48 -04:00
parent 514593101b
commit e8e12e08e9
3 changed files with 31 additions and 21 deletions

View file

@ -6,9 +6,10 @@
[clojure.java.io :as io])) [clojure.java.io :as io]))
(defn get-file [file] (defn get-file [file]
(let [f (ensure-prefix file "/")]
(couch/with-db files (couch/with-db files
(if-let [file-info (->first-view-value (if-let [file-info (->first-view-value
(couch/get-view "files" "listPublished" {:key file}))] (couch/get-view "files" "listPublished" {:key f}))]
(let [id (:_id file-info) (let [id (:_id file-info)
attachment (:filename file-info) attachment (:filename file-info)
attachment-info (second (first (:_attachments file-info))) attachment-info (second (first (:_attachments file-info)))
@ -16,12 +17,13 @@
(if gz (if gz
(merge (merge
{:data gz} {:data gz}
attachment-info)))))) attachment-info)))))))
(defn list-files [path] (defn list-files [path]
(let [p (ensure-prefix-suffix path "/")]
(if-let [file-list (->view-values (if-let [file-list (->view-values
(couch/with-db files (couch/with-db files
(couch/get-view "files" "listPublishedByPath" {:key path})))] (couch/get-view "files" "listPublishedByPath" {:key p})))]
(map (map
(fn [f] (fn [f]
(let [attachment (second (first (:_attachments f)))] (let [attachment (second (first (:_attachments f)))]
@ -30,7 +32,7 @@
:last_modified (parse-timestamp (:last_modified_at f)) :last_modified (parse-timestamp (:last_modified_at f))
:content-type (:content_type attachment) :content-type (:content_type attachment)
:size (:length attachment)})) :size (:length attachment)}))
file-list))) file-list))))
(defn get-tree [] (defn get-tree []
(->view-keys (->view-keys
@ -38,12 +40,12 @@
(couch/get-view "files" "listPaths" {:group true})))) (couch/get-view "files" "listPaths" {:group true}))))
(defn add-file [path filename file content-type] (defn add-file [path filename file content-type]
(let [proper-path (ensure-prefix path "/") (let [p (ensure-prefix-suffix path "/")
id (str proper-path filename)] id (str p filename)]
(if-let [doc (couch/with-db files (if-let [doc (couch/with-db files
(couch/put-document {:_id id (couch/put-document {:_id id
:filename filename :filename filename
:path proper-path :path p
:last_modified_at (get-timestamp) :last_modified_at (get-timestamp)
:published true}))] :published true}))]
(couch/with-db files (couch/with-db files

View file

@ -8,21 +8,21 @@
[noir.response :as resp])) [noir.response :as resp]))
(defn list-files [path] (defn list-files [path]
(let [p (ensure-prefix-suffix path "/")]
(layout/render (layout/render
"files/list.html" {:html-title (->html-title [(str "Files in " path)]) "files/list.html" {:html-title (->html-title [(str "Files in " p)])
:path path :path p
:files (files/list-files path) :files (files/list-files p)
:tree (files/get-tree)})) :tree (files/get-tree)})))
(defn handle-new-file [path file] (defn handle-new-file [path file]
(println "path: " path ", file: " file) (let [p (ensure-prefix-suffix path "/")]
(if-let [newfile (files/add-file (if-let [newfile (files/add-file p
(ensure-prefix path "/")
(:filename file) (:filename file)
(:tempfile file) (:tempfile file)
(:content-type file))] (:content-type file))]
(resp/redirect (str "/listfiles" (ensure-prefix path "/"))) (resp/redirect (str "/listfiles" p))
(throw (Exception. "Error uploading file")))) (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)]
@ -31,6 +31,6 @@
(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 *))
(restricted POST "/uploadfile" [path file] (handle-new-file path file)) (restricted POST "/uploadfile" [path file] (handle-new-file path file))
(GET "/files/*" [*] (get-file (ensure-prefix * "/")))) (GET "/files/*" [*] (get-file *)))

View file

@ -51,3 +51,11 @@
(if-not (.startsWith s prefix) (if-not (.startsWith s prefix)
(str prefix s) (str prefix s)
s)) s))
(defn ensure-suffix [s suffix]
(if-not (.endsWith s suffix)
(str s suffix)
s))
(defn ensure-prefix-suffix [s affix]
(ensure-prefix (ensure-suffix s affix) affix))