Skip to content

Commit

Permalink
Fix fileDownloadInfo fetching entirely the file when there is no cred… (
Browse files Browse the repository at this point in the history
#634)

…entials param

---------

Co-authored-by: Eliott C. <[email protected]>
  • Loading branch information
Kakulukian and coyotte508 authored Apr 18, 2024
1 parent 805488c commit 19b2871
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions packages/hub/src/lib/file-download-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ export async function fileDownloadInfo(params: {

const resp = await (params.fetch ?? fetch)(url, {
method: "GET",
headers: params.credentials
? {
Authorization: `Bearer ${params.credentials.accessToken}`,
Range: "bytes=0-0",
}
: {},
headers: {
...(params.credentials && {
Authorization: `Bearer ${params.credentials.accessToken}`,
}),
Range: "bytes=0-0",
},
});

if (resp.status === 404 && resp.headers.get("X-Error-Code") === "EntryNotFound") {
Expand All @@ -70,13 +70,14 @@ export async function fileDownloadInfo(params: {
throw new InvalidApiResponseFormatError("Expected ETag");
}

const sizeHeader = resp.headers.get("Content-Length");
const contentRangeHeader = resp.headers.get("content-range");

if (!sizeHeader) {
if (!contentRangeHeader) {
throw new InvalidApiResponseFormatError("Expected size information");
}

const size = parseInt(sizeHeader);
const [, parsedSize] = contentRangeHeader.split("/");
const size = parseInt(parsedSize);

if (isNaN(size)) {
throw new InvalidApiResponseFormatError("Invalid file size received");
Expand Down

0 comments on commit 19b2871

Please sign in to comment.