Skip to content

Commit a9a9553

Browse files
feat: move to new file uploading package
1 parent 44a44b9 commit a9a9553

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
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'
45
import { type SEOHandle } from '@nasa-gcn/remix-seo'
56
import { useState } from 'react'
67
import {
78
data,
89
redirect,
9-
unstable_createMemoryUploadHandler,
10-
unstable_parseMultipartFormData,
1110
type LoaderFunctionArgs,
1211
type ActionFunctionArgs,
1312
Form,
@@ -22,6 +21,7 @@ import { Icon } from '#app/components/ui/icon.tsx'
2221
import { StatusButton } from '#app/components/ui/status-button.tsx'
2322
import { requireUserId } from '#app/utils/auth.server.ts'
2423
import { prisma } from '#app/utils/db.server.ts'
24+
import { uploadHandler } from '#app/utils/file-uploads.server.ts'
2525
import {
2626
getUserImgSrc,
2727
useDoubleCheck,
@@ -73,11 +73,12 @@ export async function loader({ request }: LoaderFunctionArgs) {
7373

7474
export async function action({ request }: ActionFunctionArgs) {
7575
const userId = await requireUserId(request)
76-
const formData = await unstable_parseMultipartFormData(
76+
77+
const formData = await parseFormData(
7778
request,
78-
unstable_createMemoryUploadHandler({ maxPartSize: MAX_SIZE }),
79+
async (file: FileUpload) => uploadHandler(file),
80+
{ maxFileSize: MAX_SIZE },
7981
)
80-
8182
const submission = await parseWithZod(formData, {
8283
schema: PhotoFormSchema.transform(async (data) => {
8384
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,15 +1,11 @@
11
import { parseWithZod } from '@conform-to/zod'
2+
import { type FileUpload, parseFormData } from '@mjackson/form-data-parser'
23
import { createId as cuid } from '@paralleldrive/cuid2'
3-
import {
4-
unstable_createMemoryUploadHandler as createMemoryUploadHandler,
5-
data,
6-
unstable_parseMultipartFormData as parseMultipartFormData,
7-
redirect,
8-
type ActionFunctionArgs,
9-
} from 'react-router'
4+
import { data, redirect, type ActionFunctionArgs } from 'react-router'
105
import { z } from 'zod'
116
import { requireUserId } from '#app/utils/auth.server.ts'
127
import { prisma } from '#app/utils/db.server.ts'
8+
import { uploadHandler } from '#app/utils/file-uploads.server.ts'
139
import {
1410
MAX_UPLOAD_SIZE,
1511
NoteEditorSchema,
@@ -31,9 +27,10 @@ function imageHasId(
3127
export async function action({ request }: ActionFunctionArgs) {
3228
const userId = await requireUserId(request)
3329

34-
const formData = await parseMultipartFormData(
30+
const formData = await parseFormData(
3531
request,
36-
createMemoryUploadHandler({ maxPartSize: MAX_UPLOAD_SIZE }),
32+
async (file: FileUpload) => uploadHandler(file),
33+
{ maxFileSize: MAX_UPLOAD_SIZE },
3734
)
3835

3936
const submission = await parseWithZod(formData, {

app/utils/file-uploads.server.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { type FileUpload } from '@mjackson/form-data-parser'
2+
3+
/**
4+
* Handles the upload of a single file and converts it into a File object.
5+
*
6+
* @param {FileUpload} file - The FileUpload object to process.
7+
* @returns {Promise<File>} - A promise that resolves with the converted File object.
8+
*/
9+
export async function uploadHandler(file: FileUpload): Promise<File> {
10+
const fileContent = await file.arrayBuffer() // Get file content as an ArrayBuffer
11+
const fileInstance = new File([fileContent], file.name, {
12+
type: file.type,
13+
lastModified: file.lastModified,
14+
})
15+
return fileInstance
16+
}

package-lock.json

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"@epic-web/invariant": "^1.0.0",
5252
"@epic-web/remember": "^1.1.0",
5353
"@epic-web/totp": "^2.0.0",
54+
"@mjackson/form-data-parser": "^0.5.1",
5455
"@nasa-gcn/remix-seo": "^2.0.1",
5556
"@paralleldrive/cuid2": "^2.2.2",
5657
"@prisma/client": "^6.0.1",
@@ -163,4 +164,4 @@
163164
"seed": "tsx prisma/seed.ts"
164165
},
165166
"prettier": "@epic-web/config/prettier"
166-
}
167+
}

0 commit comments

Comments
 (0)