Extend FileServer to accept options (:expires, :filename)
parent
70aedbf12c
commit
c1c1ffde70
@ -1,4 +1,4 @@
|
|||||||
(ns asciinema.boundary.file-server)
|
(ns asciinema.boundary.file-server)
|
||||||
|
|
||||||
(defprotocol FileServer
|
(defprotocol FileServer
|
||||||
(serve [this path]))
|
(serve [this path] [this path opts]))
|
||||||
|
@ -1,15 +1,38 @@
|
|||||||
(ns asciinema.component.s3-file-server
|
(ns asciinema.component.s3-file-server
|
||||||
(:require [asciinema.boundary.file-server :as file-server]
|
(:require [asciinema.boundary.file-server :as file-server]
|
||||||
[aws.sdk.s3 :as s3]
|
[aws.sdk.s3 :as s3]
|
||||||
[ring.util.http-response :as response]))
|
[clj-time.core :as time]
|
||||||
|
[clj-time.coerce :as timec]
|
||||||
|
[ring.util.http-response :as response])
|
||||||
|
(:import com.amazonaws.services.s3.model.ResponseHeaderOverrides
|
||||||
|
com.amazonaws.services.s3.AmazonS3Client
|
||||||
|
com.amazonaws.auth.BasicAWSCredentials
|
||||||
|
com.amazonaws.services.s3.model.GeneratePresignedUrlRequest))
|
||||||
|
|
||||||
;; TODO support custom expiry date (it's 1 day now)
|
(defn- s3-client* [cred]
|
||||||
|
(let [credentials (BasicAWSCredentials. (:access-key cred) (:secret-key cred))]
|
||||||
|
(AmazonS3Client. credentials)))
|
||||||
|
|
||||||
|
(def ^:private s3-client (memoize s3-client*))
|
||||||
|
|
||||||
|
(defn- generate-presigned-url [cred bucket path {:keys [expires filename]
|
||||||
|
:or {expires (-> 1 time/days time/from-now)}}]
|
||||||
|
(let [client (s3-client cred)
|
||||||
|
request (GeneratePresignedUrlRequest. bucket path)]
|
||||||
|
(.setExpiration request (timec/to-date expires))
|
||||||
|
(when filename
|
||||||
|
(let [header-overrides (doto (ResponseHeaderOverrides.)
|
||||||
|
(.setContentDisposition (str "attachment; filename=" filename)))]
|
||||||
|
(.setResponseHeaders request header-overrides)))
|
||||||
|
(.toString (.generatePresignedUrl client request))))
|
||||||
|
|
||||||
(defrecord S3FileServer [cred bucket path-prefix]
|
(defrecord S3FileServer [cred bucket path-prefix]
|
||||||
file-server/FileServer
|
file-server/FileServer
|
||||||
(serve [this path]
|
(serve [this path]
|
||||||
|
(file-server/serve this path {}))
|
||||||
|
(serve [this path opts]
|
||||||
(let [path (str path-prefix path)]
|
(let [path (str path-prefix path)]
|
||||||
(response/found (s3/generate-presigned-url cred bucket path)))))
|
(response/found (generate-presigned-url cred bucket path opts)))))
|
||||||
|
|
||||||
(defn s3-file-server [{:keys [cred bucket path-prefix]}]
|
(defn s3-file-server [{:keys [cred bucket path-prefix]}]
|
||||||
(->S3FileServer cred bucket path-prefix))
|
(->S3FileServer cred bucket path-prefix))
|
||||||
|
Loading…
Reference in New Issue