From b4515d1d6ae142d970a15f6244f683b729a16321 Mon Sep 17 00:00:00 2001 From: Zias van Nes Date: Tue, 14 Oct 2025 16:05:39 +0200 Subject: [PATCH] fix:unsigned urls when storing in db --- src/lib/server/media-processor.ts | 17 +++++------------ src/routes/api/finds/+server.ts | 16 +++++----------- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/src/lib/server/media-processor.ts b/src/lib/server/media-processor.ts index 8760e9b..014c015 100644 --- a/src/lib/server/media-processor.ts +++ b/src/lib/server/media-processor.ts @@ -1,5 +1,5 @@ import sharp from 'sharp'; -import { uploadToR2, getSignedR2Url } from './r2'; +import { uploadToR2 } from './r2'; const THUMBNAIL_SIZE = 400; const MAX_IMAGE_SIZE = 1920; @@ -46,13 +46,8 @@ export async function processAndUploadImage( uploadToR2(thumbFile, `${filename}-thumb.jpg`, 'image/jpeg') ]); - // Generate signed URLs (24 hour expiration for images) - const [url, thumbnailUrl] = await Promise.all([ - getSignedR2Url(imagePath, 24 * 60 * 60), // 24 hours - getSignedR2Url(thumbPath, 24 * 60 * 60) // 24 hours - ]); - - return { url, thumbnailUrl }; + // Return the R2 paths (not signed URLs) - signed URLs will be generated when needed + return { url: imagePath, thumbnailUrl: thumbPath }; } export async function processAndUploadVideo( @@ -66,12 +61,10 @@ export async function processAndUploadVideo( // Upload video directly (no processing on server to save resources) const videoPath = await uploadToR2(file, filename, 'video/mp4'); - // Generate signed URL for video (24 hour expiration) - const url = await getSignedR2Url(videoPath, 24 * 60 * 60); - // For video thumbnail, generate on client-side or use placeholder // This keeps server-side processing minimal const thumbnailUrl = `/video-placeholder.jpg`; // Use static placeholder - return { url, thumbnailUrl }; + // Return the R2 path (not signed URL) - signed URLs will be generated when needed + return { url: videoPath, thumbnailUrl }; } diff --git a/src/routes/api/finds/+server.ts b/src/routes/api/finds/+server.ts index 52aca78..f2ae08a 100644 --- a/src/routes/api/finds/+server.ts +++ b/src/routes/api/finds/+server.ts @@ -123,18 +123,12 @@ export const GET: RequestHandler = async ({ url, locals }) => { // Generate signed URLs for all media items const mediaWithSignedUrls = await Promise.all( findMedia.map(async (mediaItem) => { - // Extract path from URL if it's still a full URL, otherwise use as-is - const path = mediaItem.url.startsWith('https://') - ? mediaItem.url.split('/').slice(3).join('/') - : mediaItem.url; - - const thumbnailPath = mediaItem.thumbnailUrl?.startsWith('https://') - ? mediaItem.thumbnailUrl.split('/').slice(3).join('/') - : mediaItem.thumbnailUrl; - + // URLs in database are now paths, generate signed URLs directly const [signedUrl, signedThumbnailUrl] = await Promise.all([ - getSignedR2Url(path, 24 * 60 * 60), // 24 hours - thumbnailPath ? getSignedR2Url(thumbnailPath, 24 * 60 * 60) : Promise.resolve(null) + getSignedR2Url(mediaItem.url, 24 * 60 * 60), // 24 hours + mediaItem.thumbnailUrl && !mediaItem.thumbnailUrl.startsWith('/') + ? getSignedR2Url(mediaItem.thumbnailUrl, 24 * 60 * 60) + : Promise.resolve(mediaItem.thumbnailUrl) // Keep static placeholder paths as-is ]); return {