diff --git a/backend/src/app/http/assets.clj b/backend/src/app/http/assets.clj index 3de6eca36..bb3e9a48e 100644 --- a/backend/src/app/http/assets.clj +++ b/backend/src/app/http/assets.clj @@ -45,7 +45,7 @@ mobj))) (defn- serve-object - "Helper function that returns the appropriate responde depending on + "Helper function that returns the appropriate response depending on the storage object backend type." [{:keys [storage] :as cfg} obj] (let [mdata (meta obj) diff --git a/backend/src/app/rpc/mutations/profile.clj b/backend/src/app/rpc/mutations/profile.clj index 876021154..7a016d992 100644 --- a/backend/src/app/rpc/mutations/profile.clj +++ b/backend/src/app/rpc/mutations/profile.clj @@ -25,6 +25,7 @@ [buddy.hashers :as hashers] [clojure.spec.alpha :as s] [cuerdas.core :as str] + [promesa.core :as p] [promesa.exec :as px])) ;; --- Helpers & Specs @@ -415,32 +416,35 @@ ;; TODO: properly handle resource usage, transactions and storage (sv/defmethod ::update-profile-photo - {::rlimit/permits (cf/get :rlimit-image)} - [{:keys [pool storage executors] :as cfg} {:keys [profile-id file] :as params}] - + [cfg {:keys [file] :as params}] ;; Validate incoming mime type (media/validate-media-type! (:content-type file) #{"image/jpeg" "image/png" "image/webp"}) + (let [cfg (update cfg :storage media/configure-assets-storage)] + (update-profile-photo cfg params))) - ;; Perform file validation - @(px/with-dispatch (:blocking executors) - (media/run {:cmd :info :input {:path (:tempfile file) :mtype (:content-type file)}})) +(defn update-profile-photo + [{:keys [pool storage executors] :as cfg} {:keys [profile-id file] :as params}] + (p/do + ;; Perform file validation, this operation executes some + ;; comandline helpers for true check of the image file. And it + ;; raises an exception if somethig is wrong with the file. + (px/with-dispatch (:blocking executors) + (media/run {:cmd :info :input {:path (:tempfile file) :mtype (:content-type file)}})) - (db/with-atomic [conn pool] - (let [profile (db/get-by-id conn :profile profile-id) - cfg (update cfg :storage media/configure-assets-storage conn) - photo @(teams/upload-photo cfg params)] + (p/let [profile (px/with-dispatch (:default executors) + (db/get-by-id pool :profile profile-id)) + photo (teams/upload-photo cfg params)] ;; Schedule deletion of old photo (when-let [id (:photo-id profile)] - @(sto/touch-object! storage id)) + (sto/touch-object! storage id)) ;; Save new photo - (db/update! conn :profile + (db/update! pool :profile {:photo-id (:id photo)} {:id profile-id}) nil))) - ;; --- MUTATION: Request Email Change (declare request-email-change) diff --git a/backend/src/app/rpc/mutations/teams.clj b/backend/src/app/rpc/mutations/teams.clj index 26e6decd8..6380b7d28 100644 --- a/backend/src/app/rpc/mutations/teams.clj +++ b/backend/src/app/rpc/mutations/teams.clj @@ -10,7 +10,6 @@ [app.common.exceptions :as ex] [app.common.spec :as us] [app.common.uuid :as uuid] - [app.config :as cf] [app.db :as db] [app.emails :as eml] [app.media :as media] @@ -18,7 +17,6 @@ [app.rpc.permissions :as perms] [app.rpc.queries.profile :as profile] [app.rpc.queries.teams :as teams] - [app.rpc.rlimit :as rlimit] [app.storage :as sto] [app.util.services :as sv] [app.util.time :as dt] @@ -280,7 +278,8 @@ ;; --- Mutation: Update Team Photo -(declare upload-photo) +(declare ^:private upload-photo) +(declare ^:private update-team-photo) (s/def ::content-type ::media/image-content-type) (s/def ::file (s/and ::media/upload (s/keys :req-un [::content-type]))) @@ -289,29 +288,32 @@ (s/keys :req-un [::profile-id ::team-id ::file])) (sv/defmethod ::update-team-photo - {::rlimit/permits (cf/get :rlimit-image)} - [{:keys [pool storage executors] :as cfg} {:keys [profile-id file team-id] :as params}] - + [cfg {:keys [file] :as params}] ;; Validate incoming mime type (media/validate-media-type! (:content-type file) #{"image/jpeg" "image/png" "image/webp"}) + (let [cfg (update cfg :storage media/configure-assets-storage)] + (update-team-photo cfg params))) - ;; Perform file validation - @(px/with-dispatch (:blocking executors) - (media/run {:cmd :info :input {:path (:tempfile file) :mtype (:content-type file)}})) +(defn update-team-photo + [{:keys [pool storage executors] :as cfg} {:keys [profile-id file team-id] :as params}] + (p/do + ;; Perform file validation, this operation executes some + ;; comandline helpers for true check of the image file. And it + ;; raises an exception if somethig is wrong with the file. + (px/with-dispatch (:blocking executors) + (media/run {:cmd :info :input {:path (:tempfile file) :mtype (:content-type file)}})) - (db/with-atomic [conn pool] - (teams/check-edition-permissions! conn profile-id team-id) - (let [team (teams/retrieve-team conn profile-id team-id) - cfg (update cfg :storage media/configure-assets-storage conn) - photo @(upload-photo cfg params)] + (p/let [team (px/with-dispatch (:default executors) + (teams/retrieve-team pool profile-id team-id)) + photo (upload-photo cfg params)] ;; Mark object as touched for make it ellegible for tentative ;; garbage collection. (when-let [id (:photo-id team)] - @(sto/touch-object! storage id)) + (sto/touch-object! storage id)) ;; Save new photo - (db/update! conn :team + (db/update! pool :team {:photo-id (:id photo)} {:id team-id})