From 1bb00a5e50301f87822847a166fc1f9255c3e0d7 Mon Sep 17 00:00:00 2001 From: Josh Larson Date: Wed, 22 Mar 2023 20:44:38 -0500 Subject: [PATCH] Fix the storage API, introduce `storage().putRandom()` (#34) * Fix the storage API, introduce `storage().putRandom()` * Link to remix --- .changeset/real-beans-live.md | 5 + .../remix-cms/app/routes/admin/upload.$.ts | 27 +++--- packages/superflare/docs/file-storage.md | 93 +++++++++++++++++++ packages/superflare/index.ts | 1 + packages/superflare/index.types.ts | 3 +- packages/superflare/package.json | 1 + packages/superflare/src/form-data.ts | 73 +++++++++++++++ packages/superflare/src/storage.ts | 28 ++++-- pnpm-lock.yaml | 2 + 9 files changed, 207 insertions(+), 26 deletions(-) create mode 100644 .changeset/real-beans-live.md create mode 100644 packages/superflare/src/form-data.ts diff --git a/.changeset/real-beans-live.md b/.changeset/real-beans-live.md new file mode 100644 index 00000000..46f54517 --- /dev/null +++ b/.changeset/real-beans-live.md @@ -0,0 +1,5 @@ +--- +"superflare": patch +--- + +Fix the storage API, introduce `storage().putRandom()`, and provide a better way to stream file uploads with `parseMultipartFormData` diff --git a/examples/remix-cms/app/routes/admin/upload.$.ts b/examples/remix-cms/app/routes/admin/upload.$.ts index 1f8b72ab..dadb696e 100644 --- a/examples/remix-cms/app/routes/admin/upload.$.ts +++ b/examples/remix-cms/app/routes/admin/upload.$.ts @@ -1,22 +1,19 @@ import { json, type ActionArgs } from "@remix-run/cloudflare"; -import { storage } from "superflare"; +import { parseMultipartFormData, storage } from "superflare"; export async function action({ request }: ActionArgs) { - /** - * This is probably inefficient, but it's the only way to get the `File` - * which includes useful information like the file name and type. - * - * Remix offers (unstable) upload handler APIs which allow you to stream the file body to - * the destination _before_ it's fully loaded into memory, whiel exposing the file name and file type. - * This would be more efficient, but the APIs aren't fully developed, and I couldn't figure - * out how to convert them to a proper `File` object to pass to `storage().put()`. We can - * revisit this later. - */ - const formData = await request.formData(); - const file = formData.get("file") as File; - const object = await storage().put(file); + const formData = await parseMultipartFormData( + request, + async ({ stream, filename }) => { + const object = await storage().putRandom(stream, { + extension: filename?.split(".").pop(), + }); + + return object.key; + } + ); return json({ - url: storage().url(object.key), + url: storage().url(formData.get("file") as string), }); } diff --git a/packages/superflare/docs/file-storage.md b/packages/superflare/docs/file-storage.md index c92f4b19..378086ab 100644 --- a/packages/superflare/docs/file-storage.md +++ b/packages/superflare/docs/file-storage.md @@ -36,6 +36,69 @@ export async function action() { } ``` +### Handling file uploads + +If your application accepts file uploads, you will likely have an endpoint, like a Remix action, which handles an incoming request. + +The simplest approach to handling file uploads is to use the [FormData API](https://developer.mozilla.org/en-US/docs/Web/API/FormData) to parse the request body with `await request.formData()` and use the `storage().put(name, formData.get('file'))` method to store the file in your R2 bucket. + +However, this approach requires you to read the entire file into memory before storing it. This can be problematic if you are uploading large files: + +```ts +import { storage } from "superflare"; + +export async function action({ request }) { + // ⚠️ Entire file is loaded into memory! + const formData = await request.formData(); + const file = formData.get("file"); + const key = "my-file.txt"; + + const r2Object = await storage().put(key, file); +} +``` + +Instead, a better option is to stream file uploads directly to R2. Superflare provides a `parseMultipartFormData` utility, [inspired by Remix](https://remix.run/docs/en/1.14.3/utils/parse-multipart-form-data#uploadhandler), to make parsing multipart form data requests easier: + +```ts +import { storage, parseMultipartFormData } from "superflare"; + +export async function action({ request }) { + const formData = await parseMultipartFormData( + request, + async ({ name, filename, stream, data }) => { + // This is the name of your HTML file input + if (name === "file") { + const r2Object = await storage().put(filename, stream); + return r2Object.key; + } + + // Support other non-file fields + return data; + } + ); + + const url = storage.url(formData.get("file")); + + // ... +} +``` + +### Uploading files with random names + +Sometimes you may want to upload a file with a random name to prevent conflicts between other uploads. You can use the `storage().putRandom(input)` method to do this: + +```ts +const r2Object = await storage().putRandom(file, { + // Optional extension + extension: filename?.split(".").pop(), + + // Optional prefix + prefix: "avatars", +}); +``` + +By default, no extension or prefix will be added to the file name. You can override this by passing an `extension` or `prefix` option to the `putRandom` method. Prefixes and filenames are separated by a forward slash (`/`) similar to a folder structure. + ## Serving public files By default, Superflare does not serve your bucket contents to the public. However, you can mark a disk as public by setting the `publicPath` property to a public route in your `superflare.config.ts` file: @@ -113,3 +176,33 @@ export default defineConfig((ctx) => { }; }); ``` + +## `storage()` API + +### `R2Input` + +The `R2Input` type is a union of the following types: + +```ts +type R2Input = string | ReadableStream | ArrayBuffer | ArrayBufferView | Blob; +``` + +### `storage().put(key: string, input: R2Input, options?: R2PutOptions)` + +Put a file in your R2 bucket. + +### `storage().putRandom(input: R2Input, options?: R2PutOptions & { prefix?: string, extension?: string })` + +Put a file in your R2 bucket with a random name. + +### `storage().get(key: string)` + +Get a file from your R2 bucket. + +### `storage().delete(key: string)` + +Delete a file from your R2 bucket. + +### `storage().url(key: string)` + +Get a URL to a file in your R2 bucket. Requires that the disk has a `publicPath` property defined. diff --git a/packages/superflare/index.ts b/packages/superflare/index.ts index fcbf7472..79fda649 100644 --- a/packages/superflare/index.ts +++ b/packages/superflare/index.ts @@ -15,3 +15,4 @@ export { Listener } from "./src/listener"; export { handleWebSockets } from "./src/websockets"; export { Channel } from "./src/durable-objects/Channel"; export { Schema } from "./src/schema"; +export { parseMultipartFormData } from "./src/form-data"; diff --git a/packages/superflare/index.types.ts b/packages/superflare/index.types.ts index 88c6f8ca..8f6e7621 100644 --- a/packages/superflare/index.types.ts +++ b/packages/superflare/index.types.ts @@ -11,7 +11,7 @@ export { } from "./src/config"; export { DatabaseException } from "./src/query-builder"; export { seed } from "./src/seeder"; -export { storage, servePublicPathFromStorage } from "./src/storage"; +export { storage, servePublicPathFromStorage, R2Input } from "./src/storage"; export { Factory } from "./src/factory"; export { handleFetch } from "./src/fetch"; export { handleQueue } from "./src/queue"; @@ -24,6 +24,7 @@ export { Listener } from "./src/listener"; export { handleWebSockets } from "./src/websockets"; export { Channel } from "./src/durable-objects/Channel"; export { Schema } from "./src/schema"; +export { parseMultipartFormData } from "./src/form-data"; /** * Shape of the model instance. diff --git a/packages/superflare/package.json b/packages/superflare/package.json index ef6e4997..e5826da6 100644 --- a/packages/superflare/package.json +++ b/packages/superflare/package.json @@ -38,6 +38,7 @@ "license": "MIT", "dependencies": { "@clack/prompts": "^0.6.3", + "@web3-storage/multipart-parser": "^1.0.0", "bcryptjs": "^2.4.3", "chalk": "^4.1.2", "cli-table3": "^0.6.3", diff --git a/packages/superflare/src/form-data.ts b/packages/superflare/src/form-data.ts new file mode 100644 index 00000000..aac5fd31 --- /dev/null +++ b/packages/superflare/src/form-data.ts @@ -0,0 +1,73 @@ +import { streamMultipart } from "@web3-storage/multipart-parser"; + +/** + * Copied from Remix: + * @see https://github.com/remix-run/remix/blob/72c22b3deb9e84e97359b481f7f2af6cdc355877/packages/remix-server-runtime/formData.ts + * Copyright 2021 Remix Software Inc. + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +export type UploadHandlerPart = { + name: string; + filename?: string; + contentType: string; + data: AsyncIterable; + stream: ReadableStream; +}; + +export type UploadHandler = ( + part: UploadHandlerPart +) => Promise; + +/** + * Allows you to handle multipart forms (file uploads) for your app. + */ +export async function parseMultipartFormData( + request: Request, + uploadHandler: UploadHandler +): Promise { + let contentType = request.headers.get("Content-Type") || ""; + const [type, boundary] = contentType.split(/\s*;\s*boundary=/); + + if (!request.body || !boundary || type !== "multipart/form-data") { + throw new TypeError("Could not parse content as FormData."); + } + + const formData = new FormData(); + const parts: AsyncIterable = + streamMultipart(request.body, boundary); + + for await (let part of parts) { + if (part.done) break; + + if (typeof part.filename === "string") { + // only pass basename as the multipart/form-data spec recommends + // https://datatracker.ietf.org/doc/html/rfc7578#section-4.2 + part.filename = part.filename.split(/[/\\]/).pop(); + } + + /** + * Build a convenience ReadableStream for the part data. + */ + const stream = new ReadableStream({ + async pull(controller) { + for await (let chunk of part.data) { + controller.enqueue(chunk); + } + controller.close(); + }, + }); + + let value = await uploadHandler({ + ...part, + stream, + }); + if (typeof value !== "undefined" && value !== null) { + formData.append(part.name, value as any); + } + } + + return formData; +} diff --git a/packages/superflare/src/storage.ts b/packages/superflare/src/storage.ts index dd14d93c..ab7e6c62 100644 --- a/packages/superflare/src/storage.ts +++ b/packages/superflare/src/storage.ts @@ -1,5 +1,7 @@ import { Config, StorageDiskConfig } from "./config"; +export type R2Input = Parameters[1]; + export function storage(disk?: string) { if (!Config.storage?.disks) { throw new Error( @@ -39,19 +41,25 @@ class Storage { return this.disk.binding.delete(key); } - put(file: File) { - const extension = file.name.split(".").pop(); + put(...args: Parameters) { + return this.disk.binding.put(...args); + } + + /** + * Takes an input without a key and generates a random filename for you. + * Optionally pass an `{ extension: string }` option to add an extension to the filename. + */ + putRandom( + input: R2Input, + options?: R2PutOptions & { extension?: string; prefix?: string } + ) { const hash = crypto.randomUUID(); - let key = hash; - if (extension) { - key += `.${extension}`; + let key = options?.prefix ? `${options.prefix}/${hash}` : hash; + if (options?.extension) { + key += `.${options.extension}`; } - return this.disk.binding.put(key, file); - } - - putAs(...args: Parameters) { - return this.disk.binding.put(...args); + return this.disk.binding.put(key, input, options); } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cea2583a..ff5104fb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -205,6 +205,7 @@ importers: '@types/pluralize': ^0.0.29 '@types/tar-fs': ^2.0.1 '@types/yargs': ^17.0.20 + '@web3-storage/multipart-parser': ^1.0.0 bcryptjs: ^2.4.3 better-sqlite3: ^7.6 chalk: ^4.1.2 @@ -224,6 +225,7 @@ importers: yargs: ^17.6.2 dependencies: '@clack/prompts': 0.6.3 + '@web3-storage/multipart-parser': 1.0.0 bcryptjs: 2.4.3 chalk: 4.1.2 cli-table3: 0.6.3