-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMultipartFileUploader.tsx
63 lines (58 loc) · 1.88 KB
/
MultipartFileUploader.tsx
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
import React from "react";
import Uppy, { type UploadResult } from "@uppy/core";
import { Dashboard } from "@uppy/react";
import AwsS3Multipart from "@uppy/aws-s3-multipart";
// Uppy styles
import "@uppy/core/dist/style.min.css";
import "@uppy/dashboard/dist/style.min.css";
const fetchUploadApiEndpoint = async (endpoint: string, data: any) => {
const res = await fetch(`/api/multipart-upload/${endpoint}`, {
method: "POST",
body: JSON.stringify(data),
headers: {
accept: "application/json",
"Content-Type": "application/json",
},
});
return res.json();
};
export function MultipartFileUploader({
onUploadSuccess,
}: {
onUploadSuccess: (result: UploadResult) => void;
}) {
const uppy = React.useMemo(() => {
const uppy = new Uppy({
autoProceed: true,
}).use(AwsS3Multipart, {
createMultipartUpload: async (file) => {
const contentType = file.type;
return fetchUploadApiEndpoint("create-multipart-upload", {
file,
contentType,
});
},
listParts: (file, props) =>
fetchUploadApiEndpoint("list-parts", { file, ...props }),
signPart: (file, props) =>
fetchUploadApiEndpoint("sign-part", { file, ...props }),
abortMultipartUpload: (file, props) =>
fetchUploadApiEndpoint("abort-multipart-upload", { file, ...props }),
completeMultipartUpload: (file, props) =>
fetchUploadApiEndpoint("complete-multipart-upload", { file, ...props }),
});
return uppy;
}, []);
uppy.on("complete", (result) => {
onUploadSuccess(result);
});
uppy.on("upload-success", (file, response) => {
uppy.setFileState(file.id, {
progress: uppy.getState().files[file.id].progress,
uploadURL: response.body.Location,
response: response,
isPaused: false,
});
});
return <Dashboard uppy={uppy} showLinkToFileUploadResult={true} />;
}