Vercel/app/api/storage/presigned-url/[key]/route.ts
2025-04-04 18:09:47 -05:00

38 lines
1.0 KiB
TypeScript

import { auth } from '@/auth/server';
import {
awsS3Client,
awsS3PutObjectCommandForKey,
} from '@/platforms/storage/aws-s3';
import {
cloudflareR2Client,
cloudflareR2PutObjectCommandForKey,
} from '@/platforms/storage/cloudflare-r2';
import { CURRENT_STORAGE } from '@/app/config';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
export async function GET(
_: Request,
{ params }: { params: Promise<{ key: string }> },
) {
const { key } = await params;
const session = await auth();
if (session?.user && key) {
const url = await getSignedUrl(
CURRENT_STORAGE === 'cloudflare-r2'
? cloudflareR2Client()
: awsS3Client(),
CURRENT_STORAGE === 'cloudflare-r2'
? cloudflareR2PutObjectCommandForKey(key)
: awsS3PutObjectCommandForKey(key),
{ expiresIn: 3600 },
);
return new Response(
url,
{ headers: { 'content-type': 'text/plain' } },
);
} else {
return new Response('Unauthorized request', { status: 401 });
}
}