add basic file retrieval from files db

This commit is contained in:
Gered 2013-05-20 20:07:42 -04:00
parent 78170ceb10
commit 515d95a2b3
4 changed files with 39 additions and 1 deletions

View file

@ -2,6 +2,7 @@
(:use blarg.routes.home
blarg.routes.posts
blarg.routes.auth
blarg.routes.files
blarg.routes.accessrules
compojure.core)
(:require [noir.util.middleware :as middleware]
@ -44,7 +45,7 @@
(timbre/info "blarg is shutting down..."))
;;append your application routes to the all-routes vector
(def all-routes [auth-routes home-routes posts-routes app-routes])
(def all-routes [auth-routes home-routes posts-routes files-routes app-routes])
(def app (-> all-routes
(middleware/app-handler)

View file

@ -0,0 +1,17 @@
(ns blarg.models.files
(:use [blarg.models.db])
(:require [com.ashafa.clutch :as couch]
[clojure.java.io :as io]))
(defn get-file [file]
(couch/with-db files
(if-let [file-info (->first-view-value
(couch/get-view "files" "listAll" {:key file}))]
(let [id (:_id file-info)
attachment (:filename file-info)
attachment-info (second (first (:_attachments file-info)))
gz (couch/get-attachment id attachment)]
(if gz
(merge
{:data gz}
attachment-info))))))

View file

@ -0,0 +1,15 @@
(ns blarg.routes.files
(:use [compojure.core]
[blarg.routes.helpers]
[blarg.util])
(:require [blarg.views.layout :as layout]
[blarg.models.files :as files]
[noir.response :as resp]))
(defn get-file [path]
(if-let [file (files/get-file (ensure-prefix path "/"))]
(resp/content-type (:content_type file) (:data file))
(resp/status 404 nil)))
(defroutes files-routes
(GET "/files/*" [*] (get-file *)))

View file

@ -46,3 +46,8 @@
(< n low) low
(> n high) high
:else n))
(defn ensure-prefix [s prefix]
(if-not (.startsWith s prefix)
(str prefix s)
s))