Skip to content

Commit

Permalink
Fix workshop file size not handling other units than MB
Browse files Browse the repository at this point in the history
  • Loading branch information
tfedor committed Jun 18, 2024
1 parent fda9247 commit f89c8ae
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/js/Background/Modules/Community/SteamCommunityApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export default class SteamCommunityApi extends Api implements MessageHandlerInte
return this.fetchJson(url, {credentials: "include"});
}

/**
* @return Promise<number> result size in kB
*/
private async fetchWorkshopFileSize(id: number): Promise<number> {
const parser = new DOMParser();

Expand All @@ -43,17 +46,26 @@ export default class SteamCommunityApi extends Api implements MessageHandlerInte

const details = doc.querySelector(".detailsStatRight")?.textContent;

// FIXME Can't handle other units like KB
if (!details || !details.includes("MB")) {
if (!details) {
throw new Error(`Couldn't find details block for item id "${id}"`);
}

const m = details.match(/(\d+(?:[.,]\d+)?) (MB|KB|B)/);
if (!m) {
throw new Error(`Couldn't find details block for item id "${id}"`);
}

const size = parseFloat(details.replace(/,/g, ""));
const size = parseFloat(m[1]!.replace(/,/g, ""));
if (Number.isNaN(size)) {
throw new Error(`Invalid file size for item id "${id}"`);
}

return size*1000;
const unit = m[2]! as "MB"|"KB"|"B";
return size*({
"MB": 1000,
"KB": 1,
"B": 0.001
}[unit]);
}

private async getWorkshopFileSize(id: number, preventFetch: boolean): Promise<number|null> {
Expand Down

0 comments on commit f89c8ae

Please sign in to comment.