Import/Export framework first version

This commit is contained in:
alonso.torres 2021-05-28 12:59:43 +02:00 committed by Andrés Moya
parent d7eec3b92b
commit bf5f845789
12 changed files with 413 additions and 7 deletions

View file

@ -0,0 +1,77 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) UXBOX Labs SL
(ns app.worker.export
(:require
[app.main.render :as r]
[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)
(rx/map (fn [markup]
{:id id
:name name
: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])]
(->> pages
(map #(hash-map
:file-name (:name file)
:data (get pages-index %))))))
(defn collect-page
[coll {:keys [id file-name name markup] :as page}]
(conj coll [(str file-name "/" name ".svg") markup]))
(defmethod impl/handler :export-file
[{:keys [team-id files] :as message}]
(let [render-stream
(->> (rx/from (->> files (mapv :id)))
(rx/merge-map query-file)
(rx/flat-map process-pages)
(rx/observe-on :async)
(rx/flat-map get-page-data)
(rx/share))]
(rx/merge
(->> render-stream
(rx/map #(hash-map :type :progress
: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 %)))))))