<commit_before>(ns vignette.api.legacy.routes
  (:require [clout.core :refer (route-compile route-matches)]))

(declare route->revision
         route->dimensions
         route->thumb-mode
         route->options
         add-request-type)

;29 {
;30    "width" : "185px",
;31    "archive" : "",
;32    "wikia" : "happywheels",
;33    "input" : "/happywheels/images/thumb/b/bb/SuperMario64_20.png/185px-SuperMario64_20.webp",
;34    "hook_name" : "image thumbnailer",
;35    "thumbpath" : "happywheels/images/thumb/b/bb/SuperMario64_20.png",
;36    "thumbname" : "185px-SuperMario64_20.webp",
;37    "filename" : "SuperMario64_20",
;38    "thumbext" : "SuperMario64_20",
;39    "type" : "images",
;40    "fileext" : "png"
;41 }
; qr{ \/((?!\w\/)?(.+)\/(images|avatars)\/thumb((?!\/archive).*|\/archive)?\/\w\/\w{2}\/(.+)\.(jpg|jpeg|png|gif{1,}))\/((\d+px|\d+x\d+|\d+x\d+x\d+|)\-(.*)\.(jpg|jpeg|jpe|png|gif|webp))(\?.*)?$ }xi,
(def thumbnail-route
  (route-compile "/:wikia:lang/:image-type/thumb:archive/:top-dir/:middle-dir/:original/:thumbname"
                 {:wikia #"[\w-]+"
                  :lang #".*"
                  :image-type #"images|avatars"
                  :archive #"(?!\/archive).*|\/archive"
                  :top-dir #"\w"
                  :middle-dir #"\w\w"
                  :original #"[^/]*"
                  :thumbname #".*"}))

(def original-route
  (route-compile "/:wikia:lang/images:archive/:top-dir/:middle-dir/:original"
                 {:wikia #"[\w-]+"
                  :lang #".*"
                  :archive #"(?!\/archive).*|\/archive"
                  :top-dir #"\w"
                  :middle-dir #"\w\w"
                  :original #".*"}))

(defn route->thumb-map
  [route-params]
  (let [map (-> route-params
                (add-request-type :thumbnail)
                (assoc :thumbnail-mode "thumbnail")
                (route->dimensions)
                (route->revision)
                (route->options))]
    map))

(defn route->original-map
  [route-params]
  (let [map (-> route-params
                (add-request-type :original)
                (route->revision)
                (route->options))]
    map))

(defn add-request-type
  [m request-type]
  {:pre [(map? m)]
   :post [(map? %)]}
  (merge m {:request-type request-type}))

(defn route->revision
  [map]
  (let [revision (if (and (not= (:archive map) "")
                          (re-matches #"^\d+!.*" (:original map)))
                   (re-find #"^\d+" (:original map))
                   "latest")
        map (assoc map :revision revision)]
    (if (= revision "latest")
      map
      (assoc map :original (clojure.string/replace (:original map) #"^\d+!" "")))))

(defn route->options
  [map]
  (let [[_ format] (re-find #"\.([a-z]+)$" (get map :thumbname ""))
        [_ lang] (re-find #"^/([a-z]+)$" (get map :lang ""))]
    (assoc map :options {:format format
                         :lang lang})))

(defmulti route->dimensions :request-type)

(defmethod route->dimensions :thumbnail
  [route]
  "Add the :width field to a request map based on the legacy parsing methods."
  (if-let [thumb-name (:thumbname route)]
    (if-let [[_ dimension] (re-find #"^(\d+)px-" thumb-name)]
      (merge route {:width dimension
                    :height dimension})
      (if-let [[_ width height] (re-find #"^(\d+)x(\d+)-" thumb-name)]
        (merge route {:width width
                    :height height
                    :thumbnail-mode "fixed-aspect-ratio"})
        (if-let [[_ width height _] (re-find #"^(\d+)x(\d+)x(\d+)-" thumb-name)]
          (merge route {:width width
                        :height height
                        :thumbnail-mode "zoom-crop"})
          route)))
    route))

(defmethod route->dimensions nil
  [map]
  map)

(defn request-map->thumbpath
  [m]
  (format "%s/%s/%s/%s/%s/%s"
          (:wikia m)
          (:image-type m)
          (:uri-request-type m)
          (:top-dir m)
          (:middle-dir m)
          (:original m)))

(defn request-map-add-thumbpath
  [m]
  (assoc m :thumbpath (request-map->thumbpath m)))

;;;;;

(defmulti request-map->dimensions :request-type)

(defmethod request-map->dimensions :image-thumbnail
  [m])


(defn remove-extension
  "Remove the extension from a filename."
  [filename]
  (clojure.string/replace filename #"\.\w+$" ""))

(declare seq-of-strings->seq-of-integers)

(def width-px-regex #"^(\d+)px$")
(def width-x-height-regex #"^(\d+)x(\d+)$")
(def width-x-height-x-scale-regex #"^(\d+)x(\d+)x(\d+)$")
;(def color-width-regex #"^v,([0-9a-f]{6},)?(\d+)px$")

; these probably need to be dispatched based on the type
; e.g. image thumbnailer, SVG thumbnailer, etc

(defn thumbnail-path->dimensions
  "Given a thumb path, extract the dimensions.
  For example, from \"130px-102,382,0,247-Brain_Attack.PNG\", it will
  extract the \"130px\" and decode it to a width.

  The return value is a vector with the type of dimension matched followed by
  a sequence with the extracted dimensions.

  Note that the current implementation favors clarity over efficiency. We end up with, in the worst
  case, 5 calls to `re-find` and in the best case 2.

  Source: backend:lib/Wikia/Thumbnailer/File.pm
  See has thumb_size declaration.

  TODO: Defaults and config checks that are currently used in File.pm?
  "
  [thumb-path]
  (let [matches (re-find #"(?ix)(\d+px|\d+x\d+|\d+x\d+x\d+|)\-(.*)\.(jpg|jpeg|jpe|png|gif|webp)" thumb-path)]
    (when-let [[_ width _ _] matches]
      (cond
        (empty? width) nil
        (re-find width-px-regex width) [:width (seq-of-strings->seq-of-integers (rest (re-find width-px-regex width)))]
        (re-find width-x-height-regex width) [:width-height (seq-of-strings->seq-of-integers (rest (re-find width-x-height-regex width)))]
        (re-find width-x-height-x-scale-regex width) [:width-height-scale (seq-of-strings->seq-of-integers (rest (re-find width-x-height-x-scale-regex width)))]
        ; doesn't belong here-- goes with images without extensions
        ;(re-find color-width-regex width) [:width-color (let [[_ color w] (re-find color-width-regex width)]
                                                       ;(list color (Integer. w)))]
        :else [:default (seq-of-strings->seq-of-integers (list width))]))))


(defn thumbnail-path->coordinates
  "Given a thumbnail path, extract the centering coordinates.
  For example, from \"130px-102,382,0,247-Brain_Attack.PNG\", it will
  extract the \"102,382,0,247\" and decode it to a set of coordinates.
  
  Source: backend:lib/Wikia/Thumbnailer/File.pm
  See the 'has' thumb_name of thumb_path.
  "
  [thumb-path]
  (let [matches (re-find #"(\d+),(\d+),(\d+),(\d+)" thumb-path)]
    (when-let [[_ & coordinates] matches]
      (zipmap [:x1 :x2 :y1 :y2] (map #(Integer. %) coordinates)))))

(defn seq-of-strings->seq-of-integers
  [s]
  (map #(Integer. %) s))<commit_msg>clean up some unused code<commit_after>(ns vignette.api.legacy.routes
  (:require [clout.core :refer (route-compile route-matches)]))

(declare route->revision
         route->dimensions
         route->thumb-mode
         route->options)

(def thumbnail-route
  (route-compile "/:wikia:lang/:image-type/thumb:archive/:top-dir/:middle-dir/:original/:thumbname"
                 {:wikia #"[\w-]+"
                  :lang #".*"
                  :image-type #"images|avatars"
                  :archive #"(?!\/archive).*|\/archive"
                  :top-dir #"\w"
                  :middle-dir #"\w\w"
                  :original #"[^/]*"
                  :thumbname #".*"}))

(def original-route
  (route-compile "/:wikia:lang/images:archive/:top-dir/:middle-dir/:original"
                 {:wikia #"[\w-]+"
                  :lang #".*"
                  :archive #"(?!\/archive).*|\/archive"
                  :top-dir #"\w"
                  :middle-dir #"\w\w"
                  :original #".*"}))

(defn route->thumb-map
  [route-params]
  (let [map (-> route-params
                (assoc :request-type :thumbnail)
                (assoc :thumbnail-mode "thumbnail")
                (route->dimensions)
                (route->revision)
                (route->options))]
    map))

(defn route->original-map
  [route-params]
  (let [map (-> route-params
                (assoc :request-type :original)
                (route->revision)
                (route->options))]
    map))

(defn route->revision
  [map]
  (let [revision (if (and (not= (:archive map) "")
                          (re-matches #"^\d+!.*" (:original map)))
                   (re-find #"^\d+" (:original map))
                   "latest")
        map (assoc map :revision revision)]
    (if (= revision "latest")
      map
      (assoc map :original (clojure.string/replace (:original map) #"^\d+!" "")))))

(defn route->options
  [map]
  (let [[_ format] (re-find #"\.([a-z]+)$" (get map :thumbname ""))
        [_ lang] (re-find #"^/([a-z]+)$" (get map :lang ""))]
    (assoc map :options {:format format
                         :lang lang})))

(defn route->dimensions
  [route]
  "Add the :width field to a request map based on the legacy parsing methods."
  (if-let [thumb-name (:thumbname route)]
    (if-let [[_ dimension] (re-find #"^(\d+)px-" thumb-name)]
      (merge route {:width dimension
                    :height dimension})
      (if-let [[_ width height] (re-find #"^(\d+)x(\d+)-" thumb-name)]
        (merge route {:width width
                    :height height
                    :thumbnail-mode "fixed-aspect-ratio"})
        (if-let [[_ width height _] (re-find #"^(\d+)x(\d+)x(\d+)-" thumb-name)]
          (merge route {:width width
                        :height height
                        :thumbnail-mode "zoom-crop"})
          route)))
    route))
{'fraction_non_alphanumeric': 0.1886734379809172, 'fraction_numerical': 0.011182928080435006, 'mean_word_length': 2.135413316178836, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6796256', 'n_tokens_mistral': 3470, 'n_tokens_neox': 3245, 'n_words': 731}