<commit_before>(ns vignette.storage.static-assets
  (:require [vignette.storage.protocols :refer :all]
            [org.httpkit.client :as http]
            [vignette.util.filesystem :as fs]
            [clojure.java.io :as io]))

(defn- parse-content-disp
  [header]
  (when-let [m (if header (re-find #"(?i)filename=\"(.*)\"" header))]
    (or (second m) "")))

(defrecord AsyncResponseStoredObject [response] StoredObjectProtocol
  (file-stream [this] (-> this :response :body))
  (content-length [this] (-> this :response :headers :content-length))
  (content-type [this] (-> this :response :headers :content-type))
  (etag [this] (-> this :response :headers :etag))
  (filename [this] (-> this :response :headers :content-disposition parse-content-disp))
  (transfer! [this to]
    (with-open [in-stream (io/input-stream (file-stream this))
                out-stream (io/output-stream to)]
      (io/copy in-stream out-stream))
    (fs/file-exists? to))
  (->response-object [this] (file-stream this)))

(defn create-async-response-stored-object [static-image-get image-review-get]
  (let [static-image-response @static-image-get]
    (if (and (-> static-image-response :status (= 200)) (some #(= % (:status @image-review-get)) [200 404 400]))
      (->AsyncResponseStoredObject static-image-response))))

(defrecord StaticImageStorage [static-image-url image-review-url] ImageStorageProtocol
  (save-thumbnail [_ _ _] nil)
  (get-thumbnail [_ _] nil)
  (save-original [_ _ _] nil)

  (get-original [_ original-map]
    (if-let [uuid (:uuid original-map)]
      (create-async-response-stored-object
        (http/get (static-image-url uuid) {:as :stream})
        (http/get (image-review-url uuid (get-in original-map [:options :status]))))))

  (original-exists? [_ image-map] nil
    (if-let [uuid (:uuid image-map)]
      (let [static-image-response (http/head (static-image-url uuid)) image-review-response (http/head (image-review-url uuid))]
        (and (-> @static-image-response :status (= 200))
             (-> @image-review-response :status (some-fn #(or (= % 200) (= % 404)))))))))

(defn create-static-image-storage [static-image-url image-review-url]
  (->StaticImageStorage static-image-url image-review-url))
<commit_msg>SER-532 Vignette will return an image if image-review returns 500 status.<commit_after>(ns vignette.storage.static-assets
  (:require [vignette.storage.protocols :refer :all]
            [org.httpkit.client :as http]
            [vignette.util.filesystem :as fs]
            [clojure.java.io :as io]))

(defn- parse-content-disp
  [header]
  (when-let [m (if header (re-find #"(?i)filename=\"(.*)\"" header))]
    (or (second m) "")))

(defrecord AsyncResponseStoredObject [response] StoredObjectProtocol
  (file-stream [this] (-> this :response :body))
  (content-length [this] (-> this :response :headers :content-length))
  (content-type [this] (-> this :response :headers :content-type))
  (etag [this] (-> this :response :headers :etag))
  (filename [this] (-> this :response :headers :content-disposition parse-content-disp))
  (transfer! [this to]
    (with-open [in-stream (io/input-stream (file-stream this))
                out-stream (io/output-stream to)]
      (io/copy in-stream out-stream))
    (fs/file-exists? to))
  (->response-object [this] (file-stream this)))

(defn create-async-response-stored-object [static-image-get image-review-get]
  (let [static-image-response @static-image-get]
    (if (and (-> static-image-response :status (= 200)) (some #(= % (:status @image-review-get)) [200 404 400 500]))
      (->AsyncResponseStoredObject static-image-response))))

(defrecord StaticImageStorage [static-image-url image-review-url] ImageStorageProtocol
  (save-thumbnail [_ _ _] nil)
  (get-thumbnail [_ _] nil)
  (save-original [_ _ _] nil)

  (get-original [_ original-map]
    (if-let [uuid (:uuid original-map)]
      (create-async-response-stored-object
        (http/get (static-image-url uuid) {:as :stream})
        (http/get (image-review-url uuid (get-in original-map [:options :status]))))))

  (original-exists? [_ image-map] nil
    (if-let [uuid (:uuid image-map)]
      (let [static-image-response (http/head (static-image-url uuid)) image-review-response (http/head (image-review-url uuid))]
        (and (-> @static-image-response :status (= 200))
             (-> @image-review-response :status (some-fn #(or (= % 200) (= % 404)))))))))

(defn create-static-image-storage [static-image-url image-review-url]
  (->StaticImageStorage static-image-url image-review-url))

{'fraction_non_alphanumeric': 0.1750878734622144, 'fraction_numerical': 0.011203866432337435, 'mean_word_length': 3.53938185443669, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '5986773', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1498, 'n_words': 341}