-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathUpload.ts
More file actions
466 lines (375 loc) · 13.7 KB
/
Upload.ts
File metadata and controls
466 lines (375 loc) · 13.7 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import { wheneverFirstSeen } from "WoltLabSuite/Core/Helper/Selector";
import { upload as filesUpload } from "WoltLabSuite/Core/Api/Files/Upload";
import WoltlabCoreFileElement from "./woltlab-core-file";
import {
ResponseCompleted,
Response as UploadChunkResponse,
uploadChunk,
} from "WoltLabSuite/Core/Api/Files/Chunk/Chunk";
import { generateThumbnails } from "WoltLabSuite/Core/Api/Files/GenerateThumbnails";
import ImageResizer from "WoltLabSuite/Core/Image/Resizer";
import { AttachmentData } from "../Ckeditor/Attachment";
import { innerError } from "WoltLabSuite/Core/Dom/Util";
import { getPhrase } from "WoltLabSuite/Core/Language";
import { createSHA256 } from "hash-wasm";
import { cropImage, CropperConfiguration } from "WoltLabSuite/Core/Component/Image/Cropper";
import { Exif, getExifBytesFromJpeg, getExifBytesFromWebP } from "WoltLabSuite/Core/Image/ExifUtil";
export type CkeditorDropEvent = {
file: File;
promise?: Promise<unknown>;
};
export type ThumbnailsGenerated = {
data: GenerateThumbnailsResponse;
fileID: number;
};
type ThumbnailData = {
identifier: string;
link: string;
};
type GenerateThumbnailsResponse = ThumbnailData[];
type ResizeConfiguration = {
maxWidth: number;
maxHeight: number;
fileType: "image/jpeg" | "image/webp" | "keep";
quality: number;
};
const BUFFER_SIZE = 10 * 1_024 * 1_024;
async function upload(
element: WoltlabCoreFileUploadElement,
file: File,
fileHash: string,
exifData: Exif | null,
): Promise<ResponseCompleted | undefined> {
const objectType = element.dataset.objectType!;
const fileElement = document.createElement("woltlab-core-file");
fileElement.dataset.filename = file.name;
fileElement.dataset.fileSize = file.size.toString();
const event = new CustomEvent<WoltlabCoreFileElement>("uploadStart", { detail: fileElement });
element.dispatchEvent(event);
const response = await filesUpload(
file.name,
file.size,
fileHash,
objectType,
element.dataset.context || "",
exifData,
);
if (!response.ok) {
const validationError = response.error.getValidationError();
if (validationError === undefined) {
fileElement.uploadFailed(response.error);
throw new Error("Unexpected validation error", { cause: response.error });
}
fileElement.uploadFailed(response.error);
return undefined;
}
const { identifier, numberOfChunks } = response.value;
const chunkSize = Math.ceil(file.size / numberOfChunks);
notifyChunkProgress(fileElement, 0, numberOfChunks);
for (let i = 0; i < numberOfChunks; i++) {
const start = i * chunkSize;
const end = start + chunkSize;
const chunk = file.slice(start, end);
const checksum = await getSha256Hash(chunk);
const response = await uploadChunk(identifier, i, checksum, chunk);
if (!response.ok) {
fileElement.uploadFailed(response.error);
throw new Error("Unexpected validation error", { cause: response.error });
}
notifyChunkProgress(fileElement, i + 1, numberOfChunks);
await chunkUploadCompleted(fileElement, response.value);
if (response.value.completed) {
return response.value;
}
}
}
function notifyChunkProgress(element: WoltlabCoreFileElement, currentChunk: number, numberOfChunks: number): void {
// Suppress the progress bar for uploads that are processed in a single
// request, because we cannot track the upload progress within a chunk.
if (numberOfChunks === 1) {
return;
}
const event = new CustomEvent<number>("uploadProgress", {
detail: Math.floor((currentChunk / numberOfChunks) * 100),
});
element.dispatchEvent(event);
}
async function chunkUploadCompleted(fileElement: WoltlabCoreFileElement, result: UploadChunkResponse): Promise<void> {
if (!result.completed) {
return;
}
fileElement.uploadCompleted(result.fileID, result.mimeType, result.link, result.data, result.generateThumbnails);
if (result.generateThumbnails) {
const { filename, fileSize, mimeType, thumbnails } = (await generateThumbnails(result.fileID)).unwrap();
fileElement.setThumbnails(thumbnails);
fileElement.updateFileData(filename, fileSize, mimeType);
}
}
async function getSha256Hash(data: Blob): Promise<string> {
const sha256 = await createSHA256();
sha256.init();
let offset = 0;
while (offset < data.size) {
const chunk = data.slice(offset, offset + BUFFER_SIZE);
const buffer = await chunk.arrayBuffer();
sha256.update(new Uint8Array(buffer));
offset += BUFFER_SIZE;
}
return sha256.digest("hex");
}
export function clearPreviousErrors(element: WoltlabCoreFileUploadElement): void {
element.parentElement?.querySelectorAll(".innerError").forEach((x) => x.remove());
}
async function resizeImage(element: WoltlabCoreFileUploadElement, file: File): Promise<File> {
switch (file.type) {
case "image/jpeg":
case "image/png":
case "image/webp":
// Potential candidate for a resize operation.
break;
default:
// Not an image or an unsupported file type.
return file;
}
const timeout = new Promise<File>((resolve) => {
window.setTimeout(() => resolve(file), 10_000);
});
const resizeConfiguration = JSON.parse(element.dataset.resizeConfiguration!) as ResizeConfiguration;
const resizer = new ImageResizer();
const { image } = await resizer.loadFile(file);
const maxHeight = resizeConfiguration.maxHeight === -1 ? image.height : resizeConfiguration.maxHeight;
let maxWidth = resizeConfiguration.maxWidth === -1 ? image.width : resizeConfiguration.maxWidth;
if (window.devicePixelRatio >= 2) {
const actualWidth = window.screen.width * window.devicePixelRatio;
const actualHeight = window.screen.height * window.devicePixelRatio;
// If the dimensions are equal then this is a screenshot from a HiDPI
// device, thus we downscale this to the “natural” dimensions.
if (actualWidth === image.width && actualHeight === image.height) {
maxWidth = Math.min(maxWidth, window.screen.width);
}
}
const canvas = await resizer.resize(image, maxWidth, maxHeight, resizeConfiguration.quality, false, timeout);
if (canvas === undefined) {
// The resize operation failed, timed out or there was no need to perform
// any scaling whatsoever.
return file;
}
let fileType: string = resizeConfiguration.fileType;
if (fileType === "image/jpeg" || fileType === "image/webp") {
fileType = "image/webp";
} else {
fileType = file.type;
}
const resizedFile = await resizer.saveFile(
{
image: canvas,
},
file.name,
fileType,
resizeConfiguration.quality,
);
return resizedFile;
}
function validateFileLimit(element: WoltlabCoreFileUploadElement, count: number): boolean {
const maximumCount = element.maximumCount;
if (maximumCount === -1) {
return true;
}
const files = Array.from(element.parentElement!.querySelectorAll("woltlab-core-file"));
const numberOfUploadedFiles = files.filter((file) => !file.isFailedUpload()).length;
if (numberOfUploadedFiles + count <= maximumCount) {
return true;
}
reportError(element, null, getPhrase("wcf.upload.error.maximumCountReached", { maximumCount }));
return false;
}
function validateFileSize(element: WoltlabCoreFileUploadElement, file: File): boolean {
if (file.size === 0) {
reportError(element, file, getPhrase("wcf.upload.error.emptyFile", { filename: file.name }));
return false;
}
let isImage = false;
switch (file.type) {
case "image/gif":
case "image/jpeg":
case "image/png":
case "image/webp":
isImage = true;
break;
}
// Skip the file size validation for images, they can potentially be resized.
if (isImage) {
return true;
}
const maximumSize = element.maximumSize;
if (maximumSize === -1) {
return true;
}
if (file.size <= maximumSize) {
return true;
}
reportError(element, file, getPhrase("wcf.upload.error.fileSizeTooLarge", { filename: file.name }));
return false;
}
function validateFileExtension(element: WoltlabCoreFileUploadElement, file: File): boolean {
const fileExtensions = (element.dataset.fileExtensions || "*").toLowerCase().split(",");
for (const fileExtension of fileExtensions) {
if (fileExtension === "*") {
return true;
} else if (file.name.toLowerCase().endsWith(fileExtension)) {
return true;
}
}
reportError(element, file, getPhrase("wcf.upload.error.fileExtensionNotPermitted", { filename: file.name }));
return false;
}
function reportError(element: WoltlabCoreFileUploadElement, file: File | null, message: string): void {
const event = new CustomEvent<{ file: File | null; message: string }>("upload:error", {
cancelable: true,
detail: {
file,
message,
},
});
element.dispatchEvent(event);
if (event.defaultPrevented) {
return;
}
innerError(element, message);
}
async function getExifBytes(file: File): Promise<Exif | null> {
if (file.type === "image/jpeg") {
try {
const bytes = await getExifBytesFromJpeg(file);
// ExifUtil returns the entire section but we only need the app data.
// Removing the first 10 bytes drops the 0xFF 0xE1 marker followed by two
// bytes for the length and then 6 bytes for the "Exif\x00\x00" header.
return bytes.slice(10);
} catch {
return null;
}
} else if (file.type === "image/webp") {
try {
return await getExifBytesFromWebP(file);
} catch {
return null;
}
}
return null;
}
export function setup(): void {
wheneverFirstSeen("woltlab-core-file-upload", (element: WoltlabCoreFileUploadElement) => {
element.addEventListener("upload:files", (event: CustomEvent<{ files: File[] }>) => {
const { files } = event.detail;
clearPreviousErrors(element);
if (!validateFileLimit(element, files.length)) {
return;
}
for (const file of files) {
if (!validateFileExtension(element, file)) {
return;
} else if (!validateFileSize(element, file)) {
return;
}
}
element.markAsBusy();
const exifData = new Map<File, Exif | null>();
let processImage: (file: File) => Promise<File>;
if (element.dataset.cropperConfiguration) {
const cropperConfiguration = JSON.parse(element.dataset.cropperConfiguration) as CropperConfiguration;
processImage = async (file) => {
exifData.set(file, await getExifBytes(file));
try {
return await cropImage(element, file, cropperConfiguration);
} catch (e) {
element.dispatchEvent(new CustomEvent("cancel"));
throw e;
}
};
} else {
processImage = async (file) => {
exifData.set(file, await getExifBytes(file));
return resizeImage(element, file);
};
}
// Resize all files in parallel but keep the original order. This ensures
// that files are uploaded in the same order that they were provided by
// the browser.
void Promise.allSettled(files.map((file) => processImage(file)))
.then(async (results) => {
const validFiles: File[] = [];
for (let i = 0, length = results.length; i < length; i++) {
const result = results[i];
if (result.status === "fulfilled") {
validFiles.push(result.value);
} else if (result.reason !== undefined) {
let message: string;
if (result.reason instanceof Error) {
message = result.reason.message;
} else if (typeof result.reason === "string") {
message = result.reason;
} else {
message = getPhrase("wcf.upload.error.damagedImageFile", { filename: files[i].name });
}
reportError(element, files[i], message);
}
}
const checksums = await Promise.allSettled(validFiles.map((file) => getSha256Hash(file)));
for (let i = 0, length = checksums.length; i < length; i++) {
const result = checksums[i];
if (result.status === "fulfilled") {
const exif = exifData.get(validFiles[i]) || null;
void upload(element, validFiles[i], result.value, exif);
} else {
throw new Error(result.reason);
}
}
})
.finally(() => {
element.markAsReady();
});
});
element.addEventListener("ckeditorDrop", (event: CustomEvent<CkeditorDropEvent>) => {
const { file } = event.detail;
let promiseResolve: (data: AttachmentData) => void;
let promiseReject: () => void;
event.detail.promise = new Promise<AttachmentData>((resolve, reject) => {
promiseResolve = resolve;
promiseReject = reject;
});
clearPreviousErrors(element);
if (!validateFileLimit(element, 1)) {
promiseReject!();
return;
}
if (!validateFileExtension(element, file)) {
promiseReject!();
return;
}
let exifData: Exif | null;
void getExifBytes(file)
.then((exif) => {
exifData = exif;
})
.then(() => resizeImage(element, file))
.then(async (resizeFile) => {
try {
const checksum = await getSha256Hash(resizeFile);
const data = await upload(element, resizeFile, checksum, exifData);
if (data === undefined || typeof data.data.attachmentID !== "number") {
promiseReject();
} else {
const attachmentData: AttachmentData = {
attachmentId: data.data.attachmentID,
url: data.link,
};
promiseResolve(attachmentData);
}
} catch (e) {
promiseReject();
throw e;
}
});
});
});
}