-
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This will generate a list of the repo given a path prefix. This is intended to be used only via fastly for repo.clojars.org, so isn't navigable if accessed directly.
- Loading branch information
Showing
6 changed files
with
180 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
(ns clojars.routes.repo-listing | ||
(:require | ||
[clojars.web.repo-listing :as repo-listing] | ||
[compojure.core :as compojure :refer [GET HEAD]] | ||
[ring.util.response :as ring.response])) | ||
|
||
(defn- repo-listing | ||
[repo-bucket path] | ||
(-> (repo-listing/index repo-bucket path) | ||
(ring.response/response) | ||
(ring.response/content-type "text/html;charset=utf-8") | ||
;; Instruct fastly to cache this result for 15 minutes | ||
(ring.response/header "Cache-Control" "s-maxage=900"))) | ||
|
||
(defn routes | ||
[repo-bucket] | ||
(compojure/routes | ||
(GET ["/repo-listing"] | ||
{{:keys [path]} :params} | ||
(repo-listing repo-bucket path)) | ||
(HEAD ["/repo-listing"] | ||
{{:keys [path]} :params} | ||
(repo-listing repo-bucket path)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
(ns clojars.web.repo-listing | ||
(:require | ||
[clojars.s3 :as s3] | ||
[clojars.web.common :as common] | ||
[clojars.web.safe-hiccup :as safe-hiccup] | ||
[clojure.string :as str] | ||
[hiccup.element :as el])) | ||
|
||
(defn- sort-entries | ||
[entries] | ||
(sort | ||
(fn [e1 e2] | ||
(cond | ||
(and (:Prefix e1) (:Prefix e2)) (compare (:Prefix e1) (:Prefix e2)) | ||
(:Prefix e1) -1 | ||
(:Prefix e2) 1 | ||
:else (compare (:Key e1) (:Key e2)))) | ||
entries)) | ||
|
||
(defn- last-segment | ||
[path] | ||
(peek (str/split path #"/"))) | ||
|
||
(defn- entry-line-dispatch | ||
[entry] | ||
(if (:Prefix entry) | ||
:prefix | ||
:file)) | ||
|
||
(defmulti entry-line | ||
#'entry-line-dispatch) | ||
|
||
(def ^:private name-col-width 50) | ||
(def ^:private date-col-width 16) | ||
(def ^:private size-col-width 10) | ||
|
||
(defn- blanks | ||
[max-len content-str] | ||
(safe-hiccup/raw (apply str (repeat (- max-len (count content-str)) " ")))) | ||
|
||
(def ^:private dash "-") | ||
|
||
(defmethod entry-line :prefix | ||
[{:keys [Prefix]}] | ||
(let [suffix (format "%s/" (last-segment Prefix))] | ||
(list | ||
(el/link-to {:title suffix} suffix suffix) | ||
(blanks name-col-width suffix) | ||
(blanks date-col-width dash) | ||
dash | ||
(blanks size-col-width dash) | ||
dash | ||
"\n"))) | ||
|
||
(defmethod entry-line :file | ||
[{:keys [ContentLength Key LastModified]}] | ||
(let [file-name (last-segment Key) | ||
size (str ContentLength)] | ||
(list | ||
(el/link-to {:title file-name} file-name file-name) | ||
(blanks name-col-width file-name) | ||
(common/format-date-with-time LastModified) | ||
(blanks size-col-width size) | ||
size | ||
"\n"))) | ||
|
||
(defn- normalize-path | ||
[path] | ||
(let [path (cond | ||
(str/blank? path) nil | ||
(= "/" path) nil | ||
(str/starts-with? path "/") (subs path 1) | ||
:else path)] | ||
(if (and (some? path) | ||
(not (str/ends-with? path "/"))) | ||
(str path "/") | ||
path))) | ||
|
||
(defn index | ||
[s3 path] | ||
(let [path (normalize-path path) | ||
entries (sort-entries (s3/list-entries s3 path))] | ||
(safe-hiccup/html5 | ||
{:lang "en"} | ||
[:head | ||
[:meta {:charset "utf-8"}] | ||
[:meta {:name "viewport" :content "width=device-width,initial-scale=1"}] | ||
[:title (format "Clojars Repository: %s" path)]] | ||
[:body | ||
[:header | ||
[:h1 (or path "/")]] | ||
[:hr] | ||
[:main | ||
[:pre#contents | ||
(when (some? path) | ||
(list | ||
(el/link-to "../" "../") | ||
"\n")) | ||
(mapcat entry-line entries)]] | ||
[:hr]]))) |