diff --git a/package-lock.json b/package-lock.json
index 2db5352..8a5e4f7 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -17,6 +17,7 @@
"axios": "^0.21.1",
"crypto-js": "^4.1.1",
"emoji-regex": "^9.2.2",
+ "jszip": "^3.7.1",
"next": "^12.0.7",
"preview-office-docs": "^1.0.2",
"prismjs": "^1.23.0",
diff --git a/package.json b/package.json
index 464e7b6..759ba53 100644
--- a/package.json
+++ b/package.json
@@ -18,6 +18,7 @@
"axios": "^0.21.1",
"crypto-js": "^4.1.1",
"emoji-regex": "^9.2.2",
+ "jszip": "^3.7.1",
"next": "^12.0.7",
"preview-office-docs": "^1.0.2",
"prismjs": "^1.23.0",
diff --git a/pages/[...path].tsx b/pages/[...path].tsx
index 0976252..44810e9 100644
--- a/pages/[...path].tsx
+++ b/pages/[...path].tsx
@@ -18,7 +18,7 @@ export default function Folders() {
-
+
diff --git a/pages/index.tsx b/pages/index.tsx
index 85eba46..e5b7d71 100644
--- a/pages/index.tsx
+++ b/pages/index.tsx
@@ -15,7 +15,7 @@ export default function Home() {
-
+
diff --git a/utils/tools.ts b/utils/tools.ts
index 904e263..b97de21 100644
--- a/utils/tools.ts
+++ b/utils/tools.ts
@@ -1,6 +1,7 @@
import axios from 'axios'
import sha256 from 'crypto-js/sha256'
import useSWR, { cache, Key, useSWRInfinite } from 'swr'
+import JSZip from 'jszip'
import siteConfig from '../config/site.json'
@@ -123,3 +124,35 @@ export const matchProtectedRoute = (route: string) => {
}
return authTokenPath
}
+
+/**
+ * Download multiple files after compressing them into a zip
+ * @param files Files to be downloaded
+ * @param folder Optional folder name to hold files, otherwise flatten files in the zip
+ */
+export const downloadMultipleFiles = async (files: { name: string; url: string }[], folder?: string) => {
+ const zip = new JSZip()
+ const dir = folder ? zip.folder(folder)! : zip
+
+ // Add selected file blobs to zip
+ files.forEach(({ name, url }) => {
+ dir.file(
+ name,
+ fetch(url).then(r => r.blob())
+ )
+ })
+
+ // Create zip file and prepare for download
+ const b = await dir.generateAsync({ type: 'blob' })
+ const el = document.createElement('a')
+ el.style.display = 'none'
+ document.body.appendChild(el)
+
+ // Download zip file
+ const bUrl = window.URL.createObjectURL(b)
+ el.href = bUrl
+ el.download = folder ? folder + '.zip' : 'download.zip'
+ el.click()
+ window.URL.revokeObjectURL(bUrl)
+ el.remove()
+}