mirror of
https://github.com/penpot/penpot.git
synced 2025-05-24 11:26:12 +02:00
✨ Frontend support for binary files
This commit is contained in:
parent
2fe770e0bb
commit
17645bb2a7
10 changed files with 281 additions and 125 deletions
|
@ -16,7 +16,8 @@
|
||||||
[app.config :as cf]
|
[app.config :as cf]
|
||||||
[app.db :as db]
|
[app.db :as db]
|
||||||
[app.media :as media]
|
[app.media :as media]
|
||||||
[app.rpc.queries.files :refer [decode-row]]
|
[app.rpc.queries.files :refer [decode-row check-edition-permissions!]]
|
||||||
|
[app.rpc.queries.profile :as profile]
|
||||||
[app.storage :as sto]
|
[app.storage :as sto]
|
||||||
[app.storage.tmp :as tmp]
|
[app.storage.tmp :as tmp]
|
||||||
[app.tasks.file-gc]
|
[app.tasks.file-gc]
|
||||||
|
@ -84,10 +85,10 @@
|
||||||
[v type]
|
[v type]
|
||||||
`(let [expected# (get-mark ~type)
|
`(let [expected# (get-mark ~type)
|
||||||
val# (long ~v)]
|
val# (long ~v)]
|
||||||
(when (not= val# expected#)
|
(when (not= val# expected#)
|
||||||
(ex/raise :type :validation
|
(ex/raise :type :validation
|
||||||
:code :unexpected-mark
|
:code :unexpected-mark
|
||||||
:hint (format "received mark %s, expected %s" val# expected#)))))
|
:hint (format "received mark %s, expected %s" val# expected#)))))
|
||||||
|
|
||||||
(defmacro assert-label
|
(defmacro assert-label
|
||||||
[expr label]
|
[expr label]
|
||||||
|
@ -759,8 +760,8 @@
|
||||||
|
|
||||||
(finally
|
(finally
|
||||||
(l/info :hint "exportation finished" :export-id id
|
(l/info :hint "exportation finished" :export-id id
|
||||||
:elapsed (str (inst-ms (dt/diff ts (dt/now))) "ms")
|
:elapsed (str (inst-ms (dt/diff ts (dt/now))) "ms")
|
||||||
:cause @cs)))))
|
:cause @cs)))))
|
||||||
|
|
||||||
(defn import!
|
(defn import!
|
||||||
[{:keys [::input] :as cfg}]
|
[{:keys [::input] :as cfg}]
|
||||||
|
@ -787,12 +788,38 @@
|
||||||
|
|
||||||
(s/def ::file-id ::us/uuid)
|
(s/def ::file-id ::us/uuid)
|
||||||
(s/def ::profile-id ::us/uuid)
|
(s/def ::profile-id ::us/uuid)
|
||||||
|
(s/def ::include-libraries? ::us/boolean)
|
||||||
|
(s/def ::embed-assets? ::us/boolean)
|
||||||
|
|
||||||
(s/def ::export-binfile
|
(s/def ::export-binfile
|
||||||
(s/keys :req-un [::profile-id ::file-id]))
|
(s/keys :req-un [::profile-id ::file-id ::include-libraries? ::embed-assets?]))
|
||||||
|
|
||||||
#_:clj-kondo/ignore
|
|
||||||
(sv/defmethod ::export-binfile
|
(sv/defmethod ::export-binfile
|
||||||
"Export a penpot file in a binary format."
|
"Export a penpot file in a binary format."
|
||||||
[{:keys [pool] :as cfg} {:keys [profile-id file-id] :as params}]
|
[{:keys [pool] :as cfg} {:keys [profile-id file-id include-libraries? embed-assets?] :as params}]
|
||||||
{:hello "world"})
|
(db/with-atomic [conn pool]
|
||||||
|
(check-edition-permissions! conn profile-id file-id)
|
||||||
|
(let [path (export! (assoc cfg
|
||||||
|
::file-ids [file-id]
|
||||||
|
::embed-assets? embed-assets?
|
||||||
|
::include-libraries? include-libraries?))]
|
||||||
|
(with-meta {}
|
||||||
|
{:transform-response (fn [_ response]
|
||||||
|
(assoc response
|
||||||
|
:body (io/input-stream path)
|
||||||
|
:headers {"content-type" "application/octet-stream"}))}))))
|
||||||
|
|
||||||
|
(s/def ::input ::media/upload)
|
||||||
|
|
||||||
|
(s/def ::import-binfile
|
||||||
|
(s/keys :req-un [::profile-id ::input]))
|
||||||
|
|
||||||
|
(sv/defmethod ::import-binfile
|
||||||
|
"Import a penpot file in a binary format."
|
||||||
|
[{:keys [pool] :as cfg} {:keys [profile-id input] :as params}]
|
||||||
|
(let [project-id (some-> (profile/retrieve-additional-data pool profile-id) :default-project-id)]
|
||||||
|
(import! (assoc cfg
|
||||||
|
::input (:path input)
|
||||||
|
::project-id project-id
|
||||||
|
::ignore-index-errors? true))))
|
||||||
|
|
||||||
|
|
|
@ -76,11 +76,12 @@
|
||||||
(defn- send-command!
|
(defn- send-command!
|
||||||
"A simple helper for a common case of sending and receiving transit
|
"A simple helper for a common case of sending and receiving transit
|
||||||
data to the penpot mutation api."
|
data to the penpot mutation api."
|
||||||
[id params]
|
[id {:keys [blob? form-data?] :as params}]
|
||||||
(->> (http/send! {:method :post
|
(->> (http/send! {:method :post
|
||||||
:uri (u/join base-uri "api/rpc/command/" (name id))
|
:uri (u/join base-uri "api/rpc/command/" (name id))
|
||||||
:credentials "include"
|
:credentials "include"
|
||||||
:body (http/transit-data params)})
|
:body (if form-data? (http/form-data params) (http/transit-data params))
|
||||||
|
:response-type (if blob? :blob :text)})
|
||||||
(rx/map http/conditional-decode-transit)
|
(rx/map http/conditional-decode-transit)
|
||||||
(rx/mapcat handle-response)))
|
(rx/mapcat handle-response)))
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@
|
||||||
(mf/defc export-dialog
|
(mf/defc export-dialog
|
||||||
{::mf/register modal/components
|
{::mf/register modal/components
|
||||||
::mf/register-as :export}
|
::mf/register-as :export}
|
||||||
[{:keys [team-id files has-libraries?]}]
|
[{:keys [team-id files has-libraries? binary?]}]
|
||||||
(let [state (mf/use-state {:status :prepare
|
(let [state (mf/use-state {:status :prepare
|
||||||
:files (->> files (mapv #(assoc % :loading? true)))})
|
:files (->> files (mapv #(assoc % :loading? true)))})
|
||||||
selected-option (mf/use-state :all)
|
selected-option (mf/use-state :all)
|
||||||
|
@ -60,10 +60,11 @@
|
||||||
(fn []
|
(fn []
|
||||||
(swap! state assoc :status :exporting)
|
(swap! state assoc :status :exporting)
|
||||||
(->> (uw/ask-many!
|
(->> (uw/ask-many!
|
||||||
{:cmd :export-file
|
{:cmd (if binary? :export-binary-file :export-standard-file)
|
||||||
:team-id team-id
|
:team-id team-id
|
||||||
:export-type @selected-option
|
:export-type @selected-option
|
||||||
:files (->> files (mapv :id))})
|
:files files
|
||||||
|
})
|
||||||
(rx/delay-emit 1000)
|
(rx/delay-emit 1000)
|
||||||
(rx/subs
|
(rx/subs
|
||||||
(fn [msg]
|
(fn [msg]
|
||||||
|
@ -73,6 +74,7 @@
|
||||||
(when (= :finish (:type msg))
|
(when (= :finish (:type msg))
|
||||||
(swap! state update :files mark-file-success (:file-id msg))
|
(swap! state update :files mark-file-success (:file-id msg))
|
||||||
(dom/trigger-download-uri (:filename msg) (:mtype msg) (:uri msg)))))))
|
(dom/trigger-download-uri (:filename msg) (:mtype msg) (:uri msg)))))))
|
||||||
|
|
||||||
cancel-fn
|
cancel-fn
|
||||||
(mf/use-callback
|
(mf/use-callback
|
||||||
(fn [event]
|
(fn [event]
|
||||||
|
|
|
@ -158,26 +158,38 @@
|
||||||
:on-accept del-shared})))
|
:on-accept del-shared})))
|
||||||
|
|
||||||
on-export-files
|
on-export-files
|
||||||
|
(fn [event-name binary?]
|
||||||
|
(st/emit! (ptk/event ::ev/event {::ev/name event-name
|
||||||
|
::ev/origin "dashboard"
|
||||||
|
:num-files (count files)}))
|
||||||
|
|
||||||
|
(->> (rx/from files)
|
||||||
|
(rx/flat-map
|
||||||
|
(fn [file]
|
||||||
|
(->> (rp/query :file-libraries {:file-id (:id file)})
|
||||||
|
(rx/map #(assoc file :has-libraries? (d/not-empty? %))))))
|
||||||
|
(rx/reduce conj [])
|
||||||
|
(rx/subs
|
||||||
|
(fn [files]
|
||||||
|
(st/emit!
|
||||||
|
(modal/show
|
||||||
|
{:type :export
|
||||||
|
:team-id current-team-id
|
||||||
|
:has-libraries? (->> files (some :has-libraries?))
|
||||||
|
:files files
|
||||||
|
:binary? binary?}))))))
|
||||||
|
|
||||||
|
on-export-binary-files
|
||||||
(mf/use-callback
|
(mf/use-callback
|
||||||
(mf/deps files current-team-id)
|
(mf/deps files current-team-id)
|
||||||
(fn [_]
|
(fn [_]
|
||||||
(st/emit! (ptk/event ::ev/event {::ev/name "export-files"
|
(on-export-files "export-binary-files" true)))
|
||||||
::ev/origin "dashboard"
|
|
||||||
:num-files (count files)}))
|
on-export-standard-files
|
||||||
(->> (rx/from files)
|
(mf/use-callback
|
||||||
(rx/flat-map
|
(mf/deps files current-team-id)
|
||||||
(fn [file]
|
(fn [_]
|
||||||
(->> (rp/query :file-libraries {:file-id (:id file)})
|
(on-export-files "export-standard-files" false)))
|
||||||
(rx/map #(assoc file :has-libraries? (d/not-empty? %))))))
|
|
||||||
(rx/reduce conj [])
|
|
||||||
(rx/subs
|
|
||||||
(fn [files]
|
|
||||||
(st/emit!
|
|
||||||
(modal/show
|
|
||||||
{:type :export
|
|
||||||
:team-id current-team-id
|
|
||||||
:has-libraries? (->> files (some :has-libraries?))
|
|
||||||
:files files})))))))
|
|
||||||
|
|
||||||
;; NOTE: this is used for detect if component is still mounted
|
;; NOTE: this is used for detect if component is still mounted
|
||||||
mounted-ref (mf/use-ref true)]
|
mounted-ref (mf/use-ref true)]
|
||||||
|
@ -210,7 +222,8 @@
|
||||||
[[(tr "dashboard.duplicate-multi" file-count) on-duplicate nil "duplicate-multi"]
|
[[(tr "dashboard.duplicate-multi" file-count) on-duplicate nil "duplicate-multi"]
|
||||||
(when (or (seq current-projects) (seq other-teams))
|
(when (or (seq current-projects) (seq other-teams))
|
||||||
[(tr "dashboard.move-to-multi" file-count) nil sub-options "move-to-multi"])
|
[(tr "dashboard.move-to-multi" file-count) nil sub-options "move-to-multi"])
|
||||||
[(tr "dashboard.export-multi" file-count) on-export-files]
|
[(tr "dashboard.export-binary-multi" file-count) on-export-binary-files]
|
||||||
|
[(tr "dashboard.export-standard-multi" file-count) on-export-standard-files]
|
||||||
[:separator]
|
[:separator]
|
||||||
[(tr "labels.delete-multi-files" file-count) on-delete nil "delete-multi-files"]]
|
[(tr "labels.delete-multi-files" file-count) on-delete nil "delete-multi-files"]]
|
||||||
|
|
||||||
|
@ -222,7 +235,9 @@
|
||||||
(if (:is-shared file)
|
(if (:is-shared file)
|
||||||
[(tr "dashboard.remove-shared") on-del-shared nil "file-del-shared"]
|
[(tr "dashboard.remove-shared") on-del-shared nil "file-del-shared"]
|
||||||
[(tr "dashboard.add-shared") on-add-shared nil "file-add-shared"])
|
[(tr "dashboard.add-shared") on-add-shared nil "file-add-shared"])
|
||||||
[(tr "dashboard.export-single") on-export-files nil "file-export"]
|
[:separator]
|
||||||
|
[(tr "dashboard.download-binary-file") on-export-binary-files nil "download-binary-file"]
|
||||||
|
[(tr "dashboard.download-standard-file") on-export-standard-files nil "download-standard-file"]
|
||||||
[:separator]
|
[:separator]
|
||||||
[(tr "labels.delete") on-delete nil "file-delete"]])]
|
[(tr "labels.delete") on-delete nil "file-delete"]])]
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@
|
||||||
|
|
||||||
(let [on-file-selected (use-import-file project-id on-finish-import)]
|
(let [on-file-selected (use-import-file project-id on-finish-import)]
|
||||||
[:form.import-file
|
[:form.import-file
|
||||||
[:& file-uploader {:accept ".penpot"
|
[:& file-uploader {:accept ".penpot,.zip"
|
||||||
:multi true
|
:multi true
|
||||||
:ref external-ref
|
:ref external-ref
|
||||||
:on-selected on-file-selected}]]))
|
:on-selected on-file-selected}]]))
|
||||||
|
@ -78,19 +78,20 @@
|
||||||
(= uri (:uri file))
|
(= uri (:uri file))
|
||||||
(assoc :status :analyze-error))))))
|
(assoc :status :analyze-error))))))
|
||||||
|
|
||||||
(defn set-analyze-result [files uri data]
|
(defn set-analyze-result [files uri type data]
|
||||||
(let [existing-files? (into #{} (->> files (map :file-id) (filter some?)))
|
(let [existing-files? (into #{} (->> files (map :file-id) (filter some?)))
|
||||||
replace-file
|
replace-file
|
||||||
(fn [file]
|
(fn [file]
|
||||||
(if (and (= uri (:uri file) )
|
(if (and (= uri (:uri file))
|
||||||
(= (:status file) :analyzing))
|
(= (:status file) :analyzing))
|
||||||
(->> (:files data)
|
(->> (:files data)
|
||||||
(remove (comp existing-files? first) )
|
(remove (comp existing-files? first))
|
||||||
(mapv (fn [[file-id file-data]]
|
(mapv (fn [[file-id file-data]]
|
||||||
(-> file-data
|
(-> file-data
|
||||||
(assoc :file-id file-id
|
(assoc :file-id file-id
|
||||||
:status :ready
|
:status :ready
|
||||||
:uri uri)))))
|
:uri uri
|
||||||
|
:type type)))))
|
||||||
[file]))]
|
[file]))]
|
||||||
(into [] (mapcat replace-file) files)))
|
(into [] (mapcat replace-file) files)))
|
||||||
|
|
||||||
|
@ -139,7 +140,7 @@
|
||||||
(str message)))
|
(str message)))
|
||||||
|
|
||||||
(mf/defc import-entry
|
(mf/defc import-entry
|
||||||
[{:keys [state file editing?]}]
|
[{:keys [state file editing? can-be-deleted?]}]
|
||||||
|
|
||||||
(let [loading? (or (= :analyzing (:status file))
|
(let [loading? (or (= :analyzing (:status file))
|
||||||
(= :importing (:status file)))
|
(= :importing (:status file)))
|
||||||
|
@ -206,9 +207,11 @@
|
||||||
|
|
||||||
[:div.file-name-label (:name file) (when is-shared? i/library)])
|
[:div.file-name-label (:name file) (when is-shared? i/library)])
|
||||||
|
|
||||||
[:div.edit-entry-buttons
|
[:div.edit-entry-buttons
|
||||||
[:button {:on-click handle-edit-entry} i/pencil]
|
(when (= "application/zip" (:type file))
|
||||||
[:button {:on-click handle-remove-entry} i/trash]]]
|
[:button {:on-click handle-edit-entry} i/pencil])
|
||||||
|
(when can-be-deleted?
|
||||||
|
[:button {:on-click handle-remove-entry} i/trash])]]
|
||||||
|
|
||||||
(cond
|
(cond
|
||||||
analyze-error?
|
analyze-error?
|
||||||
|
@ -245,21 +248,20 @@
|
||||||
(fn [files]
|
(fn [files]
|
||||||
(->> (uw/ask-many!
|
(->> (uw/ask-many!
|
||||||
{:cmd :analyze-import
|
{:cmd :analyze-import
|
||||||
:files (->> files (mapv :uri))})
|
:files files})
|
||||||
(rx/delay-emit emit-delay)
|
(rx/delay-emit emit-delay)
|
||||||
(rx/subs
|
(rx/subs
|
||||||
(fn [{:keys [uri data error] :as msg}]
|
(fn [{:keys [uri data error type] :as msg}]
|
||||||
(log/debug :uri uri :data data :error error)
|
(log/debug :uri uri :data data :error error)
|
||||||
(if (some? error)
|
(if (some? error)
|
||||||
(swap! state update :files set-analyze-error uri)
|
(swap! state update :files set-analyze-error uri)
|
||||||
(swap! state update :files set-analyze-result uri data)))))))
|
(swap! state update :files set-analyze-result uri type data)))))))
|
||||||
|
|
||||||
import-files
|
import-files
|
||||||
(mf/use-callback
|
(mf/use-callback
|
||||||
(fn [project-id files]
|
(fn [project-id files]
|
||||||
(st/emit! (ptk/event ::ev/event {::ev/name "import-files"
|
(st/emit! (ptk/event ::ev/event {::ev/name "import-files"
|
||||||
:num-files (count files)}))
|
:num-files (count files)}))
|
||||||
|
|
||||||
(->> (uw/ask-many!
|
(->> (uw/ask-many!
|
||||||
{:cmd :import-files
|
{:cmd :import-files
|
||||||
:project-id project-id
|
:project-id project-id
|
||||||
|
@ -281,7 +283,7 @@
|
||||||
(mf/deps project-id (:files @state))
|
(mf/deps project-id (:files @state))
|
||||||
(fn [event]
|
(fn [event]
|
||||||
(dom/prevent-default event)
|
(dom/prevent-default event)
|
||||||
(let [files (->> @state :files (filterv #(= :ready (:status %))))]
|
(let [files (->> @state :files (filterv #(and (= :ready (:status %)) (not (:deleted? %)))))]
|
||||||
(import-files project-id files))
|
(import-files project-id files))
|
||||||
|
|
||||||
(swap! state
|
(swap! state
|
||||||
|
@ -300,7 +302,8 @@
|
||||||
warning-files (->> @state :files (filter #(and (= (:status %) :import-finish) (d/not-empty? (:errors %)))) count)
|
warning-files (->> @state :files (filter #(and (= (:status %) :import-finish) (d/not-empty? (:errors %)))) count)
|
||||||
success-files (->> @state :files (filter #(and (= (:status %) :import-finish) (empty? (:errors %)))) count)
|
success-files (->> @state :files (filter #(and (= (:status %) :import-finish) (empty? (:errors %)))) count)
|
||||||
pending-analysis? (> (->> @state :files (filter #(= (:status %) :analyzing)) count) 0)
|
pending-analysis? (> (->> @state :files (filter #(= (:status %) :analyzing)) count) 0)
|
||||||
pending-import? (> (->> @state :files (filter #(= (:status %) :importing)) count) 0)]
|
pending-import? (> (->> @state :files (filter #(= (:status %) :importing)) count) 0)
|
||||||
|
files (->> (:files @state) (filterv (comp not :deleted?)))]
|
||||||
|
|
||||||
(mf/use-effect
|
(mf/use-effect
|
||||||
(fn []
|
(fn []
|
||||||
|
@ -333,12 +336,13 @@
|
||||||
[:div.icon i/checkbox-checked]
|
[:div.icon i/checkbox-checked]
|
||||||
[:div.message (tr "dashboard.import.import-message" success-files)]]))
|
[:div.message (tr "dashboard.import.import-message" success-files)]]))
|
||||||
|
|
||||||
(for [file (->> (:files @state) (filterv (comp not :deleted?)))]
|
(for [file files]
|
||||||
(let [editing? (and (some? (:file-id file))
|
(let [editing? (and (some? (:file-id file))
|
||||||
(= (:file-id file) (:editing @state)))]
|
(= (:file-id file) (:editing @state)))]
|
||||||
[:& import-entry {:state state
|
[:& import-entry {:state state
|
||||||
:file file
|
:file file
|
||||||
:editing? editing?}]))]
|
:editing? editing?
|
||||||
|
:can-be-deleted? (> (count files) 1)}]))]
|
||||||
|
|
||||||
[:div.modal-footer
|
[:div.modal-footer
|
||||||
[:div.action-buttons
|
[:div.action-buttons
|
||||||
|
|
|
@ -121,26 +121,26 @@
|
||||||
(mf/use-fn
|
(mf/use-fn
|
||||||
(mf/deps file)
|
(mf/deps file)
|
||||||
#(st/emit! (modal/show
|
#(st/emit! (modal/show
|
||||||
{:type :confirm
|
{:type :confirm
|
||||||
:message ""
|
:message ""
|
||||||
:title (tr "modals.add-shared-confirm.message" (:name file))
|
:title (tr "modals.add-shared-confirm.message" (:name file))
|
||||||
:hint (tr "modals.add-shared-confirm.hint")
|
:hint (tr "modals.add-shared-confirm.hint")
|
||||||
:cancel-label :omit
|
:cancel-label :omit
|
||||||
:accept-label (tr "modals.add-shared-confirm.accept")
|
:accept-label (tr "modals.add-shared-confirm.accept")
|
||||||
:accept-style :primary
|
:accept-style :primary
|
||||||
:on-accept add-shared-fn})))
|
:on-accept add-shared-fn})))
|
||||||
|
|
||||||
on-remove-shared
|
on-remove-shared
|
||||||
(mf/use-fn
|
(mf/use-fn
|
||||||
(mf/deps file)
|
(mf/deps file)
|
||||||
#(st/emit! (modal/show
|
#(st/emit! (modal/show
|
||||||
{:type :confirm
|
{:type :confirm
|
||||||
:message ""
|
:message ""
|
||||||
:title (tr "modals.remove-shared-confirm.message" (:name file))
|
:title (tr "modals.remove-shared-confirm.message" (:name file))
|
||||||
:hint (tr "modals.remove-shared-confirm.hint")
|
:hint (tr "modals.remove-shared-confirm.hint")
|
||||||
:cancel-label :omit
|
:cancel-label :omit
|
||||||
:accept-label (tr "modals.remove-shared-confirm.accept")
|
:accept-label (tr "modals.remove-shared-confirm.accept")
|
||||||
:on-accept del-shared-fn})))
|
:on-accept del-shared-fn})))
|
||||||
|
|
||||||
handle-blur (fn [_]
|
handle-blur (fn [_]
|
||||||
(let [value (-> edit-input-ref mf/ref-val dom/get-value)]
|
(let [value (-> edit-input-ref mf/ref-val dom/get-value)]
|
||||||
|
@ -160,27 +160,38 @@
|
||||||
(st/emit! (de/show-workspace-export-dialog))))
|
(st/emit! (de/show-workspace-export-dialog))))
|
||||||
|
|
||||||
on-export-file
|
on-export-file
|
||||||
|
(fn [event-name binary?]
|
||||||
|
(st/emit! (ptk/event ::ev/event {::ev/name event-name
|
||||||
|
::ev/origin "workspace"
|
||||||
|
:num-files 1}))
|
||||||
|
|
||||||
|
(->> (rx/of file)
|
||||||
|
(rx/flat-map
|
||||||
|
(fn [file]
|
||||||
|
(->> (rp/query :file-libraries {:file-id (:id file)})
|
||||||
|
(rx/map #(assoc file :has-libraries? (d/not-empty? %))))))
|
||||||
|
(rx/reduce conj [])
|
||||||
|
(rx/subs
|
||||||
|
(fn [files]
|
||||||
|
(st/emit!
|
||||||
|
(modal/show
|
||||||
|
{:type :export
|
||||||
|
:team-id team-id
|
||||||
|
:has-libraries? (->> files (some :has-libraries?))
|
||||||
|
:files files
|
||||||
|
:binary? binary?}))))))
|
||||||
|
|
||||||
|
on-export-binary-file
|
||||||
(mf/use-callback
|
(mf/use-callback
|
||||||
(mf/deps file team-id)
|
(mf/deps file team-id)
|
||||||
(fn [_]
|
(fn [_]
|
||||||
(st/emit! (ptk/event ::ev/event {::ev/name "export-files"
|
(on-export-file "export-binary-files" true)))
|
||||||
::ev/origin "workspace"
|
|
||||||
:num-files 1}))
|
|
||||||
|
|
||||||
(->> (rx/of file)
|
on-export-standard-file
|
||||||
(rx/flat-map
|
(mf/use-callback
|
||||||
(fn [file]
|
(mf/deps file team-id)
|
||||||
(->> (rp/query :file-libraries {:file-id (:id file)})
|
(fn [_]
|
||||||
(rx/map #(assoc file :has-libraries? (d/not-empty? %))))))
|
(on-export-file "export-standard-files" false)))
|
||||||
(rx/reduce conj [])
|
|
||||||
(rx/subs
|
|
||||||
(fn [files]
|
|
||||||
(st/emit!
|
|
||||||
(modal/show
|
|
||||||
{:type :export
|
|
||||||
:team-id team-id
|
|
||||||
:has-libraries? (->> files (some :has-libraries?))
|
|
||||||
:files files})))))))
|
|
||||||
|
|
||||||
on-export-frames
|
on-export-frames
|
||||||
(mf/use-callback
|
(mf/use-callback
|
||||||
|
@ -274,10 +285,12 @@
|
||||||
[:li.export-file {:on-click on-export-shapes}
|
[:li.export-file {:on-click on-export-shapes}
|
||||||
[:span (tr "dashboard.export-shapes")]
|
[:span (tr "dashboard.export-shapes")]
|
||||||
[:span.shortcut (sc/get-tooltip :export-shapes)]]
|
[:span.shortcut (sc/get-tooltip :export-shapes)]]
|
||||||
[:li.export-file {:on-click on-export-file}
|
[:li.separator.export-file {:on-click on-export-binary-file}
|
||||||
[:span (tr "dashboard.export-single")]]
|
[:span (tr "dashboard.download-binary-file")]]
|
||||||
|
[:li.export-file {:on-click on-export-standard-file}
|
||||||
|
[:span (tr "dashboard.download-standard-file")]]
|
||||||
(when (seq frames)
|
(when (seq frames)
|
||||||
[:li.export-file {:on-click on-export-frames}
|
[:li.separator.export-file {:on-click on-export-frames}
|
||||||
[:span (tr "dashboard.export-frames")]])]]
|
[:span (tr "dashboard.export-frames")]])]]
|
||||||
|
|
||||||
[:& dropdown {:show (= @show-sub-menu? :edit)
|
[:& dropdown {:show (= @show-sub-menu? :edit)
|
||||||
|
|
|
@ -450,13 +450,34 @@
|
||||||
(->> (uz/compress-files data)
|
(->> (uz/compress-files data)
|
||||||
(rx/map #(vector (get files file-id) %)))))))))
|
(rx/map #(vector (get files file-id) %)))))))))
|
||||||
|
|
||||||
(defmethod impl/handler :export-file
|
(defmethod impl/handler :export-binary-file
|
||||||
|
[{:keys [files export-type] :as message}]
|
||||||
|
(->> (rx/from files)
|
||||||
|
(rx/mapcat
|
||||||
|
(fn [file]
|
||||||
|
(->> (rp/command! :export-binfile {:file-id (:id file)
|
||||||
|
:include-libraries? (= export-type :all)
|
||||||
|
:embed-assets? (= export-type :merge)
|
||||||
|
:blob? true})
|
||||||
|
(rx/map #(hash-map :type :finish
|
||||||
|
:file-id (:id file)
|
||||||
|
:filename (:name file)
|
||||||
|
:mtype "application/penpot"
|
||||||
|
:description "Penpot export (*.penpot)"
|
||||||
|
:uri (wapi/create-uri (wapi/create-blob %))))
|
||||||
|
(rx/catch
|
||||||
|
(fn [err]
|
||||||
|
(rx/of {:type :error
|
||||||
|
:error (str err)
|
||||||
|
:file-id (:id file)}))))))))
|
||||||
|
|
||||||
|
(defmethod impl/handler :export-standard-file
|
||||||
[{:keys [team-id files export-type] :as message}]
|
[{:keys [team-id files export-type] :as message}]
|
||||||
|
|
||||||
(->> (rx/from files)
|
(->> (rx/from files)
|
||||||
(rx/mapcat
|
(rx/mapcat
|
||||||
(fn [file]
|
(fn [file]
|
||||||
(->> (export-file team-id file export-type)
|
(->> (export-file team-id (:id file) export-type)
|
||||||
(rx/map
|
(rx/map
|
||||||
(fn [value]
|
(fn [value]
|
||||||
(if (contains? value :type)
|
(if (contains? value :type)
|
||||||
|
@ -465,11 +486,11 @@
|
||||||
{:type :finish
|
{:type :finish
|
||||||
:file-id (:id file)
|
:file-id (:id file)
|
||||||
:filename (:name file)
|
:filename (:name file)
|
||||||
:mtype "application/penpot"
|
:mtype "application/zip"
|
||||||
:description "Penpot export (*.penpot)"
|
:description "Penpot export (*.zip)"
|
||||||
:uri (wapi/create-uri export-blob)}))))
|
:uri (wapi/create-uri export-blob)}))))
|
||||||
(rx/catch
|
(rx/catch
|
||||||
(fn [err]
|
(fn [err]
|
||||||
(rx/of {:type :error
|
(rx/of {:type :error
|
||||||
:error (str err)
|
:error (str err)
|
||||||
:file-id file}))))))))
|
:file-id (:id file)}))))))))
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
(ns app.worker.import
|
(ns app.worker.import
|
||||||
(:refer-clojure :exclude [resolve])
|
(:refer-clojure :exclude [resolve])
|
||||||
(:require
|
(:require
|
||||||
|
["jszip" :as zip]
|
||||||
[app.common.data :as d]
|
[app.common.data :as d]
|
||||||
[app.common.file-builder :as fb]
|
[app.common.file-builder :as fb]
|
||||||
[app.common.geom.point :as gpt]
|
[app.common.geom.point :as gpt]
|
||||||
|
@ -20,6 +21,7 @@
|
||||||
[app.util.http :as http]
|
[app.util.http :as http]
|
||||||
[app.util.import.parser :as cip]
|
[app.util.import.parser :as cip]
|
||||||
[app.util.json :as json]
|
[app.util.json :as json]
|
||||||
|
[app.util.webapi :as wapi]
|
||||||
[app.util.zip :as uz]
|
[app.util.zip :as uz]
|
||||||
[app.worker.impl :as impl]
|
[app.worker.impl :as impl]
|
||||||
[beicon.core :as rx]
|
[beicon.core :as rx]
|
||||||
|
@ -519,48 +521,95 @@
|
||||||
(rx/flat-map link-file-libraries)
|
(rx/flat-map link-file-libraries)
|
||||||
(rx/ignore)))))
|
(rx/ignore)))))
|
||||||
|
|
||||||
|
(defn parse-mtype [ba]
|
||||||
|
(let [u8 (js/Uint8Array. ba 0 4)
|
||||||
|
sg (areduce u8 i ret "" (str ret (if (zero? i) "" " ") (.toString (aget u8 i) 8)))]
|
||||||
|
(case sg
|
||||||
|
"120 113 3 4" "application/zip"
|
||||||
|
"application/octet-stream")))
|
||||||
|
|
||||||
(defmethod impl/handler :analyze-import
|
(defmethod impl/handler :analyze-import
|
||||||
[{:keys [files]}]
|
[{:keys [files]}]
|
||||||
|
|
||||||
(->> (rx/from files)
|
(->> (rx/from files)
|
||||||
(rx/flat-map
|
(rx/flat-map
|
||||||
(fn [uri]
|
(fn [file]
|
||||||
(->> (rx/of uri)
|
(let [st (->> (http/send!
|
||||||
(rx/flat-map uz/load-from-url)
|
{:uri (:uri file)
|
||||||
(rx/flat-map #(get-file {:zip %} :manifest))
|
:response-type :blob
|
||||||
(rx/map (comp d/kebab-keys cip/string->uuid))
|
:method :get})
|
||||||
(rx/map #(hash-map :uri uri :data %))
|
(rx/map :body)
|
||||||
(rx/catch #(rx/of {:uri uri :error (.-message %)})))))))
|
(rx/mapcat wapi/read-file-as-array-buffer)
|
||||||
|
(rx/map (fn [data]
|
||||||
|
{:type (parse-mtype data)
|
||||||
|
:uri (:uri file)
|
||||||
|
:body data})))]
|
||||||
|
(->> (rx/merge
|
||||||
|
(->> st
|
||||||
|
(rx/filter (fn [data] (= "application/zip" (:type data))))
|
||||||
|
(rx/flat-map #(zip/loadAsync (:body %)))
|
||||||
|
(rx/flat-map #(get-file {:zip %} :manifest))
|
||||||
|
(rx/map (comp d/kebab-keys cip/string->uuid))
|
||||||
|
(rx/map #(hash-map :uri (:uri file) :data % :type "application/zip")))
|
||||||
|
(->> st
|
||||||
|
(rx/filter (fn [data] (= "application/octet-stream" (:type data))))
|
||||||
|
(rx/map (fn [_]
|
||||||
|
(let [file-id (uuid/next)]
|
||||||
|
{:uri (:uri file)
|
||||||
|
:data {:name (:name file)
|
||||||
|
:file-id file-id
|
||||||
|
:files {file-id {:name (:name file)}}
|
||||||
|
:status :ready}
|
||||||
|
:type "application/octet-stream"})))))
|
||||||
|
(rx/catch #(rx/of {:uri (:uri file) :error (.-message %)}))))))))
|
||||||
|
|
||||||
(defmethod impl/handler :import-files
|
(defmethod impl/handler :import-files
|
||||||
[{:keys [project-id files]}]
|
[{:keys [project-id files]}]
|
||||||
|
|
||||||
(let [context {:project-id project-id
|
(let [context {:project-id project-id
|
||||||
:resolve (resolve-factory)}]
|
:resolve (resolve-factory)}
|
||||||
|
zip-files (filter #(= "application/zip" (:type %)) files)
|
||||||
|
binary-files (filter #(= "application/octet-stream" (:type %)) files)]
|
||||||
|
|
||||||
(->> (create-files context files)
|
(->> (rx/merge
|
||||||
(rx/flat-map
|
(->> (create-files context zip-files)
|
||||||
(fn [[file data]]
|
(rx/flat-map
|
||||||
(->> (uz/load-from-url (:uri data))
|
(fn [[file data]]
|
||||||
(rx/map #(-> context (assoc :zip %) (merge data)))
|
(->> (uz/load-from-url (:uri data))
|
||||||
(rx/merge-map
|
(rx/map #(-> context (assoc :zip %) (merge data)))
|
||||||
(fn [context]
|
(rx/merge-map
|
||||||
;; process file retrieves a stream that will emit progress notifications
|
(fn [context]
|
||||||
;; and other that will emit the files once imported
|
;; process file retrieves a stream that will emit progress notifications
|
||||||
(let [[progress-stream file-stream] (process-file context file)]
|
;; and other that will emit the files once imported
|
||||||
(rx/merge progress-stream
|
(let [[progress-stream file-stream] (process-file context file)]
|
||||||
(->> file-stream
|
(rx/merge progress-stream
|
||||||
(rx/map
|
(->> file-stream
|
||||||
(fn [file]
|
(rx/map
|
||||||
{:status :import-finish
|
(fn [file]
|
||||||
:errors (:errors file)
|
{:status :import-finish
|
||||||
:file-id (:file-id data)})))))))
|
:errors (:errors file)
|
||||||
(rx/catch (fn [cause]
|
:file-id (:file-id data)})))))))
|
||||||
(log/error :hint (ex-message cause) :file-id (:file-id data) :cause cause)
|
(rx/catch (fn [cause]
|
||||||
(rx/of {:status :import-error
|
(log/error :hint (ex-message cause) :file-id (:file-id data) :cause cause)
|
||||||
:file-id (:file-id data)
|
(rx/of {:status :import-error
|
||||||
:error (ex-message cause)
|
:file-id (:file-id data)
|
||||||
:error-data (ex-data cause)}))))))
|
:error (ex-message cause)
|
||||||
|
:error-data (ex-data cause)})))))))
|
||||||
|
|
||||||
|
(->> (rx/from binary-files)
|
||||||
|
(rx/flat-map
|
||||||
|
(fn [data]
|
||||||
|
(->> (http/send!
|
||||||
|
{:uri (:uri data)
|
||||||
|
:response-type :blob
|
||||||
|
:method :get})
|
||||||
|
(rx/map :body)
|
||||||
|
(rx/mapcat #(rp/command! :import-binfile {:input %
|
||||||
|
:form-data? true}))
|
||||||
|
(rx/map
|
||||||
|
(fn [_]
|
||||||
|
{:status :import-finish
|
||||||
|
:file-id (:file-id data)})))))))
|
||||||
|
|
||||||
(rx/catch (fn [cause]
|
(rx/catch (fn [cause]
|
||||||
(log/error :hint "unexpected error on import process"
|
(log/error :hint "unexpected error on import process"
|
||||||
|
|
|
@ -309,6 +309,18 @@ msgstr "Export selection"
|
||||||
msgid "dashboard.export-single"
|
msgid "dashboard.export-single"
|
||||||
msgstr "Export Penpot file"
|
msgstr "Export Penpot file"
|
||||||
|
|
||||||
|
msgid "dashboard.download-binary-file"
|
||||||
|
msgstr "Download Penpot file (.penpot)"
|
||||||
|
|
||||||
|
msgid "dashboard.download-standard-file"
|
||||||
|
msgstr "Download standard file (.svg + .json)"
|
||||||
|
|
||||||
|
msgid "dashboard.export-binary-multi"
|
||||||
|
msgstr "Download %s Penpot files (.penpot)"
|
||||||
|
|
||||||
|
msgid "dashboard.export-standard-multi"
|
||||||
|
msgstr "Download %s standard files (.svg + .json)"
|
||||||
|
|
||||||
msgid "dashboard.export.detail"
|
msgid "dashboard.export.detail"
|
||||||
msgstr "* Might include components, graphics, colors and/or typographies."
|
msgstr "* Might include components, graphics, colors and/or typographies."
|
||||||
|
|
||||||
|
|
|
@ -315,6 +315,18 @@ msgstr "Exportar selección"
|
||||||
msgid "dashboard.export-single"
|
msgid "dashboard.export-single"
|
||||||
msgstr "Exportar archivo Penpot"
|
msgstr "Exportar archivo Penpot"
|
||||||
|
|
||||||
|
msgid "dashboard.download-binary-file"
|
||||||
|
msgstr "Descargar archivo Penpot (.penpot)"
|
||||||
|
|
||||||
|
msgid "dashboard.download-standard-file"
|
||||||
|
msgstr "Descargar archivo estándar (.svg + .json)"
|
||||||
|
|
||||||
|
msgid "dashboard.export-binary-multi"
|
||||||
|
msgstr "Descargar %s archivos Penpot (.penpot)"
|
||||||
|
|
||||||
|
msgid "dashboard.export-standard-multi"
|
||||||
|
msgstr "Descargar %s archivos estándar (.svg + .json)"
|
||||||
|
|
||||||
msgid "dashboard.export.detail"
|
msgid "dashboard.export.detail"
|
||||||
msgstr "* Pueden incluir components, gráficos, colores y/o tipografias."
|
msgstr "* Pueden incluir components, gráficos, colores y/o tipografias."
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue