0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-15 17:21:17 -05:00

Import/export workers

This commit is contained in:
alonso.torres 2021-06-02 15:52:51 +02:00 committed by Andrés Moya
parent 21aa23e7f5
commit 61545ea13e
2 changed files with 97 additions and 31 deletions

View file

@ -7,25 +7,12 @@
(ns app.worker.export
(:require
[app.main.render :as r]
[app.main.repo :as rp]
[app.util.dom :as dom]
[app.util.http :as http]
[app.util.zip :as uz]
[app.worker.impl :as impl]
[beicon.core :as rx]))
(defn- handle-response
[response]
(cond
(http/success? response)
(rx/of (:body response))
(http/client-error? response)
(rx/throw (:body response))
:else
(rx/throw {:type :unexpected
:code (:error response)})))
(defn get-page-data
[{file-name :file-name {:keys [id name] :as data} :data}]
(->> (r/render-page data)
@ -35,13 +22,6 @@
:file-name file-name
:markup markup}))))
(defn query-file [file-id]
(->> (http/send! {:uri "/api/rpc/query/file"
:query {:id file-id}
:method :get})
(rx/map http/conditional-decode-transit)
(rx/mapcat handle-response)))
(defn process-pages [file]
(let [pages (get-in file [:data :pages])
pages-index (get-in file [:data :pages-index])]
@ -59,7 +39,7 @@
(let [render-stream
(->> (rx/from (->> files (mapv :id)))
(rx/merge-map query-file)
(rx/merge-map #(rp/query :file {:id %}))
(rx/flat-map process-pages)
(rx/observe-on :async)
(rx/flat-map get-page-data)
@ -71,7 +51,6 @@
:data (str "Render " (:file-name %) " - " (:name %)))))
(->> render-stream
(rx/reduce collect-page [])
(rx/tap #(prn %))
(rx/flat-map uz/compress-files)
(rx/map #(hash-map :type :finish
:data (dom/create-uri %)))))))

View file

@ -8,26 +8,109 @@
(:require
[app.common.data :as d]
[app.common.file-builder :as fb]
[app.common.pages :as cp]
[app.common.uuid :as uuid]
[app.main.repo :as rp]
[app.util.import.parser :as cip]
[app.util.zip :as uz]
[app.worker.impl :as impl]
[beicon.core :as rx]
[cuerdas.core :as str]
[tubax.core :as tubax]))
;; Upload changes batches size
(def change-batch-size 100)
(defn create-empty-file
"Create a new file on the back-end"
[project-id file]
(rp/mutation
:create-file
{:id (:id file)
:name (:name file)
:project-id project-id
:data (-> cp/empty-file-data
(assoc :id (:id file)))}))
(defn send-changes
"Creates batches of changes to be sent to the backend"
[file init-revn]
(let [revn (atom init-revn)
file-id (:id file)
session-id (uuid/next)
changes-batches
(->> (fb/generate-changes file)
(partition change-batch-size change-batch-size nil)
(mapv vec))]
(->> (rx/from changes-batches)
(rx/merge-map
(fn [cur-changes-batch]
(rp/mutation
:update-file
{:id file-id
:session-id session-id
:revn @revn
:changes cur-changes-batch})))
(rx/tap #(reset! revn (:revn %))))))
(defn persist-file
"Sends to the back-end the imported data"
[project-id file]
(->> (create-empty-file project-id file)
(rx/flat-map #(send-changes file (:revn %)))))
(defn parse-file-name
[dir]
(if (str/ends-with? dir "/")
(subs dir 0 (dec (count dir)))
dir))
(defn parse-page-name [path]
(defn parse-page-name
[path]
(let [[file page] (str/split path "/")]
(str/replace page ".svg" "")))
(defn import-page [file {:keys [path data]}]
(defn add-shape-file
[file node]
(let [type (cip/get-type node)
close? (cip/close? node)
data (cip/parse-data type node)]
(if close?
(case type
:frame
(fb/close-artboard file)
:group
(fb/close-group file)
;; default
file)
(case type
:frame (fb/add-artboard file data)
:group (fb/add-group file data)
:rect (fb/create-rect file data)
:circle (fb/create-circle file data)
:path (fb/create-path file data)
:text (fb/create-text file data)
:image (fb/create-image file data)
;; default
file))))
(defn import-page
[file {:keys [path content]}]
(let [page-name (parse-page-name path)]
(-> file
(fb/add-page page-name))))
(when (cip/valid? content)
(let [nodes (->> content cip/node-seq)]
(->> nodes
(filter cip/shape?)
(reduce add-shape-file (fb/add-page file page-name))
(fb/close-page))))))
(defmethod impl/handler :import-file
[{:keys [project-id files]}]
@ -49,8 +132,12 @@
(->> dir-str
(rx/merge-map
(fn [dir]
(->> file-str
(rx/filter #(str/starts-with? (:path %) dir))
(rx/reduce import-page (fb/create-file (parse-file-name dir))))))
(let [file (fb/create-file (parse-file-name dir))]
(rx/concat
(->> file-str
(rx/filter #(str/starts-with? (:path %) dir))
(rx/reduce import-page file)
(rx/flat-map #(persist-file project-id %))
(rx/ignore))
(rx/map #(select-keys % [:id :name])))))
(rx/of (select-keys file [:id :name])))))))))