Skip to content

Fix: Correctly generate thumbnail URLs for path-style and virtual-hosted S3 providers #712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions apps/web/app/api/thumbnail/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export async function GET(request: NextRequest) {
return new Response(
JSON.stringify({ error: true, message: "Video does not exist" }),
{
status: 401,
status: 404,
headers: getHeaders(origin),
}
);
Expand All @@ -52,51 +52,53 @@ export async function GET(request: NextRequest) {
return new Response(
JSON.stringify({ error: true, message: "Video not found" }),
{
status: 401,
status: 404,
headers: getHeaders(origin),
}
);
}

const { video } = result;
const prefix = `${userId}/${videoId}/`;

const screenshotPath = `${prefix}screenshot/screen-capture.jpg`;
let thumbnailUrl: string;

if (!result.bucket || video.awsBucket === serverEnv().CAP_AWS_BUCKET) {
thumbnailUrl = `${S3_BUCKET_URL}/${prefix}screenshot/screen-capture.jpg`;
const usePathStyle = serverEnv().S3_PATH_STYLE;
const customBucketUrl = serverEnv().CAP_AWS_BUCKET_URL;
const endpoint = serverEnv().CAP_AWS_ENDPOINT;

// Handle path-style URLs
if (usePathStyle && endpoint && video.awsBucket) {
thumbnailUrl = `${endpoint}/${video.awsBucket}/${screenshotPath}`;
return new Response(JSON.stringify({ screen: thumbnailUrl }), {
status: 200,
headers: getHeaders(origin),
});
}

const bucketProvider = await createBucketProvider(result.bucket);

try {
const listResponse = await bucketProvider.listObjects({
prefix: prefix,
// Handle virtual-hosted/subdomain style URLs
if (!usePathStyle && customBucketUrl) {
thumbnailUrl = `${customBucketUrl}/${screenshotPath}`;
return new Response(JSON.stringify({ screen: thumbnailUrl }), {
status: 200,
headers: getHeaders(origin),
});
const contents = listResponse.Contents || [];
}

const thumbnailKey = contents.find((item: any) =>
item.Key?.endsWith("screen-capture.jpg")
)?.Key;

if (!thumbnailKey) {
return new Response(
JSON.stringify({
error: true,
message: "No thumbnail found for this video",
}),
{
status: 404,
headers: getHeaders(origin),
}
);
}
// Fallback if no custom URL/endpoint is configured
if (!result.bucket || video.awsBucket === serverEnv().CAP_AWS_BUCKET) {
thumbnailUrl = `${S3_BUCKET_URL}/${screenshotPath}`;
return new Response(JSON.stringify({ screen: thumbnailUrl }), {
status: 200,
headers: getHeaders(origin),
});
}

thumbnailUrl = await bucketProvider.getSignedObjectUrl(thumbnailKey);
// Fallback for other custom S3 setups that require pre-signed URLs
const bucketProvider = await createBucketProvider(result.bucket);
try {
thumbnailUrl = await bucketProvider.getSignedObjectUrl(screenshotPath);
} catch (error) {
return new Response(
JSON.stringify({
Expand Down