Skip to content

Commit

Permalink
Set the content-type when uploading to cloudfiles [#433]
Browse files Browse the repository at this point in the history
Otherwise, they end up with "application/unknown".
  • Loading branch information
tobias committed May 1, 2016
1 parent 1943f7c commit 71f7b33
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 25 deletions.
71 changes: 47 additions & 24 deletions src/clojars/cloudfiles.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns clojars.cloudfiles
(:require [clojure.java.io :as io]
(:require [clojars.file-utils :as fu]
[clojure.java.io :as io]
[clojure.string :as str]
[org.jclouds.blobstore2 :as jc])
(:import [org.jclouds.blobstore
Expand All @@ -23,7 +24,7 @@
(jc/create-container bs container-name :public-read? true))
(->Connection bs container-name))))

(defn- apply-conn [f conn & args]
(defn apply-conn [f conn & args]
(apply f (:bs conn) (:container conn) args))

(defn remote-path [prefix path]
Expand All @@ -32,31 +33,54 @@
(defn artifact-exists? [conn path]
(apply-conn jc/blob-exists? conn path))

(def ^:private metadata-builders
{:md5 (memfn getETag)
:content-type #(.getContentType (.getContentMetadata %))
:created (memfn getCreationDate)
:last-modified (memfn getLastModified)
:name (memfn getName)
:size (memfn getSize)
:user-metadata #(into {} (.getUserMetadata %))})

(defn artifact-metadata [conn path]
(when-let [md (apply-conn jc/blob-metadata conn path)]
(let [metadata-builders
{:md5 (memfn getETag)
:content-type #(.getContentType (.getContentMetadata %))
:created (memfn getCreationDate)
:last-modified (memfn getLastModified)
:name (memfn getName)
:size (memfn getSize)
:uri (memfn getUri)
:user-metadata #(into {} (.getUserMetadata %))}]
(defn metadata->map [md]
(reduce (fn [m [k f]]
(assoc m k (f md)))
{}
metadata-builders)))

(defn artifact-metadata [conn path]
(when-let [md (apply-conn jc/blob-metadata conn path)]
(metadata->map md)))

(let [content-types
{:asc :txt
:bundle :xml
:clj :txt
:eclipse-plugin :zip
:gz "application/gzip"
:jar "application/x-java-archive"
:md5 :txt
:pom :xml
:properties :txt
:sha1 :txt
:txt "text/plain"
:xml "application/xml"
:zip "application/zip"
:unknown "application/unknown"}]
(defn content-type [suffix]
(let [ct (content-types (keyword suffix) :unknown)]
(if (keyword? ct)
(content-type ct)
ct))))

(defn put-file
([conn name file]
(put-file conn name file nil))
([conn name file options]
(apply-conn jc/put-blob conn
(apply-map jc/blob name
(-> options
(assoc :payload file)
(update :content-md5 #(when % (HashCode/fromString %))))))))
[conn name file]
(let [file' (io/file file)]
(apply-conn jc/put-blob conn
(jc/blob name
:payload file'
:content-type (content-type (last (str/split name #"\.")))
:content-md5 (HashCode/fromString (fu/checksum file' :md5))))))

;; borrowed from jclouds
;; (https://github.com/jclouds/jclouds/blob/master/blobstore/src/main/clojure/org/jclouds/blobstore2.clj)
Expand Down Expand Up @@ -89,9 +113,8 @@

;; end jclouds code

(defn artifact-seq [conn]
(map (memfn getName) (apply-conn container-seq conn {:recursive true :with-details false})))

(defn metadata-seq [conn]
(map metadata->map (apply-conn container-seq conn {:recursive true :with-details false})))

;; All deletions require purging from the CDN:
;; https://developer.rackspace.com/docs/cdn/v1/developer-guide/#purge-a-cached-asset
Expand Down
2 changes: 1 addition & 1 deletion src/clojars/tools/upload_repo.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

(defn get-existing [conn]
(println "Retrieving current artifact list (this will take a while)")
(into #{} (cf/artifact-seq conn)))
(into #{} (map :name (cf/metadata-seq conn))))

(defn upload-repo [conn repo]
(let [existing (get-existing conn)]
Expand Down
16 changes: 16 additions & 0 deletions test/clojars/test/unit/cloudfiles.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(ns clojars.test.unit.cloudfiles
(:require [clojars.cloudfiles :refer :all]
[clojure.java.io :as io]
[clojure.test :refer :all]))

(deftest put-file-should-set-content-type
(let [f (io/file (io/resource "config.clj"))
conn (connect "" "" "test" "transient")]
(put-file conn "foo.clj" f)
(is (= "text/plain" (:content-type (artifact-metadata conn "foo.clj"))))

(put-file conn "foo.blah" f)
(is (= "application/unknown" (:content-type (artifact-metadata conn "foo.blah"))))

(put-file conn "foo.gz" f)
(is (= "application/gzip" (:content-type (artifact-metadata conn "foo.gz"))))))

0 comments on commit 71f7b33

Please sign in to comment.