diff --git a/src/app/api/storage/presigned-url/[key]/route.ts b/src/app/api/storage/presigned-url/[key]/route.ts index 2b77455d..da506a22 100644 --- a/src/app/api/storage/presigned-url/[key]/route.ts +++ b/src/app/api/storage/presigned-url/[key]/route.ts @@ -7,7 +7,7 @@ import { cloudflareR2Client, cloudflareR2PutObjectCommandForKey, } from '@/services/storage/cloudflare-r2'; -import { STORAGE_PREFERENCE } from '@/site/config'; +import { CURRENT_STORAGE } from '@/site/config'; import { getSignedUrl } from '@aws-sdk/s3-request-presigner'; export const runtime = 'edge'; @@ -19,10 +19,10 @@ export async function GET( const session = await auth(); if (session?.user && key) { const url = await getSignedUrl( - STORAGE_PREFERENCE === 'cloudflare-r2' + CURRENT_STORAGE === 'cloudflare-r2' ? cloudflareR2Client() : awsS3Client(), - STORAGE_PREFERENCE === 'cloudflare-r2' + CURRENT_STORAGE === 'cloudflare-r2' ? cloudflareR2PutObjectCommandForKey(key) : awsS3PutObjectCommandForKey(key), { expiresIn: 3600 } diff --git a/src/services/storage/index.ts b/src/services/storage/index.ts index 64605928..2b58ebe3 100644 --- a/src/services/storage/index.ts +++ b/src/services/storage/index.ts @@ -13,7 +13,7 @@ import { isUrlFromAwsS3, } from './aws-s3'; import { - STORAGE_PREFERENCE, + CURRENT_STORAGE, HAS_AWS_S3_STORAGE, HAS_VERCEL_BLOB_STORAGE, HAS_CLOUDFLARE_R2_STORAGE, @@ -111,15 +111,15 @@ export const uploadFromClientViaPresignedUrl = async ( .then((response) => response.text()); return fetch(url, { method: 'PUT', body: file }) - .then(() => `${baseUrlForStorage(STORAGE_PREFERENCE)}/${key}`); + .then(() => `${baseUrlForStorage(CURRENT_STORAGE)}/${key}`); }; export const uploadPhotoFromClient = async ( file: File | Blob, extension = 'jpg', ) => ( - STORAGE_PREFERENCE === 'cloudflare-r2' || - STORAGE_PREFERENCE === 'aws-s3' + CURRENT_STORAGE === 'cloudflare-r2' || + CURRENT_STORAGE === 'aws-s3' ) ? uploadFromClientViaPresignedUrl(file, PREFIX_UPLOAD, extension, true) : vercelBlobUploadFromClient(file, `${PREFIX_UPLOAD}.${extension}`); diff --git a/src/site/SiteChecklistClient.tsx b/src/site/SiteChecklistClient.tsx index 39086660..98a20c6f 100644 --- a/src/site/SiteChecklistClient.tsx +++ b/src/site/SiteChecklistClient.tsx @@ -27,7 +27,8 @@ export default function SiteChecklistClient({ hasVercelBlobStorage, hasCloudflareR2Storage, hasAwsS3Storage, - storagePreference, + hasMultipleStorageProviders, + currentStorage, hasAuth, hasAdminUser, hasTitle, @@ -142,10 +143,11 @@ export default function SiteChecklistClient({ and connect to project diff --git a/src/site/config.ts b/src/site/config.ts index 1cc4b59e..c8f131f1 100644 --- a/src/site/config.ts +++ b/src/site/config.ts @@ -56,9 +56,15 @@ export const HAS_AWS_S3_STORAGE = (process.env.AWS_S3_ACCESS_KEY ?? '').length > 0 && (process.env.AWS_S3_SECRET_ACCESS_KEY ?? '').length > 0; -// Storage preference relies on client-only keys -// so that it's available in the browser when uploading -export const STORAGE_PREFERENCE: StorageType = +export const HAS_MULTIPLE_STORAGE_PROVIDERS = [ + HAS_VERCEL_BLOB_STORAGE, + HAS_CLOUDFLARE_R2_STORAGE, + HAS_AWS_S3_STORAGE, +].filter(Boolean).length > 1; + +// Storage preference requires client-available keys +// so it can be reached in the browser when uploading +export const CURRENT_STORAGE: StorageType = (process.env.NEXT_PUBLIC_STORAGE_PREFERENCE as StorageType | undefined) || ( HAS_CLOUDFLARE_R2_STORAGE_CLIENT ? 'cloudflare-r2' @@ -94,7 +100,8 @@ export const CONFIG_CHECKLIST_STATUS = { HAS_VERCEL_BLOB_STORAGE || HAS_CLOUDFLARE_R2_STORAGE || HAS_AWS_S3_STORAGE, - storagePreference: STORAGE_PREFERENCE, + hasMultipleStorageProviders: HAS_MULTIPLE_STORAGE_PROVIDERS, + currentStorage: CURRENT_STORAGE, hasAuth: (process.env.AUTH_SECRET ?? '').length > 0, hasAdminUser: ( (process.env.ADMIN_EMAIL ?? '').length > 0 &&