0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-02 20:39:09 -05:00

🐛 Fix image upload internal error.

This commit is contained in:
Andrey Antukh 2021-02-04 11:28:53 +01:00 committed by Hirunatan
parent cb23c8b093
commit ec2683417f
4 changed files with 60 additions and 43 deletions

View file

@ -13,6 +13,7 @@
[app.common.media :as cm] [app.common.media :as cm]
[app.common.spec :as us] [app.common.spec :as us]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
[app.common.exceptions :as ex]
[app.main.data.messages :as dm] [app.main.data.messages :as dm]
[app.main.repo :as rp] [app.main.repo :as rp]
[app.main.store :as st] [app.main.store :as st]
@ -50,9 +51,13 @@
;; Check that a file obtained with the file javascript API is valid. ;; Check that a file obtained with the file javascript API is valid.
[file] [file]
(when (> (.-size file) cm/max-file-size) (when (> (.-size file) cm/max-file-size)
(throw (ex-info (tr "errors.media-too-large") {}))) (ex/raise :type :validation
:code :media-too-large
:hint (str/fmt "media size is large than 5mb (size: %s)" (.-size file))))
(when-not (contains? cm/valid-media-types (.-type file)) (when-not (contains? cm/valid-media-types (.-type file))
(throw (ex-info (tr "errors.media-format-unsupported") {}))) (ex/raise :type :validation
:code :media-type-not-allowed
:hint (str/fmt "media type %s is not supported" (.-type file))))
file) file)
(defn notify-start-loading (defn notify-start-loading

View file

@ -1289,7 +1289,6 @@
;; Clipboard ;; Clipboard
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn copy-selected (defn copy-selected
[] []
(letfn [;; Retrieve all ids of selected shapes with corresponding (letfn [;; Retrieve all ids of selected shapes with corresponding
@ -1408,12 +1407,22 @@
(let [paste-data (wapi/read-from-paste-event event) (let [paste-data (wapi/read-from-paste-event event)
image-data (wapi/extract-images paste-data) image-data (wapi/extract-images paste-data)
text-data (wapi/extract-text paste-data) text-data (wapi/extract-text paste-data)
decoded-data (and (t/transit? text-data) (t/decode text-data))] decoded-data (and (t/transit? text-data)
(t/decode text-data))]
(cond (cond
(seq image-data) (rx/from (map paste-image image-data)) (seq image-data)
decoded-data (rx/of (paste-shape decoded-data in-viewport?)) (rx/from (map paste-image image-data))
(string? text-data) (rx/of (paste-text text-data))
:else (rx/empty))) (coll? decoded-data)
(->> (rx/of decoded-data)
(rx/filter #(= :copied-shapes (:type %)))
(rx/map #(paste-shape % in-viewport?)))
(string? text-data)
(rx/of (paste-text text-data))
:else
(rx/empty)))
(catch :default err (catch :default err
(js/console.error "Clipboard error:" err)))))) (js/console.error "Clipboard error:" err))))))

View file

@ -9,9 +9,8 @@
(ns app.main.data.workspace.persistence (ns app.main.data.workspace.persistence
(:require (:require
[cuerdas.core :as str]
[app.util.http :as http]
[app.common.data :as d] [app.common.data :as d]
[app.common.exceptions :as ex]
[app.common.geom.point :as gpt] [app.common.geom.point :as gpt]
[app.common.media :as cm] [app.common.media :as cm]
[app.common.pages :as cp] [app.common.pages :as cp]
@ -21,21 +20,22 @@
[app.main.data.media :as di] [app.main.data.media :as di]
[app.main.data.messages :as dm] [app.main.data.messages :as dm]
[app.main.data.workspace.common :as dwc] [app.main.data.workspace.common :as dwc]
[app.main.data.workspace.svg-upload :as svg]
[app.main.data.workspace.libraries :as dwl] [app.main.data.workspace.libraries :as dwl]
[app.main.data.workspace.selection :as dws] [app.main.data.workspace.selection :as dws]
[app.main.data.workspace.svg-upload :as svg]
[app.main.repo :as rp] [app.main.repo :as rp]
[app.main.store :as st] [app.main.store :as st]
[app.util.avatars :as avatars]
[app.util.http :as http]
[app.util.i18n :as i18n :refer [tr]] [app.util.i18n :as i18n :refer [tr]]
[app.util.object :as obj] [app.util.object :as obj]
[app.util.router :as rt] [app.util.router :as rt]
[app.util.time :as dt] [app.util.time :as dt]
[app.util.transit :as t] [app.util.transit :as t]
[app.util.avatars :as avatars]
[beicon.core :as rx] [beicon.core :as rx]
[cljs.spec.alpha :as s] [cljs.spec.alpha :as s]
[potok.core :as ptk] [cuerdas.core :as str]
[app.main.store :as st])) [potok.core :as ptk]))
(declare persist-changes) (declare persist-changes)
(declare persist-sychronous-changes) (declare persist-sychronous-changes)
@ -417,24 +417,27 @@
(defn- handle-upload-error [on-error stream] (defn- handle-upload-error [on-error stream]
(->> stream (->> stream
(rx/catch (rx/catch
(fn [error] (fn on-error [error]
(cond (if (ex/ex-info? error)
(= (:code error) :media-type-not-allowed) (on-error (ex-data error))
(rx/of (dm/error (tr "errors.media-type-not-allowed"))) (cond
(= (:code error) :media-type-not-allowed)
(rx/of (dm/error (tr "errors.media-type-not-allowed")))
(= (:code error) :media-type-mismatch) (= (:code error) :media-too-large)
(rx/of (dm/error (tr "errors.media-type-mismatch"))) (rx/of (dm/error (tr "errors.media-too-large")))
(= (:code error) :unable-to-optimize) (= (:code error) :media-type-mismatch)
(rx/of (dm/error (:hint error))) (rx/of (dm/error (tr "errors.media-type-mismatch")))
(fn? on-error) (= (:code error) :unable-to-optimize)
(do (rx/of (dm/error (:hint error)))
(fn? on-error)
(on-error error) (on-error error)
(rx/empty))
:else :else
(rx/throw error)))))) (rx/throw error)))))))
(defn- upload-uris [file-id local? name uris mtype on-image on-svg] (defn- upload-uris [file-id local? name uris mtype on-image on-svg]
(letfn [(svg-url? [url] (letfn [(svg-url? [url]
@ -499,7 +502,6 @@
(let [{:keys [on-image on-svg on-error] (let [{:keys [on-image on-svg on-error]
:or {on-image identity :or {on-image identity
on-svg identity}} (meta params)] on-svg identity}} (meta params)]
(rx/concat (rx/concat
(rx/of (dm/show {:content (tr "media.loading") (rx/of (dm/show {:content (tr "media.loading")
:type :info :type :info
@ -515,7 +517,8 @@
(handle-upload-error on-error) (handle-upload-error on-error)
(rx/finalize (st/emitf (dm/hide-tag :media-loading))))))))) (rx/finalize (st/emitf (dm/hide-tag :media-loading)))))))))
(defn upload-media-asset [params] (defn upload-media-asset
[params]
(let [params (-> params (let [params (-> params
(assoc :svg-as-images true) (assoc :svg-as-images true)
(assoc :local? false) (assoc :local? false)
@ -525,13 +528,12 @@
(defn upload-media-workspace (defn upload-media-workspace
[params position] [params position]
(let [{:keys [x y]} position (let [{:keys [x y]} position
params (-> params mdata {:on-image #(st/emit! (dwc/image-uploaded % x y))
(assoc :local? true) :on-svg #(st/emit! (svg/svg-uploaded % x y))}
(with-meta
{:on-image params (-> (assoc params :local? true)
#(st/emit! (dwc/image-uploaded % x y)) (with-meta mdata))]
:on-svg
#(st/emit! (svg/svg-uploaded % x y))}))]
(upload-media-objects params))) (upload-media-objects params)))

View file

@ -229,13 +229,14 @@
:timeout 3000}))) :timeout 3000})))
;; Print to the console some debug info. ;; Print to the console some debug info.
(js/console.group "Server Error") (js/console.group "Validation Error")
(js/console.info (ex/ignoring
(with-out-str (js/console.info
(pprint (dissoc error :explain)))) (with-out-str
(when-let [explain (:explain error)] (pprint (dissoc error :explain))))
(js/console.error explain)) (when-let [explain (:explain error)]
(js/console.endGroup "Server Error")) (js/console.error explain)))
(js/console.groupEnd "Validation Error"))
;; This is a pure frontend error that can be caused by an active ;; This is a pure frontend error that can be caused by an active
;; assertion (assertion that is preserved on production builds). From ;; assertion (assertion that is preserved on production builds). From