Line data Source code
1 4 : (ns matchscore.media.service
2 : "Upload, signing and cleanup orchestration. Storage is injectable so tests use
3 : the in-memory fake without credentials or network access."
4 : (:require
5 : [clojure.string :as str]
6 : [matchscore.media.db :as media-db]
7 : [matchscore.media.image :as image]
8 : [matchscore.media.storage :as storage]
9 : [matchscore.utils.uuid :as uuid-utils]
10 : [next.jdbc :as jdbc])
11 : (:import
12 : (java.time Instant)))
13 :
14 :
15 8 : (def signed-url-seconds 300)
16 :
17 :
18 36 : (defonce ^:private storage-cache (atom {}))
19 :
20 :
21 4 : (defn uploads-enabled?
22 : [config]
23 24 : (true? (get-in config [:media :uploads-enabled?])))
24 :
25 :
26 4 : (defn ensure-gallery-mutations-enabled!
27 : "Rejects client-controlled gallery changes while keeping reads and
28 : server-side template snapshot inheritance available."
29 : [config]
30 17 : (when-not (uploads-enabled? config)
31 14 : (throw (ex-info "Media gallery changes are disabled" {:type :media/unavailable}))))
32 :
33 :
34 4 : (defn storage-for
35 : [{:keys [config media-storage]}]
36 10 : (or
37 2 : media-storage
38 : (let [{:keys [bucket region]
39 : :as storage-config}
40 12 : (get-in config [:media :storage])]
41 20 : (when (and (not (str/blank? bucket)) (not (str/blank? region)))
42 0 : (or
43 0 : (get @storage-cache storage-config)
44 0 : (let [factory (requiring-resolve 'matchscore.media.storage.s3/make-s3-storage)
45 0 : instance (factory storage-config)]
46 0 : (get
47 0 : (swap! storage-cache
48 0 : #(if (contains? % storage-config) % (assoc % storage-config instance)))
49 0 : storage-config)))))))
50 :
51 :
52 4 : (defn image-view
53 : [store asset]
54 10 : (let [expires-at (.plusSeconds (Instant/now) signed-url-seconds)]
55 28 : {:id (str (:uuid asset))
56 20 : :url (when store (storage/sign-get-url store (:image-key asset) expires-at))
57 20 : :thumbnail-url (when store (storage/sign-get-url store (:thumbnail-key asset) expires-at))
58 6 : :width (:width asset)
59 6 : :height (:height asset)
60 16 : :focus-x (some->
61 6 : (:focus-x asset)
62 : double)
63 16 : :focus-y (some->
64 6 : (:focus-y asset)
65 : double)
66 6 : :expires-at (str expires-at)}))
67 :
68 :
69 4 : (defn galleries-for
70 : [ctx entity-type entity-ids]
71 16 : (let [raw (media-db/galleries-by-entity-ids (:pg ctx) entity-type entity-ids)
72 16 : store (when (seq raw) (storage-for ctx))]
73 4 : (into
74 2 : {}
75 4 : (map
76 2 : (fn [[entity-id assets]]
77 20 : [entity-id (mapv #(image-view store %) assets)]))
78 2 : raw)))
79 :
80 :
81 4 : (defn with-gallery
82 : [ctx entity-type entity]
83 6 : (when entity
84 32 : (let [images (get (galleries-for ctx entity-type [(:uuid entity)]) (str (:uuid entity)) [])]
85 12 : (assoc entity
86 2 : :images images
87 6 : :cover-image (first images)
88 14 : :images-version (or (:images-version entity) 0)))))
89 :
90 :
91 4 : (defn upload!
92 : "Reserves quota, normalizes one file, stores both derivatives, and marks the
93 : asset ready. Any partial storage failure is best-effort deleted and retained
94 : as a retryable lifecycle row."
95 : [{:keys [pg config]
96 : :as ctx}
97 : uploader-uuid
98 : resource-type
99 : resource-uuid
100 : bytes]
101 6 : (when-not (uploads-enabled? config)
102 7 : (throw (ex-info "Media uploads are disabled" {:type :media/unavailable})))
103 8 : (when-not (contains? #{"event" "template"} resource-type)
104 0 : (throw (ex-info "Invalid upload scope" {:type :media/invalid-scope})))
105 15 : (when (or (nil? uploader-uuid) (not (uuid-utils/uuid-string? (str uploader-uuid))))
106 0 : (throw (ex-info "Authentication required" {:type :media/unauthorized})))
107 3 : (let [asset-id (uuid-utils/generate-uuid7-str)
108 3 : image-key (storage/random-key "image")
109 3 : thumbnail-key (storage/random-key "thumbnail")
110 2 : byte-size (alength ^bytes bytes)
111 4 : store (or
112 3 : (storage-for ctx)
113 : (throw
114 0 : (ex-info "Media storage is unavailable" {:type :media/storage-unavailable})))
115 9 : reservation {:uuid asset-id
116 1 : :uploader-uuid uploader-uuid
117 1 : :resource-type resource-type
118 1 : :resource-uuid resource-uuid
119 1 : :image-key image-key
120 1 : :thumbnail-key thumbnail-key
121 1 : :byte-size byte-size}
122 4 : reserve! (or
123 3 : (:media-reserve-upload! ctx)
124 0 : #(jdbc/with-transaction [tx pg] (media-db/reserve-upload! tx %)))
125 7 : ready! (or (:media-mark-ready! ctx) #(media-db/mark-ready! pg %1 %2))
126 7 : failed! (or (:media-mark-failed! ctx) #(media-db/mark-storage-failure! pg %1 %2))]
127 3 : (reserve! reservation)
128 1 : (try
129 4 : (let [normalized (image/validate-and-normalize bytes)]
130 1 : (try
131 8 : (storage/put-object! store image-key "image/jpeg" (:image-bytes normalized))
132 8 : (storage/put-object! store thumbnail-key "image/jpeg" (:thumbnail-bytes normalized))
133 3 : (let [asset (ready!
134 1 : asset-id
135 5 : {:mime "image/jpeg"
136 3 : :width (:width normalized)
137 3 : :height (:height normalized)})]
138 4 : (image-view store asset))
139 : (catch Throwable storage-error
140 28 : (doseq [key [image-key thumbnail-key]]
141 1 : (try
142 4 : (storage/delete-object! store key)
143 : (catch Throwable _)))
144 4 : (failed! asset-id (.getMessage storage-error))
145 1 : (throw
146 7 : (ex-info "Media storage failed" {:type :media/storage-unavailable} storage-error)))))
147 : (catch clojure.lang.ExceptionInfo e
148 9 : (when-not (= :media/storage-unavailable (:type (ex-data e)))
149 0 : (failed! asset-id (.getMessage e)))
150 2 : (throw e)))))
151 :
152 :
153 4 : (defn cleanup-once!
154 0 : ([ctx] (cleanup-once! ctx 100))
155 : ([{:keys [pg]
156 : :as ctx}
157 : limit]
158 0 : (let [assets (jdbc/with-transaction [tx pg] (media-db/claim-cleanup-batch! tx limit))
159 0 : store (when (seq assets)
160 0 : (or
161 0 : (storage-for ctx)
162 : (throw
163 0 : (ex-info
164 : "Media storage unavailable for cleanup"
165 0 : {:type :media/storage-unavailable}))))]
166 0 : (doseq [asset assets]
167 0 : (try
168 0 : (when store
169 0 : (storage/delete-object! store (:image-key asset))
170 0 : (storage/delete-object! store (:thumbnail-key asset)))
171 0 : (media-db/finish-delete! pg (:uuid asset))
172 0 : (catch Throwable e (media-db/fail-delete! pg (:uuid asset) (.getMessage e)))))
173 0 : {:claimed (count assets)})))
|