fix(downloads): stop repeating an extension the name already carries

A voice comment's display name is the generated file name, extension
included, so appending the extension again downloaded it as
<uuid>.webm.webm. Both download paths, the single asset route and the
project zip, now append only when the name does not already end in it.
This commit is contained in:
yusufipk
2026-07-30 23:13:04 +07:00
parent e7008c571a
commit 93e85683e9
5 changed files with 69 additions and 8 deletions
@@ -10,6 +10,7 @@ import {
extractAudioFileNameFromProxyUrl,
extractVideoFileNameFromProxyUrl,
getVideoAssetAccessContext,
withFileExtension,
} from '@/lib/video-assets';
import { buildVideoObjectKey } from '@/lib/video-upload-validation';
import { logError } from '@/lib/logger';
@@ -55,6 +56,10 @@ function sanitizeFileName(value: string): string {
return sanitized.length > 0 ? sanitized : 'asset';
}
function withExtension(displayName: string, extension: string): string {
return withFileExtension(sanitizeFileName(displayName), extension);
}
function toAsciiFileName(value: string): string {
const normalized = value
.normalize('NFKD')
@@ -117,7 +122,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
if (!fileName) return apiErrors.badRequest('Invalid image asset URL');
const key = `images/${fileName}`;
const extension = fileName.includes('.') ? fileName.slice(fileName.lastIndexOf('.')) : '.png';
const downloadName = `${sanitizeFileName(asset.displayName)}${extension}`;
const downloadName = withExtension(asset.displayName, extension);
const contentDisposition = buildContentDisposition(downloadName);
return proxyR2MediaObject({
@@ -139,7 +144,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
if (!fileName) return apiErrors.badRequest('Invalid audio asset URL');
const key = `voice/${fileName}`;
const ext = fileName.includes('.') ? fileName.slice(fileName.lastIndexOf('.')) : '.webm';
const downloadName = `${sanitizeFileName(asset.displayName)}${ext}`;
const downloadName = withExtension(asset.displayName, ext);
const contentDisposition = buildContentDisposition(downloadName);
const extKey = ext.replace('.', '');
const contentType = AUDIO_CONTENT_TYPE_BY_EXTENSION[extKey] || 'audio/webm';
@@ -162,7 +167,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
if (!fileName) return apiErrors.badRequest('Invalid video asset URL');
const key = buildVideoObjectKey(fileName);
const ext = fileName.includes('.') ? fileName.slice(fileName.lastIndexOf('.')) : '.mp4';
const downloadName = `${sanitizeFileName(asset.displayName)}${ext}`;
const downloadName = withExtension(asset.displayName, ext);
const contentDisposition = buildContentDisposition(downloadName);
const extKey = ext.replace('.', '');
const contentType = VIDEO_CONTENT_TYPE_BY_EXTENSION[extKey] || 'video/mp4';