-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathFileUploader.tsx
62 lines (57 loc) · 1.74 KB
/
FileUploader.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
import React from "react";
import Uppy, { type UploadResult, UppyFile } from "@uppy/core";
import AwsS3, { type AwsS3UploadParameters } from "@uppy/aws-s3";
import { Dashboard } from "@uppy/react";
import { sha256 } from "crypto-hash";
// Uppy styles
import "@uppy/core/dist/style.min.css";
import "@uppy/dashboard/dist/style.min.css";
export async function getUploadParameters(file: UppyFile) {
const response = await fetch("/api/upload", {
method: "POST",
headers: {
accept: "application/json",
},
body: JSON.stringify({
filename: file.name,
contentType: file.type,
}),
});
if (!response.ok) throw new Error("Unsuccessful request");
// Parse the JSON response.
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const data: { url: string; method: "PUT" } = await response.json();
// Return an object in the correct shape.
const object: AwsS3UploadParameters = {
method: data.method,
url: data.url,
fields: {}, // For presigned PUT uploads, this should be left empty.
// Provide content type header required by S3
headers: {
"Content-Type": file.type ? file.type : "application/octet-stream",
},
};
return object;
}
export function FileUploader({
onUploadSuccess,
}: {
onUploadSuccess: (result: UploadResult) => void;
}) {
const uppy = React.useMemo(() => {
const uppy = new Uppy({
autoProceed: true,
restrictions: {
maxNumberOfFiles: 3,
},
}).use(AwsS3, {
id: "AwsS3",
getUploadParameters: (file: UppyFile) => getUploadParameters(file),
});
return uppy;
}, []);
uppy.on("complete", (result) => {
onUploadSuccess(result);
});
return <Dashboard uppy={uppy} showLinkToFileUploadResult={true} />;
}