Fix unexpected exception on uploading new image.

And additionally add spec for image entity on
image collections related ns.
This commit is contained in:
Andrey Antukh 2017-02-26 10:57:05 +01:00
parent 345a788cdd
commit 1172c56dfa
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
3 changed files with 52 additions and 14 deletions

View file

@ -5,15 +5,45 @@
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz> ;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.main.data.images (ns uxbox.main.data.images
(:require [cuerdas.core :as str] (:require [cljs.spec :as s]
[cuerdas.core :as str]
[beicon.core :as rx] [beicon.core :as rx]
[potok.core :as ptk]
[uxbox.main.store :as st]
[uxbox.main.repo :as rp]
[uxbox.util.data :refer (jscoll->vec)] [uxbox.util.data :refer (jscoll->vec)]
[uxbox.util.uuid :as uuid] [uxbox.util.uuid :as uuid]
[potok.core :as ptk] [uxbox.util.time :as ts]
[uxbox.util.spec :as us]
[uxbox.util.router :as r] [uxbox.util.router :as r]
[uxbox.util.files :as files] [uxbox.util.files :as files]))
[uxbox.main.store :as st]
[uxbox.main.repo :as rp])) ;; --- Specs
(s/def ::name string?)
(s/def ::width number?)
(s/def ::height number?)
(s/def ::modified-at ts/instant?)
(s/def ::created-at ts/instant?)
(s/def ::mimetype string?)
(s/def ::thumbnail us/url-str?)
(s/def ::id uuid?)
(s/def ::url us/url-str?)
(s/def ::collection (s/nilable uuid?))
(s/def ::user uuid?)
(s/def ::image-entity
(s/keys :req-un [::id
::name
::width
::height
::created-at
::modified-at
::mimetype
::thumbnail
::url
::collection
::user]))
;; --- Initialize ;; --- Initialize
@ -184,6 +214,7 @@
(defn image-created (defn image-created
[item] [item]
{:pre [(us/valid? ::image-entity item)]}
(ImageCreated. item)) (ImageCreated. item))
;; --- Create Image ;; --- Create Image
@ -196,13 +227,13 @@
(assoc-in state [:dashboard :images :uploading] true)) (assoc-in state [:dashboard :images :uploading] true))
ptk/WatchEvent ptk/WatchEvent
(watch [_ state s] (watch [_ state stream]
(letfn [(image-size [file] (letfn [(image-size [file]
(->> (files/get-image-size file) (->> (files/get-image-size file)
(rx/map (partial vector file)))) (rx/map (partial vector file))))
(allowed-file? [file] (allowed-file? [file]
(contains? allowed-file-types (.-type file))) (contains? allowed-file-types (.-type file)))
(finalize-upload [] (finalize-upload [state]
(assoc-in state [:dashboard :images :uploading] false)) (assoc-in state [:dashboard :images :uploading] false))
(prepare [[file [width height]]] (prepare [[file [width height]]]
{:collection id {:collection id
@ -211,7 +242,7 @@
:file file :file file
:width width :width width
:height height})] :height height})]
(->> (rx/from-coll (jscoll->vec files)) (->> (rx/from-coll files)
(rx/filter allowed-file?) (rx/filter allowed-file?)
(rx/mapcat image-size) (rx/mapcat image-size)
(rx/map prepare) (rx/map prepare)

View file

@ -8,7 +8,7 @@
(ns uxbox.main.ui.dashboard.images (ns uxbox.main.ui.dashboard.images
(:require [cuerdas.core :as str] (:require [cuerdas.core :as str]
[lentes.core :as l] [lentes.core :as l]
[uxbox.util.i18n :as t :refer (tr)] [uxbox.util.i18n :as t :refer [tr]]
[uxbox.main.store :as st] [uxbox.main.store :as st]
[potok.core :as ptk] [potok.core :as ptk]
[uxbox.main.data.lightbox :as udl] [uxbox.main.data.lightbox :as udl]
@ -17,9 +17,9 @@
[uxbox.util.mixins :as mx :include-macros true] [uxbox.util.mixins :as mx :include-macros true]
[uxbox.main.ui.lightbox :as lbx] [uxbox.main.ui.lightbox :as lbx]
[uxbox.main.ui.keyboard :as kbd] [uxbox.main.ui.keyboard :as kbd]
[uxbox.main.ui.dashboard.header :refer (header)] [uxbox.main.ui.dashboard.header :refer [header]]
[uxbox.util.time :as dt] [uxbox.util.time :as dt]
[uxbox.util.data :as data :refer (read-string)] [uxbox.util.data :refer [read-string jscoll->vec]]
[uxbox.util.dom :as dom])) [uxbox.util.dom :as dom]))
;; --- Helpers & Constants ;; --- Helpers & Constants
@ -223,7 +223,8 @@
(letfn [(forward-click [event] (letfn [(forward-click [event]
(dom/click (mx/ref-node own "file-input"))) (dom/click (mx/ref-node own "file-input")))
(on-file-selected [event] (on-file-selected [event]
(let [files (dom/get-event-files event)] (let [files (dom/get-event-files event)
files (jscoll->vec files)]
(st/emit! (di/create-images coll-id files))))] (st/emit! (di/create-images coll-id files))))]
(let [uploading? (mx/react uploading?-ref)] (let [uploading? (mx/react uploading?-ref)]
[:div.grid-item.add-project {:on-click forward-click} [:div.grid-item.add-project {:on-click forward-click}
@ -371,8 +372,8 @@
(let [editable? (or (= type :own) (nil? id)) (let [editable? (or (= type :own) (nil? id))
ordering (:order state :name) ordering (:order state :name)
filtering (:filter state "") filtering (:filter state "")
images (mx/react images-ref) images-map (mx/react images-ref)
images (->> (vals images) images (->> (vals images-map)
(filter #(= id (:collection %))) (filter #(= id (:collection %)))
(filter-images-by filtering) (filter-images-by filtering)
(sort-images-by ordering))] (sort-images-by ordering))]

View file

@ -31,6 +31,12 @@
[v] [v]
(instance? js/File v)) (instance? js/File v))
;; TODO: properly implement
(defn url-str?
[v]
(string? v))
;; --- Default Specs ;; --- Default Specs
(s/def ::uuid uuid?) (s/def ::uuid uuid?)