feat(storage-uploadthing): configurable upload router input config (#11954)

Fixes https://github.com/payloadcms/payload/issues/11949 by setting the
default limit to `512MB`.
Additionally, makes this configurable via
`clientUploads.routerInputConfig`. Details are here
https://docs.uploadthing.com/file-routes#route-config
This commit is contained in:
Sasha
2025-04-03 00:14:08 +03:00
committed by GitHub
parent 760cfadaad
commit f9c73ad5f2
2 changed files with 18 additions and 4 deletions

View File

@@ -12,6 +12,7 @@ type Args = {
req: PayloadRequest req: PayloadRequest
}) => boolean | Promise<boolean> }) => boolean | Promise<boolean>
acl: 'private' | 'public-read' acl: 'private' | 'public-read'
routerInputConfig?: FileRouterInputConfig
token?: string token?: string
} }
@@ -22,18 +23,24 @@ import type { FileRouter } from 'uploadthing/server'
import { createRouteHandler } from 'uploadthing/next' import { createRouteHandler } from 'uploadthing/next'
import { createUploadthing } from 'uploadthing/server' import { createUploadthing } from 'uploadthing/server'
import type { FileRouterInputConfig } from './index.js'
export const getClientUploadRoute = ({ export const getClientUploadRoute = ({
access = defaultAccess, access = defaultAccess,
acl, acl,
routerInputConfig = {},
token, token,
}: Args): PayloadHandler => { }: Args): PayloadHandler => {
const f = createUploadthing() const f = createUploadthing()
const uploadRouter = { const uploadRouter = {
uploader: f({ uploader: f({
...routerInputConfig,
blob: { blob: {
acl, acl,
maxFileCount: 1, maxFileCount: 1,
maxFileSize: '512MB',
...('blob' in routerInputConfig ? routerInputConfig.blob : {}),
}, },
}) })
.middleware(async ({ req: rawReq }) => { .middleware(async ({ req: rawReq }) => {

View File

@@ -1,17 +1,17 @@
import type { import type {
Adapter, Adapter,
ClientUploadsConfig, ClientUploadsAccess,
PluginOptions as CloudStoragePluginOptions, PluginOptions as CloudStoragePluginOptions,
CollectionOptions, CollectionOptions,
GeneratedAdapter, GeneratedAdapter,
} from '@payloadcms/plugin-cloud-storage/types' } from '@payloadcms/plugin-cloud-storage/types'
import type { Config, Field, Plugin, UploadCollectionSlug } from 'payload' import type { Config, Field, Plugin, UploadCollectionSlug } from 'payload'
import type { createUploadthing } from 'uploadthing/server'
import type { UTApiOptions } from 'uploadthing/types' import type { UTApiOptions } from 'uploadthing/types'
import { cloudStoragePlugin } from '@payloadcms/plugin-cloud-storage' import { cloudStoragePlugin } from '@payloadcms/plugin-cloud-storage'
import { initClientUploads } from '@payloadcms/plugin-cloud-storage/utilities' import { initClientUploads } from '@payloadcms/plugin-cloud-storage/utilities'
import { createRouteHandler } from 'uploadthing/next' import { UTApi } from 'uploadthing/server'
import { createUploadthing, UTApi } from 'uploadthing/server'
import { generateURL } from './generateURL.js' import { generateURL } from './generateURL.js'
import { getClientUploadRoute } from './getClientUploadRoute.js' import { getClientUploadRoute } from './getClientUploadRoute.js'
@@ -19,11 +19,18 @@ import { getHandleDelete } from './handleDelete.js'
import { getHandleUpload } from './handleUpload.js' import { getHandleUpload } from './handleUpload.js'
import { getHandler } from './staticHandler.js' import { getHandler } from './staticHandler.js'
export type FileRouterInputConfig = Parameters<ReturnType<typeof createUploadthing>>[0]
export type UploadthingStorageOptions = { export type UploadthingStorageOptions = {
/** /**
* Do uploads directly on the client, to bypass limits on Vercel. * Do uploads directly on the client, to bypass limits on Vercel.
*/ */
clientUploads?: ClientUploadsConfig clientUploads?:
| {
access?: ClientUploadsAccess
routerInputConfig?: FileRouterInputConfig
}
| boolean
/** /**
* Collection options to apply the adapter to. * Collection options to apply the adapter to.