-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupload.basic.tsx
More file actions
171 lines (148 loc) · 5.16 KB
/
Copy pathupload.basic.tsx
File metadata and controls
171 lines (148 loc) · 5.16 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
/**
* @akoenig/remix-observable-file-upload-demo
*
* Copyright, 2023 - André König, Hamburg, Germany
*
* All rights reserved
*/
/**
* @author André König <hi@andrekoenig.de>
*
*/
import type { ActionFunctionArgs, MetaFunction } from "@remix-run/node";
import { InfoCircledIcon } from "@radix-ui/react-icons";
import { json, unstable_parseMultipartFormData } from "@remix-run/node";
import { Form, Link, useLoaderData, useResolvedPath } from "@remix-run/react";
import { nanoid } from "nanoid";
import { Button } from "~/components/ui/button.tsx";
import { Card } from "~/components/ui/card.tsx";
import { Progress } from "~/components/ui/progress.tsx";
import { uploadEventBus } from "~/utils/UploadEventBus.server.ts";
import { redirectWithConfetti } from "~/utils/confetti.server.ts";
import { createObservableFileUploadHandler } from "~/utils/createObservableFileUploadHandler.server.ts";
import { useUploadProgress } from "~/utils/useUploadProgress.ts";
type UploadProgressEvent = Readonly<{
uploadId: string;
name: string;
filename: string;
filesize: number;
uploadedBytes: number;
percentageStatus: number;
}>;
export const meta: MetaFunction = () => [
{
title: "Basic Example",
},
];
export function loader() {
const uploadId = nanoid();
return json({ uploadId });
}
export async function action({ request }: ActionFunctionArgs) {
const url = new URL(request.url);
const uploadId = url.searchParams.get("uploadId");
const maxPartSize = 100_000_000; // 100 MB
if (!uploadId) {
throw new Response(null, {
status: 400,
statusText: "Upload ID is missing.",
});
}
// Get the overall filesize of the uploadable file.
const filesize = Number(request.headers.get("Content-Length"));
if (filesize > maxPartSize) {
throw new Response(null, {
status: 400,
statusText: "File size exceeded",
});
}
const observableFileUploadHandler = createObservableFileUploadHandler({
avoidFileConflicts: true,
maxPartSize,
onProgress({ name, filename, uploadedBytes }) {
uploadEventBus.emit<UploadProgressEvent>({
uploadId,
name,
filename,
filesize,
uploadedBytes,
percentageStatus: Math.floor((uploadedBytes * 100) / filesize),
});
},
onDone({ name, filename, uploadedBytes }) {
uploadEventBus.emit<UploadProgressEvent>({
uploadId,
name,
filename,
filesize,
uploadedBytes,
percentageStatus: 100,
});
},
});
await unstable_parseMultipartFormData(request, observableFileUploadHandler);
return redirectWithConfetti("/upload/done");
}
export default function BasicExample() {
const loaderData = useLoaderData<typeof loader>();
const currentPath = useResolvedPath(".");
const progress = useUploadProgress<UploadProgressEvent>(loaderData.uploadId);
return (
<section className="flex flex-col gap-8">
<header className="flex flex-col gap-2">
<h3 className="text-xl font-bold">Basic Example</h3>
<p className="text-muted-foreground">
This example demonstrates a basic implementation of an observable file
upload by utilizing a file input field and an action. Although the
client implementation is straightforward, the example streams the
upload progress to the client via{" "}
<Link
className="text-pink-500 underline"
to="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events"
>
SSE
</Link>{" "}
and displays it as a progress bar.
</p>
</header>
<Card className="p-4 shadow-xl">
<Form
className="flex flex-col gap-4"
method="POST"
encType="multipart/form-data"
action={`${currentPath.pathname}?uploadId=${loaderData.uploadId}`}
>
<input name="the-file" type="file" />
<Button type="submit">Upload</Button>
<p className="text-center text-muted-foreground">
<small>
max. 100 MB (configurable via{" "}
<Link
to="https://github.com/akoenig/remix-observable-file-upload-demo/blob/33aa02bfa7703e02b2ea0033f6f83135ffb361ca/app/routes/upload.basic.tsx#L65"
className="text-pink-500 underline"
>
maxPartSize
</Link>
)
</small>
</p>
{progress?.success && progress.event ? (
<div className="flex flex-col gap-4">
<Progress value={progress.event.percentageStatus} />
<p className="text-center text-muted-foreground">
{progress.event.percentageStatus}% ·{" "}
{progress.event.uploadedBytes} / {progress.event.filesize} bytes
transferred
</p>
</div>
) : null}
</Form>
</Card>
<p className="flex gap-2 text-xs text-muted-foreground items-center justify-center p-4">
<InfoCircledIcon className="w-4 h-4" />
Although the uploaded files are deleted after some time, please refrain
from uploading any sensitive files here.
</p>
</section>
);
}