mirror of
https://github.com/penpot/penpot.git
synced 2025-02-10 17:18:21 -05:00
🐛 Adapt trim-file task to page/file data model changes.
This commit is contained in:
parent
c21dc7ea7f
commit
2bd89c94d9
3 changed files with 33 additions and 26 deletions
|
@ -48,7 +48,7 @@
|
||||||
;; modification in order to make the file ellegible for
|
;; modification in order to make the file ellegible for
|
||||||
;; trimming. The value only supports s(econds) m(inutes) and
|
;; trimming. The value only supports s(econds) m(inutes) and
|
||||||
;; h(ours) as time unit.
|
;; h(ours) as time unit.
|
||||||
:file-trimming-max-age "72h"
|
:file-trimming-threshold "72h"
|
||||||
|
|
||||||
;; LDAP auth disabled by default. Set ldap-auth-host to enable
|
;; LDAP auth disabled by default. Set ldap-auth-host to enable
|
||||||
;:ldap-auth-host "ldap.mysupercompany.com"
|
;:ldap-auth-host "ldap.mysupercompany.com"
|
||||||
|
@ -92,7 +92,9 @@
|
||||||
(s/def ::debug-humanize-transit ::us/boolean)
|
(s/def ::debug-humanize-transit ::us/boolean)
|
||||||
(s/def ::public-uri ::us/string)
|
(s/def ::public-uri ::us/string)
|
||||||
(s/def ::backend-uri ::us/string)
|
(s/def ::backend-uri ::us/string)
|
||||||
|
|
||||||
(s/def ::image-process-max-threads ::us/integer)
|
(s/def ::image-process-max-threads ::us/integer)
|
||||||
|
(s/def ::file-trimming-threshold ::dt/duration)
|
||||||
|
|
||||||
(s/def ::google-client-id ::us/string)
|
(s/def ::google-client-id ::us/string)
|
||||||
(s/def ::google-client-secret ::us/string)
|
(s/def ::google-client-secret ::us/string)
|
||||||
|
@ -113,7 +115,6 @@
|
||||||
(s/def ::ldap-auth-email-attribute ::us/string)
|
(s/def ::ldap-auth-email-attribute ::us/string)
|
||||||
(s/def ::ldap-auth-fullname-attribute ::us/string)
|
(s/def ::ldap-auth-fullname-attribute ::us/string)
|
||||||
(s/def ::ldap-auth-avatar-attribute ::us/string)
|
(s/def ::ldap-auth-avatar-attribute ::us/string)
|
||||||
(s/def ::file-trimming-threshold ::dt/duration)
|
|
||||||
|
|
||||||
(s/def ::config
|
(s/def ::config
|
||||||
(s/keys :opt-un [::http-server-cors
|
(s/keys :opt-un [::http-server-cors
|
||||||
|
@ -143,7 +144,7 @@
|
||||||
::smtp-password
|
::smtp-password
|
||||||
::smtp-tls
|
::smtp-tls
|
||||||
::smtp-ssl
|
::smtp-ssl
|
||||||
::file-trimming-max-age
|
::file-trimming-threshold
|
||||||
::debug-humanize-transit
|
::debug-humanize-transit
|
||||||
::allow-demo-users
|
::allow-demo-users
|
||||||
::registration-enabled
|
::registration-enabled
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
(ns app.tasks.trim-file
|
(ns app.tasks.trim-file
|
||||||
(:require
|
(:require
|
||||||
|
[app.common.pages-migrations :as pmg]
|
||||||
[app.config :as cfg]
|
[app.config :as cfg]
|
||||||
[app.db :as db]
|
[app.db :as db]
|
||||||
[app.tasks :as tasks]
|
[app.tasks :as tasks]
|
||||||
|
@ -29,7 +30,8 @@
|
||||||
(bytes? data) (assoc :data (blob/decode data))))
|
(bytes? data) (assoc :data (blob/decode data))))
|
||||||
|
|
||||||
(def sql:retrieve-files-to-trim
|
(def sql:retrieve-files-to-trim
|
||||||
"select id from file as f
|
"select f.id, f.data
|
||||||
|
from file as f
|
||||||
where f.has_media_trimmed is false
|
where f.has_media_trimmed is false
|
||||||
and f.modified_at < now() - ?::interval
|
and f.modified_at < now() - ?::interval
|
||||||
order by f.modified_at asc
|
order by f.modified_at asc
|
||||||
|
@ -40,39 +42,42 @@
|
||||||
file is considered candidate when some time passes whith no
|
file is considered candidate when some time passes whith no
|
||||||
modification."
|
modification."
|
||||||
[conn]
|
[conn]
|
||||||
(let [interval (:file-trimming-max-age cfg/config)]
|
(let [threshold (:file-trimming-threshold cfg/config)
|
||||||
(->> (db/exec! conn [sql:retrieve-files-to-trim interval])
|
interval (db/interval threshold)]
|
||||||
(map :id))))
|
(db/exec! conn [sql:retrieve-files-to-trim interval])))
|
||||||
|
|
||||||
|
|
||||||
(def collect-media-xf
|
(def collect-media-xf
|
||||||
(comp (map :data)
|
(comp
|
||||||
(map :objects)
|
(map :objects)
|
||||||
(mapcat vals)
|
(mapcat vals)
|
||||||
(filter #(= :image (:type %)))
|
(filter #(= :image (:type %)))
|
||||||
(map :metadata)
|
(map :metadata)
|
||||||
(map :id)))
|
(map :id)))
|
||||||
|
|
||||||
|
|
||||||
(defn collect-used-media
|
(defn collect-used-media
|
||||||
[pages]
|
[data]
|
||||||
(into #{} collect-media-xf pages))
|
(-> #{}
|
||||||
|
(into collect-media-xf (vals (:pages-index data)))
|
||||||
|
(into collect-media-xf (vals (:components data)))
|
||||||
|
(into (keys (:media data)))))
|
||||||
|
|
||||||
(defn process-file
|
(defn process-file
|
||||||
[file-id]
|
[{:keys [id data] :as file}]
|
||||||
(log/debugf "Processing file: '%s'." file-id)
|
(log/debugf "Processing file: '%s'." id)
|
||||||
(db/with-atomic [conn db/pool]
|
(db/with-atomic [conn db/pool]
|
||||||
(let [mobjs (db/query conn :media-object {:file-id file-id})
|
(let [mobjs (map :id (db/query conn :media-object {:file-id id}))
|
||||||
pages (->> (db/query conn :page {:file-id file-id})
|
data (-> (blob/decode data)
|
||||||
(map decode-row))
|
(pmg/migrate-data))
|
||||||
used (into #{} collect-media-xf pages)
|
|
||||||
unused (into #{} (comp (map :id) (remove #(contains? used %))) mobjs)]
|
used (collect-used-media data)
|
||||||
|
unused (into #{} (remove #(contains? used %)) mobjs)]
|
||||||
|
|
||||||
(log/debugf "Collected media ids: '%s'." (pr-str used))
|
(log/debugf "Collected media ids: '%s'." (pr-str used))
|
||||||
(log/debugf "Unused media ids: '%s'." (pr-str unused))
|
(log/debugf "Unused media ids: '%s'." (pr-str unused))
|
||||||
|
|
||||||
(db/update! conn :file
|
(db/update! conn :file
|
||||||
{:has-media-trimmed true}
|
{:has-media-trimmed true}
|
||||||
{:id file-id})
|
{:id id})
|
||||||
|
|
||||||
(doseq [id unused]
|
(doseq [id unused]
|
||||||
;; TODO: add task batching
|
;; TODO: add task batching
|
||||||
|
|
|
@ -103,9 +103,10 @@
|
||||||
(letfn [(conformer [v]
|
(letfn [(conformer [v]
|
||||||
(cond
|
(cond
|
||||||
(duration? v) v
|
(duration? v) v
|
||||||
|
|
||||||
(string? v)
|
(string? v)
|
||||||
(try
|
(try
|
||||||
(parse-duration v)
|
(duration v)
|
||||||
(catch java.time.format.DateTimeParseException _e
|
(catch java.time.format.DateTimeParseException _e
|
||||||
::s/invalid))
|
::s/invalid))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue