fix: avoid object head precheck for watermarked media

This commit is contained in:
FengLee
2026-06-06 00:04:02 +08:00
parent f3d5135e0b
commit 23c13f8274
3 changed files with 22 additions and 15 deletions

View File

@@ -116,6 +116,11 @@ await runTest('local storage route uses watermark access instead of exposing raw
assert.match(source, /shouldWatermarkStorageResponse\(/);
assert.match(source, /serveWatermarkedStorageFile\(/);
assert.doesNotMatch(
source,
/shouldWatermarkStorageResponse[\s\S]+?fileExistsAsync\(/,
'storage display route should not require a slow object HEAD before watermark rendering',
);
});
await runTest('download route applies watermark and checks authenticated no-watermark entitlement', () => {

View File

@@ -122,20 +122,21 @@ async function downloadLocalStorageFile(
const mayAccessOriginal = canAccessOriginalMedia(watermarkAccess);
if (shouldWatermark) {
if (!await localStorage.fileExistsAsync(key)) {
try {
const watermarked = await serveWatermarkedDownloadFile(key, contentType);
return buildDownloadResponse(
watermarked.buffer.buffer.slice(
watermarked.buffer.byteOffset,
watermarked.buffer.byteOffset + watermarked.buffer.byteLength,
) as ArrayBuffer,
watermarked.contentType,
filename,
watermarked.buffer.byteLength,
disposition,
);
} catch {
return NextResponse.json({ error: '文件不存在' }, { status: 404 });
}
const watermarked = await serveWatermarkedDownloadFile(key, contentType);
return buildDownloadResponse(
watermarked.buffer.buffer.slice(
watermarked.buffer.byteOffset,
watermarked.buffer.byteOffset + watermarked.buffer.byteLength,
) as ArrayBuffer,
watermarked.contentType,
filename,
watermarked.buffer.byteLength,
disposition,
);
}
const shouldTryObjectRedirect = contentType.startsWith('video/') || !localStorage.fileExists(key);

View File

@@ -18,11 +18,12 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{
const contentType = getContentType(filePath);
if (shouldWatermarkStorageResponse(filePath, contentType, null)) {
if (!await localStorage.fileExistsAsync(filePath)) {
try {
const watermarked = await serveWatermarkedStorageFile(filePath, contentType);
return serveLocalBuffer(watermarked.key, watermarked.buffer, WATERMARK_CACHE_CONTROL, watermarked.contentType);
} catch {
return NextResponse.json({ error: 'File not found' }, { status: 404 });
}
const watermarked = await serveWatermarkedStorageFile(filePath, contentType);
return serveLocalBuffer(watermarked.key, watermarked.buffer, WATERMARK_CACHE_CONTROL, watermarked.contentType);
}
if (filePath.startsWith('thumbnails/')) {