Skip to content

Commit

Permalink
fix(fetchDownloadInfo): fetching content info with GET
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed Nov 10, 2024
1 parent d13539e commit 4ac36ac
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ export default class DownloadEngineFetchStreamFetch extends BaseDownloadEngineFe
const fileName = parseContentDisposition(response.headers.get("content-disposition"));

let length = parseInt(response.headers.get("content-length")!) || 0;
if (method != "GET" && response.headers.get("content-encoding") || browserCheck() && MIN_LENGTH_FOR_MORE_INFO_REQUEST < length) {
length = acceptRange ? await this.fetchDownloadInfoWithoutRetryContentRange(url) : 0;
if (response.headers.get("content-encoding") || browserCheck() && MIN_LENGTH_FOR_MORE_INFO_REQUEST < length) {
length = acceptRange ? await this.fetchDownloadInfoWithoutRetryContentRange(url, method === "GET" ? response : undefined) : 0;
}

return {
Expand All @@ -95,8 +95,8 @@ export default class DownloadEngineFetchStreamFetch extends BaseDownloadEngineFe
};
}

protected async fetchDownloadInfoWithoutRetryContentRange(url: string) {
const responseGet = await fetch(url, {
protected async fetchDownloadInfoWithoutRetryContentRange(url: string, response?: Response) {
const responseGet = response ?? await fetch(url, {
method: "GET",
headers: {
accept: "*/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default class DownloadEngineFetchStreamXhr extends BaseDownloadEngineFetc
xhr.onload = async () => {
if (xhr.status >= 200 && xhr.status < 300) {
const contentLength = parseInt(xhr.getResponseHeader("content-length")!);
const length = MIN_LENGTH_FOR_MORE_INFO_REQUEST < contentLength && method != "GET" ? await this.fetchDownloadInfoWithoutRetryContentRange(url) : 0;
const length = MIN_LENGTH_FOR_MORE_INFO_REQUEST < contentLength ? await this.fetchDownloadInfoWithoutRetryContentRange(url, method === "GET" ? xhr : undefined) : 0;
const fileName = parseContentDisposition(xhr.getResponseHeader("content-disposition"));
const acceptRange = this.options.acceptRangeIsKnown ?? xhr.getResponseHeader("Accept-Ranges") === "bytes";

Expand All @@ -177,7 +177,16 @@ export default class DownloadEngineFetchStreamXhr extends BaseDownloadEngineFetc

}

protected fetchDownloadInfoWithoutRetryContentRange(url: string) {
protected fetchDownloadInfoWithoutRetryContentRange(url: string, xhrResponse?: XMLHttpRequest) {
const getSize = (xhr: XMLHttpRequest) => {
const contentRange = xhr.getResponseHeader("Content-Range");
return parseHttpContentRange(contentRange)?.size || 0;
};

if (xhrResponse) {
return getSize(xhrResponse);
}

return new Promise<number>((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
Expand All @@ -192,8 +201,7 @@ export default class DownloadEngineFetchStreamXhr extends BaseDownloadEngineFetc
}

xhr.onload = () => {
const contentRange = xhr.getResponseHeader("Content-Range");
resolve(parseHttpContentRange(contentRange)?.size || 0);
resolve(getSize(xhr));
};

xhr.onerror = () => {
Expand Down

0 comments on commit 4ac36ac

Please sign in to comment.