mirror of
https://github.com/penpot/penpot.git
synced 2025-05-11 09:46:38 +02: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
|
(ns app.util.zip
|
||||||
"Helpers for make zip file (using jszip)."
|
"Helpers for make zip file (using jszip)."
|
||||||
(:require [vendor.jszip]
|
(:require
|
||||||
[beicon.core :as rx]))
|
["jszip" :as zip]
|
||||||
|
[app.common.data :as d]
|
||||||
|
[beicon.core :as rx]
|
||||||
|
[promesa.core :as p]))
|
||||||
|
|
||||||
(defn build
|
(defn compress-files
|
||||||
[files]
|
[files]
|
||||||
(letfn [(attach-file [zobj [name content]]
|
(letfn [(attach-file [zobj [name content]]
|
||||||
(.file zobj name content))]
|
(.file zobj name content))]
|
||||||
(let [zobj (js/JSZip.)]
|
(let [zobj (zip.)]
|
||||||
(run! (partial attach-file zobj) files)
|
(run! (partial attach-file zobj) files)
|
||||||
(->> (.generateAsync zobj #js {:type "blob"})
|
(->> (.generateAsync zobj #js {:type "blob"})
|
||||||
(rx/from)))))
|
(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
Add a link
Reference in a new issue