mirror of
https://github.com/penpot/penpot.git
synced 2025-02-10 00:58:26 -05:00
✨ Zip utils
This commit is contained in:
parent
aaef0777b0
commit
b648fb7446
1 changed files with 37 additions and 4 deletions
|
@ -6,14 +6,47 @@
|
|||
|
||||
(ns app.util.zip
|
||||
"Helpers for make zip file (using jszip)."
|
||||
(:require [vendor.jszip]
|
||||
[beicon.core :as rx]))
|
||||
(:require
|
||||
["jszip" :as zip]
|
||||
[app.common.data :as d]
|
||||
[beicon.core :as rx]
|
||||
[promesa.core :as p]))
|
||||
|
||||
(defn build
|
||||
(defn compress-files
|
||||
[files]
|
||||
(letfn [(attach-file [zobj [name content]]
|
||||
(.file zobj name content))]
|
||||
(let [zobj (js/JSZip.)]
|
||||
(let [zobj (zip.)]
|
||||
(run! (partial attach-file zobj) files)
|
||||
(->> (.generateAsync zobj #js {:type "blob"})
|
||||
(rx/from)))))
|
||||
|
||||
(defn extract-files
|
||||
"Creates a stream that will emit values for every file in the zip"
|
||||
[file]
|
||||
(rx/create
|
||||
(fn [subs]
|
||||
(let [process-entry
|
||||
(fn [path entry]
|
||||
(if (.-dir entry)
|
||||
(rx/push! subs {:dir path})
|
||||
(p/then
|
||||
(.async entry "text")
|
||||
(fn [content]
|
||||
(rx/push! subs
|
||||
{:path path
|
||||
:content content})))))]
|
||||
|
||||
(p/let [response (js/fetch file)
|
||||
data (.blob response)
|
||||
content (zip/loadAsync data)]
|
||||
|
||||
(let [promises (atom [])]
|
||||
(.forEach content
|
||||
(fn [path entry]
|
||||
(let [current (process-entry path entry)]
|
||||
(swap! promises conj current))))
|
||||
|
||||
(p/then (p/all @promises)
|
||||
#(rx/end! subs))))
|
||||
nil))))
|
||||
|
|
Loading…
Add table
Reference in a new issue