Skip to content

Commit 5cc0e74

Browse files
authored
fix(storage-*): client uploads with disablePayloadAccessControl: true (#11530)
Fixes #11473 Previously, when `disablePayloadAccessControl: true` was defined, client uploads were working improperly. The reason is that `addDataAndFileToRequest` expects `staticHandler` to be defined and we don't add in case if `disablePayloadAccessControl: true`. This PR makes it so otherwise and if we have `clientUploads`, it pushes the "proxied" handler that responses only when the file was requested in the context of client upload (from `addDataAndFileToRequest`)
1 parent 6939a83 commit 5cc0e74

File tree

7 files changed

+28
-4
lines changed

7 files changed

+28
-4
lines changed

packages/plugin-cloud-storage/src/plugin.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ export const cloudStoragePlugin =
6060

6161
if (!options.disablePayloadAccessControl) {
6262
handlers.push(adapter.staticHandler)
63+
// Else if disablePayloadAccessControl: true and clientUploads is used
64+
// Build the "proxied" handler that responses only when the file was requested by client upload in addDataAndFileToRequest
65+
} else if (adapter.clientUploads) {
66+
handlers.push((req, args) => {
67+
if ('clientUploadContext' in args.params) {
68+
return adapter.staticHandler(req, args)
69+
}
70+
71+
// Otherwise still skip staticHandler
72+
return null
73+
})
6374
}
6475

6576
return {

packages/plugin-cloud-storage/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export type StaticHandler = (
6363
) => Promise<Response> | Response
6464

6565
export interface GeneratedAdapter {
66+
clientUploads?: ClientUploadsConfig
6667
/**
6768
* Additional fields to be injected into the base collection and image sizes
6869
*/

packages/storage-azure/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,13 @@ export const azureStorage: AzureStoragePlugin =
134134

135135
function azureStorageInternal(
136136
getStorageClient: () => ContainerClient,
137-
{ allowContainerCreate, baseURL, connectionString, containerName }: AzureStorageOptions,
137+
{
138+
allowContainerCreate,
139+
baseURL,
140+
clientUploads,
141+
connectionString,
142+
containerName,
143+
}: AzureStorageOptions,
138144
): Adapter {
139145
const createContainerIfNotExists = () => {
140146
void getStorageClientFunc({ connectionString, containerName }).createIfNotExists({
@@ -145,6 +151,7 @@ function azureStorageInternal(
145151
return ({ collection, prefix }): GeneratedAdapter => {
146152
return {
147153
name: 'azure',
154+
clientUploads,
148155
generateURL: getGenerateURL({ baseURL, containerName }),
149156
handleDelete: getHandleDelete({ collection, getStorageClient }),
150157
handleUpload: getHandleUpload({

packages/storage-gcs/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,12 @@ export const gcsStorage: GcsStoragePlugin =
128128

129129
function gcsStorageInternal(
130130
getStorageClient: () => Storage,
131-
{ acl, bucket }: GcsStorageOptions,
131+
{ acl, bucket, clientUploads }: GcsStorageOptions,
132132
): Adapter {
133133
return ({ collection, prefix }): GeneratedAdapter => {
134134
return {
135135
name: 'gcs',
136+
clientUploads,
136137
generateURL: getGenerateURL({ bucket, getStorageClient }),
137138
handleDelete: getHandleDelete({ bucket, getStorageClient }),
138139
handleUpload: getHandleUpload({

packages/storage-s3/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,12 @@ export const s3Storage: S3StoragePlugin =
142142

143143
function s3StorageInternal(
144144
getStorageClient: () => AWS.S3,
145-
{ acl, bucket, config = {} }: S3StorageOptions,
145+
{ acl, bucket, clientUploads, config = {} }: S3StorageOptions,
146146
): Adapter {
147147
return ({ collection, prefix }): GeneratedAdapter => {
148148
return {
149149
name: 's3',
150+
clientUploads,
150151
generateURL: getGenerateURL({ bucket, config }),
151152
handleDelete: getHandleDelete({ bucket, getStorageClient }),
152153
handleUpload: getHandleUpload({

packages/storage-uploadthing/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,15 @@ function uploadthingInternal(options: UploadthingStorageOptions): Adapter {
141141

142142
return (): GeneratedAdapter => {
143143
const {
144+
clientUploads,
144145
options: { acl = 'public-read', ...utOptions },
145146
} = options
146147

147148
const utApi = new UTApi(utOptions)
148149

149150
return {
150151
name: 'uploadthing',
152+
clientUploads,
151153
fields,
152154
generateURL,
153155
handleDelete: getHandleDelete({ utApi }),

packages/storage-vercel-blob/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,15 @@ function vercelBlobStorageInternal(
172172
options: { baseUrl: string } & VercelBlobStorageOptions,
173173
): Adapter {
174174
return ({ collection, prefix }): GeneratedAdapter => {
175-
const { access, addRandomSuffix, baseUrl, cacheControlMaxAge, token } = options
175+
const { access, addRandomSuffix, baseUrl, cacheControlMaxAge, clientUploads, token } = options
176176

177177
if (!token) {
178178
throw new Error('Vercel Blob storage token is required')
179179
}
180180

181181
return {
182182
name: 'vercel-blob',
183+
clientUploads,
183184
generateURL: getGenerateUrl({ baseUrl, prefix }),
184185
handleDelete: getHandleDelete({ baseUrl, prefix, token }),
185186
handleUpload: getHandleUpload({

0 commit comments

Comments
 (0)