fix:unsigned urls when storing in db

This commit is contained in:
2025-10-14 16:05:39 +02:00
parent bf542e6b76
commit b4515d1d6a
2 changed files with 10 additions and 23 deletions

View File

@@ -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 };
}

View File

@@ -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 {