-
Notifications
You must be signed in to change notification settings - Fork 512
/
demo-create-file.js
139 lines (127 loc) · 4.13 KB
/
demo-create-file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* global zip, document, URL, MouseEvent, AbortController, alert */
(() => {
if (typeof TransformStream == "undefined") {
const script = document.createElement("script");
script.src = "lib/web-streams-polyfill.min.js";
document.body.appendChild(script);
}
const model = (() => {
let zipWriter;
return {
addFile(file, options) {
if (!zipWriter) {
zipWriter = new zip.ZipWriter(new zip.BlobWriter("application/zip"), { bufferedWrite: true });
}
return zipWriter.add(file.name, new zip.BlobReader(file), options);
},
async getBlobURL() {
if (zipWriter) {
const blobURL = URL.createObjectURL(await zipWriter.close());
zipWriter = null;
return blobURL;
} else {
throw new Error("Zip file closed");
}
}
};
})();
(() => {
const fileInput = document.getElementById("file-input");
const fileInputButton = document.getElementById("file-input-button");
const zipProgress = document.createElement("progress");
const downloadButton = document.getElementById("download-button");
const fileList = document.getElementById("file-list");
const filenameInput = document.getElementById("filename-input");
const passwordInput = document.getElementById("password-input");
fileInputButton.addEventListener("click", () => fileInput.dispatchEvent(new MouseEvent("click")), false);
downloadButton.addEventListener("click", onDownloadButtonClick, false);
fileInput.onchange = selectFiles;
async function selectFiles() {
try {
await addFiles();
fileInput.value = "";
downloadButton.disabled = false;
} catch (error) {
alert(error);
} finally {
zipProgress.remove();
}
}
async function addFiles() {
downloadButton.disabled = true;
await Promise.all(Array.from(fileInput.files).map(async file => {
const li = document.createElement("li");
const filenameContainer = document.createElement("span");
const filename = document.createElement("span");
const zipProgress = document.createElement("progress");
filenameContainer.classList.add("filename-container");
li.appendChild(filenameContainer);
filename.classList.add("filename");
filename.textContent = file.name;
filenameContainer.appendChild(filename);
zipProgress.value = 0;
zipProgress.max = 0;
li.appendChild(zipProgress);
fileList.classList.remove("empty");
fileList.appendChild(li);
li.title = file.name;
li.classList.add("pending");
li.onclick = event => event.preventDefault();
const controller = new AbortController();
const signal = controller.signal;
const abortButton = document.createElement("button");
abortButton.onclick = () => controller.abort();
abortButton.textContent = "✖";
abortButton.title = "Abort";
filenameContainer.appendChild(abortButton);
try {
const entry = await model.addFile(file, {
password: passwordInput.value,
signal,
onstart(max) {
li.classList.remove("pending");
li.classList.add("busy");
zipProgress.max = max;
},
onprogress(index, max) {
zipProgress.value = index;
zipProgress.max = max;
}
});
li.title += `\n Last modification date: ${entry.lastModDate.toLocaleString()}\n Compressed size: ${entry.compressedSize.toLocaleString()} bytes`;
} catch (error) {
if (signal.reason && signal.reason.code == error.code) {
if (!li.previousElementSibling && !li.nextElementSibling) {
fileList.classList.add("empty");
}
li.remove();
} else {
throw error;
}
} finally {
li.classList.remove("busy");
zipProgress.remove();
}
}));
}
async function onDownloadButtonClick(event) {
let blobURL;
try {
blobURL = await model.getBlobURL();
} catch (error) {
alert(error);
}
if (blobURL) {
const anchor = document.createElement("a");
const clickEvent = new MouseEvent("click");
anchor.href = blobURL;
anchor.download = filenameInput.value;
anchor.dispatchEvent(clickEvent);
fileList.innerHTML = "";
fileList.classList.add("empty");
}
downloadButton.disabled = true;
event.preventDefault();
}
})();
})();