<commit_before>(ns clojars.routes.artifact
  (:require [compojure.core :as compojure :refer [GET POST]]
            [clojars.db :as db]
            [clojars.auth :as auth]
            [clojars.web.jar :as view]
            [clojars.web.common :as common]
            [clojars.promote :as promote]
            [ring.util.response :as response]
            [clojure.set :as set]))

(defn show [db reporter stats group-id artifact-id]
  (if-let [artifact (db/find-jar db group-id artifact-id)]
    (auth/try-account
     (view/show-jar db
                    reporter
                    stats
                    account
                    artifact
                    (db/recent-versions db group-id artifact-id 5)
                    (db/count-versions db group-id artifact-id)))))

(defn list-versions [db group-id artifact-id]
  (if-let [artifact (db/find-jar db group-id artifact-id)]
    (auth/try-account
     (view/show-versions account
                         artifact
                         (db/recent-versions db group-id artifact-id)))))

(defn show-version [db reporter stats group-id artifact-id version]
  (if-let [artifact (db/find-jar db group-id artifact-id version)]
    (auth/try-account
     (view/show-jar db
                    reporter
                    stats
                    account
                    artifact
                    (db/recent-versions db group-id artifact-id 5)
                    (db/count-versions db group-id artifact-id)))))

(defn response-based-on-format
  "render appropriate response based on the file type suffix provided:
  JSON or SVG"
  [db file-format artifact-id & [group-id]]
  (let [group-id (or group-id artifact-id)]
  (cond
    (= file-format "json") (-> (response/response (view/make-latest-version-json db group-id artifact-id))
                               (response/header "Cache-Control" "no-cache")
                               (response/content-type "application/json; charset=UTF-8"))
    (= file-format "svg") (-> (response/response (view/make-latest-version-svg db group-id artifact-id))
                              (response/header "Cache-Control" "no-cache")
                              (response/content-type "image/svg+xml")))))

(defn routes [db reporter stats]
  (compojure/routes
   (GET ["/:artifact-id", :artifact-id #"[^/]+"] [artifact-id]
        (show db reporter stats artifact-id artifact-id))
   (GET ["/:group-id/:artifact-id", :group-id #"[^/]+" :artifact-id #"[^/]+"]
        [group-id artifact-id]
        (show db reporter stats group-id artifact-id))

   (GET ["/:artifact-id/versions" :artifact-id #"[^/]+"] [artifact-id]
        (list-versions db artifact-id artifact-id))
   (GET ["/:group-id/:artifact-id/versions"
         :group-id #"[^/]+" :artifact-id #"[^/]+"]
        [group-id artifact-id]<commit_msg>Ensure CORS conformity on artifact endpoints<commit_after>(ns clojars.routes.artifact
  (:require [compojure.core :as compojure :refer [GET POST]]
            [clojars.db :as db]
            [clojars.auth :as auth]
            [clojars.web.jar :as view]
            [clojars.web.common :as common]
            [clojars.promote :as promote]
            [ring.util.response :as response]
            [clojure.set :as set]))

(defn show [db reporter stats group-id artifact-id]
  (if-let [artifact (db/find-jar db group-id artifact-id)]
    (auth/try-account
     (view/show-jar db
                    reporter
                    stats
                    account
                    artifact
                    (db/recent-versions db group-id artifact-id 5)
                    (db/count-versions db group-id artifact-id)))))

(defn list-versions [db group-id artifact-id]
  (if-let [artifact (db/find-jar db group-id artifact-id)]
    (auth/try-account
     (view/show-versions account
                         artifact
                         (db/recent-versions db group-id artifact-id)))))

(defn show-version [db reporter stats group-id artifact-id version]
  (if-let [artifact (db/find-jar db group-id artifact-id version)]
    (auth/try-account
     (view/show-jar db
                    reporter
                    stats
                    account
                    artifact
                    (db/recent-versions db group-id artifact-id 5)
                    (db/count-versions db group-id artifact-id)))))

(defn response-based-on-format
  "render appropriate response based on the file type suffix provided:
  JSON or SVG"
  [db file-format artifact-id & [group-id]]
  (let [group-id (or group-id artifact-id)]
  (cond
    (= file-format "json") (-> (response/response (view/make-latest-version-json db group-id artifact-id))
                               (response/header "Cache-Control" "no-cache")
                               (response/content-type "application/json; charset=UTF-8")
                               (response/header "Access-Control-Allow-Origin" "*"))
    (= file-format "svg") (-> (response/response (view/make-latest-version-svg db group-id artifact-id))
                              (response/header "Cache-Control" "no-cache")
                              (response/content-type "image/svg+xml")
                              (response/header "Access-Control-Allow-Origin" "*")))))

(defn routes [db reporter stats]
  (compojure/routes
   (GET ["/:artifact-id", :artifact-id #"[^/]+"] [artifact-id]
        (show db reporter stats artifact-id artifact-id))
   (GET ["/:group-id/:artifact-id", :group-id #"[^/]+" :artifact-id #"[^/]+"]
        [group-id artifact-id]
        (show db reporter stats group-id artifact-id))

   (GET ["/:artifact-id/versions" :artifact-id #"[^/]+"] [artifact-id]
        (list-versions db artifact-id artifact-id))
   (GET ["/:group-id/:artifact-id/versions"
         :group-id #"[^/]+" :artifact-id #"[^/]+"]
        [group-id artifact-id]
{'fraction_non_alphanumeric': 0.1397738951695786, 'fraction_numerical': 0.0010277492291880781, 'mean_word_length': 1.8692874692874692, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3080418', 'n_tokens_mistral': 1834, 'n_tokens_neox': 1701, 'n_words': 410}