mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
Subtitle tracks hang off a version rather than off a video, because re-editing a cut shifts every cue. The file always lands in our own S3-compatible storage whatever hosts the video, so a Bunny-hosted cut and an R2 one take the same path: both already play through our own video element, so a track element is all it takes. Uploads are normalised before they are stored. Whatever arrives, SRT or WebVTT, is parsed into cues and re-serialised as a canonical WebVTT file, and anything we did not understand is dropped rather than passed through. That is what makes it safe to serve a user-supplied text file from our own origin. Files saved out of Windows editors are decoded as windows-1254 or windows-1252 when they are not valid UTF-8, rather than refused. A YouTube version cannot carry an uploaded track, so the same CC menu drives YouTube's own captions through the iframe module API. The embed hides YouTube's controls, so until now those captions were unreachable even when the video had them. Uploading and deleting take the editor permission rather than the commenter one: a subtitle is part of the delivered cut, not a comment attachment.
92 lines
2.8 KiB
TypeScript
92 lines
2.8 KiB
TypeScript
import { NextRequest } from 'next/server';
|
|
import { auth, checkProjectAccess } from '@/lib/auth';
|
|
import { db } from '@/lib/db';
|
|
import { validateShareLinkAccess } from '@/lib/share-links';
|
|
import { getShareSessionFromRequest } from '@/lib/share-session';
|
|
import { apiErrors } from '@/lib/api-response';
|
|
import { proxyR2MediaObject } from '@/lib/r2-media-proxy';
|
|
import { logError } from '@/lib/logger';
|
|
import {
|
|
SAFE_SUBTITLE_FILENAME,
|
|
SUBTITLE_CONTENT_TYPE,
|
|
SUBTITLE_OBJECT_KEY_PREFIX,
|
|
subtitleFileNameToProxyUrl,
|
|
} from '@/lib/subtitle-validation';
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ filename: string }> }
|
|
) {
|
|
try {
|
|
const { filename } = await params;
|
|
|
|
// Validate filename to prevent path traversal
|
|
if (!SAFE_SUBTITLE_FILENAME.test(filename)) {
|
|
return apiErrors.badRequest('Invalid filename');
|
|
}
|
|
|
|
// Parallelize the DB lookup and session check to narrow the timing delta
|
|
// between "subtitle not found" and "subtitle found, access denied" responses.
|
|
const [subtitle, session] = await Promise.all([
|
|
db.videoSubtitle.findUnique({
|
|
where: { sourceUrl: subtitleFileNameToProxyUrl(filename) },
|
|
select: {
|
|
version: {
|
|
select: {
|
|
video: {
|
|
select: {
|
|
id: true,
|
|
projectId: true,
|
|
project: {
|
|
select: { id: true, ownerId: true, workspaceId: true, visibility: true },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
auth(),
|
|
]);
|
|
|
|
const video = subtitle?.version?.video ?? null;
|
|
if (!video) {
|
|
return apiErrors.forbidden('Access denied');
|
|
}
|
|
|
|
const access = await checkProjectAccess(video.project, session?.user?.id);
|
|
|
|
if (!access.hasAccess) {
|
|
const shareSession = getShareSessionFromRequest(request, video.id);
|
|
const shareAccess = shareSession
|
|
? await validateShareLinkAccess({
|
|
token: shareSession.token,
|
|
projectId: video.projectId,
|
|
videoId: video.id,
|
|
requiredPermission: 'VIEW',
|
|
passwordVerified: shareSession.passwordVerified,
|
|
})
|
|
: null;
|
|
|
|
if (!shareAccess?.hasAccess) {
|
|
return apiErrors.forbidden('Access denied');
|
|
}
|
|
}
|
|
|
|
return proxyR2MediaObject({
|
|
request,
|
|
key: `${SUBTITLE_OBJECT_KEY_PREFIX}${filename}`,
|
|
fallbackContentType: SUBTITLE_CONTENT_TYPE,
|
|
cacheControl: 'private, no-store',
|
|
extraHeaders: {
|
|
'X-Content-Type-Options': 'nosniff',
|
|
'Content-Security-Policy': "default-src 'none'; sandbox",
|
|
},
|
|
internalErrorMessage: 'Failed to retrieve subtitle',
|
|
});
|
|
} catch (error: unknown) {
|
|
logError('Error serving subtitle:', error);
|
|
return apiErrors.internalError('Failed to retrieve subtitle');
|
|
}
|
|
}
|