clojure-clutch/clutch

Provide access to content type through get-attachment

Closed this issue · 3 comments

The _attachments map of a document contains the content type of an attachment, but it is currently not available through the get-attachment function of Clutch. As a result, 2 queries are needed when e.g. forwarding an attachment with an unknown content type to a client. It would be useful to have the option of retrieving the content type (and possibly the other metadata) together with the input stream.

There's a brief discussion at http://groups.google.com/group/clojure-clutch/browse_thread/thread/7481bd39ad681533

Thanks for considering this!

Starting in 0.4.0-SNAPSHOT, clutch optionally provides access to the underlying HTTP response (actually, the clj-http response map) in its entirety. This allows you to get the content type provided by CouchDB in response to get-attachment:

=> (require '[com.ashafa.clutch :as db])
nil
=> (use '[com.ashafa.clutch.http-client :only (*response*)])
nil
=> (db/put-document "demo" {:_id "123"})
{:_rev "1-967a00dff5e02add41819138abb3284d", :_id "123"}
=> (db/put-attachment "demo" *1 (java.io.File. "contacts.csv") :mime-type "text/csv")
{:ok true, :id "123", :rev "2-68a3b4a2b592d27634be989e41f02ffa"}
=> (binding [*response* nil]
     (let [attachment (db/get-attachment "demo" (db/get-document "demo" "123") "contacts.csv")]
       [attachment (-> *response* :headers (get "content-type"))]))
[#<GZIPInputStream java.util.zip.GZIPInputStream@71558d83> "text/csv"]

Of course, you can access any of the other bits of information as well. Perhaps a better to serve others' use cases would be:

=> (binding [*response* nil]
     (let [attachment (db/get-attachment "demo" (db/get-document "demo" "123") "contacts.csv")]
       [attachment (-> *response* :headers (select-keys ["content-length" "content-type" "etag"]))]))
[#<GZIPInputStream java.util.zip.GZIPInputStream@7a00bec2>
 {"etag" "\"2-68a3b4a2b592d27634be989e41f02ffa\"", "content-type" "text/csv", "content-length" "65979"}]

Does this properly suit your requirements?

Hi,

Binding response works well for me. I wasn't really aware of this possibility earlier, but I now see it as a good solution after having reading about it in your excellent book. Thanks for all the great work!

Best regards,
Miikka

On May 4, 2012, at 4:42 PM, Chas Emerick wrote:

Starting in 0.4.0-SNAPSHOT, clutch optionally provides access to the underlying HTTP response (actually, the clj-http response map) in its entirety. This allows you to get the content type provided by CouchDB in response to get-attachment:

=> (require '[com.ashafa.clutch :as db])
nil
=> (use '[com.ashafa.clutch.http-client :only (*response*)])
nil
=> (db/put-document "demo" {:_id "123"})
{:_rev "1-967a00dff5e02add41819138abb3284d", :_id "123"}
=> (db/put-attachment "demo" *1 (java.io.File. "contacts.csv") :mime-type "text/csv")
{:ok true, :id "123", :rev "2-68a3b4a2b592d27634be989e41f02ffa"}
=> (binding [*response* nil]
    (let [attachment (db/get-attachment "demo" (db/get-document "demo" "123") "contacts.csv")]
      [attachment (-> *response* :headers (get "content-type"))]))
[#<GZIPInputStream java.util.zip.GZIPInputStream@71558d83> "text/csv"]

Of course, you can access any of the other bits of information as well. Perhaps a better to serve others' use cases would be:

=> (binding [*response* nil]
    (let [attachment (db/get-attachment "demo" (db/get-document "demo" "123") "contacts.csv")]
      [attachment (-> *response* :headers (select-keys ["content-length" "content-type" "etag"]))]))
[#<GZIPInputStream java.util.zip.GZIPInputStream@7a00bec2>
{"etag" "\"2-68a3b4a2b592d27634be989e41f02ffa\"", "content-type" "text/csv", "content-length" "65979"}]

Does this properly suit your requirements?


Reply to this email directly or view it on GitHub:
#50 (comment)

Well, only the HTTP response code was available before, so this is far more general.

For the record, the functionality described here is available starting in [com.ashafa/clutch "0.4.0-SNAPSHOT"], and will be in the final 0.4.0 release.