mirror of
https://github.com/penpot/penpot.git
synced 2025-06-13 10:11:39 +02:00
✨ Improve error checking and notification
This commit is contained in:
parent
bbf3bc7909
commit
e3e8180b7a
5 changed files with 124 additions and 83 deletions
|
@ -12,6 +12,7 @@
|
|||
[potok.core :as ptk]
|
||||
[uxbox.common.spec :as us]
|
||||
[uxbox.common.data :as d]
|
||||
[uxbox.main.data.messages :as dm]
|
||||
[uxbox.main.store :as st]
|
||||
[uxbox.main.repo :as rp]
|
||||
[uxbox.util.i18n :refer [tr]]
|
||||
|
@ -352,7 +353,11 @@
|
|||
|
||||
;; --- Create Image
|
||||
(declare create-images-result)
|
||||
(def allowed-file-types #{"image/jpeg" "image/png"})
|
||||
(def allowed-file-types #{"image/jpeg" "image/png" "image/webp"})
|
||||
(def max-file-size (* 5 1024 1024))
|
||||
|
||||
;; TODO: unify with upload-image at main/data/workspace/persistence.cljs
|
||||
;; https://tree.taiga.io/project/uxboxproject/us/440
|
||||
|
||||
(defn create-images
|
||||
([library-id files] (create-images library-id files identity))
|
||||
|
@ -362,22 +367,28 @@
|
|||
(ptk/reify ::create-images
|
||||
ptk/WatchEvent
|
||||
(watch [_ state stream]
|
||||
(letfn [(allowed-file? [file]
|
||||
(contains? allowed-file-types (.-type file)))
|
||||
#_(finalize-upload [state]
|
||||
(assoc-in state [:dashboard-images :uploading] false))
|
||||
(on-success [_]
|
||||
#_(st/emit! finalize-upload)
|
||||
(on-uploaded))
|
||||
(on-error [e]
|
||||
#_(st/emit! finalize-upload)
|
||||
(rx/throw e))
|
||||
(prepare [file]
|
||||
{:name (.-name file)
|
||||
:library-id library-id
|
||||
:content file})]
|
||||
(let [check-file
|
||||
(fn [file]
|
||||
(when (> (.-size file) max-file-size)
|
||||
(throw (ex-info (tr "errors.image-too-large") {})))
|
||||
(when-not (contains? allowed-file-types (.-type file))
|
||||
(throw (ex-info (tr "errors.image-format-unsupported") {})))
|
||||
file)
|
||||
|
||||
on-success on-uploaded
|
||||
|
||||
on-error #(if (.-message %)
|
||||
(rx/of (dm/error (.-message %)))
|
||||
(rx/of (dm/error (tr "errors.unexpected-error"))))
|
||||
|
||||
prepare
|
||||
(fn [file]
|
||||
{:name (.-name file)
|
||||
:library-id library-id
|
||||
:content file})]
|
||||
|
||||
(->> (rx/from files)
|
||||
(rx/filter allowed-file?)
|
||||
(rx/map check-file)
|
||||
(rx/map prepare)
|
||||
(rx/mapcat #(rp/mutation! :upload-image %))
|
||||
(rx/reduce conj [])
|
||||
|
|
|
@ -16,10 +16,12 @@
|
|||
[uxbox.common.pages :as cp]
|
||||
[uxbox.common.spec :as us]
|
||||
[uxbox.main.data.dashboard :as dd]
|
||||
[uxbox.main.data.messages :as dm]
|
||||
[uxbox.main.data.workspace.common :as dwc]
|
||||
[uxbox.main.repo :as rp]
|
||||
[uxbox.main.store :as st]
|
||||
[uxbox.common.geom.point :as gpt]
|
||||
[uxbox.util.i18n :as i18n :refer [tr]]
|
||||
[uxbox.util.router :as rt]
|
||||
[uxbox.util.time :as dt]
|
||||
[uxbox.util.transit :as t]))
|
||||
|
@ -292,7 +294,11 @@
|
|||
;; --- Upload Image
|
||||
|
||||
(declare image-uploaded)
|
||||
(def allowed-file-types #{"image/jpeg" "image/png"})
|
||||
(def allowed-file-types #{"image/jpeg" "image/png" "image/webp"})
|
||||
(def max-file-size (* 5 1024 1024))
|
||||
|
||||
;; TODO: unify with create-images at main/data/images.cljs
|
||||
;; https://tree.taiga.io/project/uxboxproject/us/440
|
||||
|
||||
(defn upload-image
|
||||
([file] (upload-image file identity))
|
||||
|
@ -305,22 +311,34 @@
|
|||
|
||||
ptk/WatchEvent
|
||||
(watch [_ state stream]
|
||||
(let [allowed-file? #(contains? allowed-file-types (.-type %))
|
||||
(let [check-file
|
||||
(fn [file]
|
||||
(when (> (.-size file) max-file-size)
|
||||
(throw (ex-info (tr "errors.image-too-large") {})))
|
||||
(when-not (contains? allowed-file-types (.-type file))
|
||||
(throw (ex-info (tr "errors.image-format-unsupported") {})))
|
||||
file)
|
||||
|
||||
finalize-upload #(assoc-in % [:workspace-local :uploading] false)
|
||||
|
||||
file-id (get-in state [:workspace-page :file-id])
|
||||
|
||||
on-success #(do (st/emit! finalize-upload)
|
||||
(on-uploaded %))
|
||||
|
||||
on-error #(do (st/emit! finalize-upload)
|
||||
(rx/throw %))
|
||||
(if (.-message %)
|
||||
(rx/of (dm/error (.-message %)))
|
||||
(rx/of (dm/error (tr "errors.unexpected-error")))))
|
||||
|
||||
prepare
|
||||
(fn [file]
|
||||
{:name (.-name file)
|
||||
:file-id file-id
|
||||
:content file})]
|
||||
|
||||
(->> (rx/of file)
|
||||
(rx/filter allowed-file?)
|
||||
(rx/map check-file)
|
||||
(rx/map prepare)
|
||||
(rx/mapcat #(rp/mutation! :upload-file-image %))
|
||||
(rx/do on-success)
|
||||
|
|
|
@ -19,11 +19,11 @@
|
|||
(let [opt-pick-one #(if multi % (first %))
|
||||
|
||||
on-files-selected (fn [event] (st/emit!
|
||||
(-> (dom/get-target event)
|
||||
(dom/get-files)
|
||||
(array-seq)
|
||||
(opt-pick-one)
|
||||
(on-selected))))]
|
||||
(some-> (dom/get-target event)
|
||||
(dom/get-files)
|
||||
(array-seq)
|
||||
(opt-pick-one)
|
||||
(on-selected))))]
|
||||
[:*
|
||||
(when label-text
|
||||
[:label {:for input-id :class-name label-class} label-text])
|
||||
|
|
|
@ -165,7 +165,7 @@
|
|||
(t locale (str "dashboard.library.add-item." (name section)))]
|
||||
|
||||
[:& file-uploader {:accept (case section
|
||||
:images "image"
|
||||
:images "image/jpeg,image/png,image/webp"
|
||||
:icons "image/svg+xml"
|
||||
"")
|
||||
:multi true
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue