Skip to content

Commit 40750b1

Browse files
committed
perf: stream file uploads
1 parent 8fa8fd9 commit 40750b1

File tree

4 files changed

+21
-33
lines changed

4 files changed

+21
-33
lines changed

app/routes/settings+/profile.photo.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getFormProps, getInputProps, useForm } from '@conform-to/react'
22
import { getZodConstraint, parseWithZod } from '@conform-to/zod'
33
import { invariantResponse } from '@epic-web/invariant'
4-
import { type FileUpload, parseFormData } from '@mjackson/form-data-parser'
4+
import { parseFormData } from '@mjackson/form-data-parser'
55
import { type SEOHandle } from '@nasa-gcn/remix-seo'
66
import { useState } from 'react'
77
import { data, redirect, Form, useNavigation } from 'react-router'
@@ -12,7 +12,6 @@ import { Icon } from '#app/components/ui/icon.tsx'
1212
import { StatusButton } from '#app/components/ui/status-button.tsx'
1313
import { requireUserId } from '#app/utils/auth.server.ts'
1414
import { prisma } from '#app/utils/db.server.ts'
15-
import { uploadHandler } from '#app/utils/file-uploads.server.ts'
1615
import {
1716
getUserImgSrc,
1817
useDoubleCheck,
@@ -67,11 +66,7 @@ export async function loader({ request }: Route.LoaderArgs) {
6766
export async function action({ request }: Route.ActionArgs) {
6867
const userId = await requireUserId(request)
6968

70-
const formData = await parseFormData(
71-
request,
72-
{ maxFileSize: MAX_SIZE },
73-
async (file: FileUpload) => uploadHandler(file),
74-
)
69+
const formData = await parseFormData(request, { maxFileSize: MAX_SIZE })
7570
const submission = await parseWithZod(formData, {
7671
schema: PhotoFormSchema.transform(async (data) => {
7772
if (data.intent === 'delete') return { intent: 'delete' }

app/routes/users+/$username_+/__note-editor.server.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { parseWithZod } from '@conform-to/zod'
2-
import { type FileUpload, parseFormData } from '@mjackson/form-data-parser'
2+
import { parseFormData } from '@mjackson/form-data-parser'
33
import { createId as cuid } from '@paralleldrive/cuid2'
44
import { data, redirect, type ActionFunctionArgs } from 'react-router'
55
import { z } from 'zod'
66
import { requireUserId } from '#app/utils/auth.server.ts'
77
import { prisma } from '#app/utils/db.server.ts'
8-
import { uploadHandler } from '#app/utils/file-uploads.server.ts'
98
import { uploadNoteImage } from '#app/utils/storage.server.ts'
109
import {
1110
MAX_UPLOAD_SIZE,
@@ -21,18 +20,16 @@ function imageHasFile(
2120

2221
function imageHasId(
2322
image: ImageFieldset,
24-
): image is ImageFieldset & { id: NonNullable<ImageFieldset['id']> } {
25-
return image.id != null
23+
): image is ImageFieldset & { id: string } {
24+
return Boolean(image.id)
2625
}
2726

2827
export async function action({ request }: ActionFunctionArgs) {
2928
const userId = await requireUserId(request)
3029

31-
const formData = await parseFormData(
32-
request,
33-
{ maxFileSize: MAX_UPLOAD_SIZE },
34-
async (file: FileUpload) => uploadHandler(file),
35-
)
30+
const formData = await parseFormData(request, {
31+
maxFileSize: MAX_UPLOAD_SIZE,
32+
})
3633

3734
const submission = await parseWithZod(formData, {
3835
schema: NoteEditorSchema.superRefine(async (data, ctx) => {

app/utils/file-uploads.server.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

app/utils/storage.server.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async function uploadToStorage(file: File | FileUpload, key: string) {
1414
const uploadResponse = await fetch(url, {
1515
method: 'PUT',
1616
headers,
17-
body: file instanceof File ? file : Buffer.from(await file.arrayBuffer()),
17+
body: file instanceof File ? file : file.stream(),
1818
})
1919

2020
if (!uploadResponse.ok) {
@@ -169,3 +169,15 @@ function getSignedPutRequestInfo(file: File | FileUpload, key: string) {
169169
},
170170
}
171171
}
172+
173+
export function getSignedGetRequestInfo(key: string) {
174+
const { url, baseHeaders } = getBaseSignedRequestInfo({
175+
method: 'GET',
176+
key,
177+
})
178+
179+
return {
180+
url,
181+
headers: baseHeaders,
182+
}
183+
}

0 commit comments

Comments
 (0)