Skip to content

Commit

Permalink
range
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx committed Jan 25, 2025
1 parent ad9367f commit e23184e
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions app/api/bucket/[bucket]/[...path]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ export const runtime = 'edge';
type Params = { bucket: string; path: string[] };

export const GET = async (
_req: Request,
req: Request,
{ params: { bucket: bucketName, path } }: { params: Params },
) => {
const bucket = await getBucket(bucketName);
if (!bucket) {
return new Response('Unable to read bucket', { status: 400 });
}

const object = await bucket.get(path.join('/'));
const [, start, end] = /^bytes=(\d+)-(\d+)?$/.exec(req.headers.get('range') ?? '') ?? [];

const object = await bucket.get(path.join('/'), {
...(start && { range: { offset: Number(start), length: end ? Number(end) : undefined } }),
});

if (!object) {
return new Response('Not found', { status: 404 });
Expand All @@ -28,6 +32,20 @@ export const GET = async (
headers.set('cache-control', cacheControlSetting.value);
}

console.log(object.range);
const test = new Headers();
object.writeHttpMetadata(test);
console.log(Object.fromEntries(test.entries()));

if (start && object.range && 'offset' in object.range && object.range.offset !== undefined) {
headers.set(
'content-range',
`bytes ${object.range.offset}-${
object.range.length ? object.range.offset + object.range.length : object.size
}/*`,
);
}

// object.writeHttpMetadata does not work properly with Miniflare in next dev
if (object.httpMetadata?.contentDisposition)
headers.set('content-disposition', object.httpMetadata?.contentDisposition);
Expand All @@ -37,6 +55,7 @@ export const GET = async (
headers.set('content-language', object.httpMetadata?.contentLanguage);
if (object.httpMetadata?.contentType)
headers.set('content-type', object.httpMetadata?.contentType);
if (object.httpMetadata?.contentType?.startsWith('video')) headers.set('accept-ranges', 'bytes');

headers.set('etag', object.httpEtag);

Expand Down

0 comments on commit e23184e

Please sign in to comment.