(resource-response path)
(resource-response path options)
Returns a Ring response to serve a packaged resource, or nil if the
resource does not exist.
Options:
:root - take the resource relative to this root
:loader - resolve the resource in this class loader
:allow-symlinks? - allow symlinks that lead to paths outside the root
classpath directories (defaults to false)
Source
(defn resource-response
"Returns a Ring response to serve a packaged resource, or nil if the
resource does not exist.
Options:
:root - take the resource relative to this root
:loader - resolve the resource in this class loader
:allow-symlinks? - allow symlinks that lead to paths outside the root
classpath directories (defaults to false)"
([path]
(resource-response path {}))
([path options]
(let [path (-> (str "/" path) (.replace "//" "/"))
root+path (-> (str (:root options) path) (.replaceAll "^/" ""))
load #(if-let [loader (:loader options)]
(io/resource % loader)
(io/resource %))]
(if-not (directory-transversal? root+path)
(if-let [resource (load root+path)]
(let [response (url-response resource)]
(if (or (not (instance? File (:body response)))
(safe-file-resource? response options))
response)))))))