Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

fix(uploads): honor HTTP Range requests so local-storage video uploads work again

Gilad Resisi78515f71cf9d
diff --git a/apps/frontend/src/app/(app)/api/uploads/[[...path]]/route.ts b/apps/frontend/src/app/(app)/api/uploads/[[...path]]/route.ts
index 7dc3d678e..383c3211d 100644
--- a/apps/frontend/src/app/(app)/api/uploads/[[...path]]/route.ts
+++ b/apps/frontend/src/app/(app)/api/uploads/[[...path]]/route.ts
@@ -36,20 +36,43 @@ export const GET = async (
if (filePath !== base && !filePath.startsWith(base + sep)) {
return new NextResponse('Not found', { status: 404 });
}
- const response = createReadStream(filePath);
const fileStats = statSync(filePath);
const contentType = mime.getType(filePath) || 'application/octet-stream';
+
+ // Honor ranged requests: providers that push video in chunks (TikTok,
+ // YouTube, LinkedIn, X) fetch byte windows with a Range header and reject
+ // anything but a 206, so ignoring Range breaks their uploads.
+ const range = /^bytes=(\d+)-(\d*)$/.exec(request.headers.get('range') || '');
+ const start = range ? Number(range[1]) : 0;
+ const end = range && range[2] ? Number(range[2]) : fileStats.size - 1;
+
+ if (
+ range &&
+ (start >= fileStats.size || end >= fileStats.size || start > end)
+ ) {
+ return new NextResponse(null, {
+ status: 416,
+ headers: { 'Content-Range': `bytes */${fileStats.size}` },
+ });
+ }
+
+ const response = createReadStream(filePath, range ? { start, end } : {});
const iterator = nodeStreamToIterator(response);
const webStream = iteratorToStream(iterator);
return new Response(webStream, {
+ status: range ? 206 : 200,
headers: {
'Content-Type': contentType,
// Set the appropriate content-type header
- 'Content-Length': fileStats.size.toString(),
+ 'Content-Length': (end - start + 1).toString(),
// Set the content-length header
'Last-Modified': fileStats.mtime.toUTCString(),
// Set the last-modified header
'Cache-Control': 'public, max-age=31536000, immutable', // Example cache-control header
+ 'Accept-Ranges': 'bytes',
+ ...(range
+ ? { 'Content-Range': `bytes ${start}-${end}/${fileStats.size}` }
+ : {}),
},
});
};