Skip to content

Commit a68cf79

Browse files
committed
fix: fix total file size calculation from external link URLs
1 parent 1fd5919 commit a68cf79

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

src/pages/DatasetDetailPage.tsx

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ const DatasetDetailPage: React.FC = () => {
143143
const [matches, setMatches] = useState<HTMLElement[]>([]);
144144
const [highlightedIndex, setHighlightedIndex] = useState(-1);
145145
const [downloadScript, setDownloadScript] = useState<string>("");
146+
const [downloadScriptSize, setDownloadScriptSize] = useState<number>(0);
147+
const [totalFileSize, setTotalFileSize] = useState<number>(0);
148+
146149
const [previewIsInternal, setPreviewIsInternal] = useState(false);
147150
const [isExternalExpanded, setIsExternalExpanded] = useState(true);
148151
const [expandedPaths, setExpandedPaths] = useState<string[]>([]);
@@ -166,11 +169,24 @@ const DatasetDetailPage: React.FC = () => {
166169
// };
167170

168171
// Dataset download button size calculation function
172+
// const formatSize = (sizeInBytes: number): string => {
173+
// if (sizeInBytes < 1024 * 1024) {
174+
// return `${(sizeInBytes / 1024).toFixed(1)} KB`;
175+
// }
176+
// return `${(sizeInBytes / 1024 / 1024).toFixed(2)} MB`;
177+
// };
169178
const formatSize = (sizeInBytes: number): string => {
170-
if (sizeInBytes < 1024 * 1024) {
179+
if (sizeInBytes < 1024) {
180+
return `${sizeInBytes} Bytes`;
181+
} else if (sizeInBytes < 1024 * 1024) {
171182
return `${(sizeInBytes / 1024).toFixed(1)} KB`;
183+
} else if (sizeInBytes < 1024 * 1024 * 1024) {
184+
return `${(sizeInBytes / (1024 * 1024)).toFixed(2)} MB`;
185+
} else if (sizeInBytes < 1024 * 1024 * 1024 * 1024) {
186+
return `${(sizeInBytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
187+
} else {
188+
return `${(sizeInBytes / (1024 * 1024 * 1024 * 1024)).toFixed(2)} TB`;
172189
}
173-
return `${(sizeInBytes / 1024 / 1024).toFixed(2)} MB`;
174190
};
175191

176192
// Recursive function to find `_DataLink_`
@@ -335,6 +351,16 @@ const DatasetDetailPage: React.FC = () => {
335351
const transformed = transformJsonForDisplay(datasetDocument);
336352
setTransformedDataset(transformed);
337353

354+
// ✅ Calculate total file size from size= query param
355+
let total = 0;
356+
links.forEach((link) => {
357+
const sizeMatch = link.url.match(/(?:[?&]size=)(\d+)/);
358+
if (sizeMatch && sizeMatch[1]) {
359+
total += parseInt(sizeMatch[1], 10);
360+
}
361+
});
362+
setTotalFileSize(total);
363+
338364
let totalSize = 0;
339365

340366
// 1️⃣ Sum external link sizes (from URL like ...?size=12345678)
@@ -401,6 +427,9 @@ const DatasetDetailPage: React.FC = () => {
401427
script += `curl -L --create-dirs "${url}" -o "${outputPath}"\n`;
402428
});
403429
setDownloadScript(script);
430+
// ✅ Calculate and set script size
431+
const scriptBlob = new Blob([script], { type: "text/plain" });
432+
setDownloadScriptSize(scriptBlob.size);
404433
}
405434
}, [datasetDocument]);
406435

@@ -755,7 +784,6 @@ const DatasetDetailPage: React.FC = () => {
755784
);
756785
}
757786
console.log("datasetDocument", datasetDocument);
758-
// const onekey = "README";
759787
const onekey = datasetDocument
760788
? datasetDocument.hasOwnProperty("README")
761789
? "README"
@@ -946,8 +974,13 @@ const DatasetDetailPage: React.FC = () => {
946974
"&:hover": { backgroundColor: Colors.secondaryPurple },
947975
}}
948976
>
949-
Script to Download All Files ({downloadScript.length} Bytes)
950-
(links: {externalLinks.length})
977+
{/* Script to Download All Files ({downloadScript.length} Bytes) */}
978+
Script to Download All Files ({formatSize(downloadScriptSize)})
979+
{/* (links: {externalLinks.length}) */}
980+
{externalLinks.length > 0 &&
981+
` (links: ${externalLinks.length}, total: ${formatSize(
982+
totalFileSize
983+
)})`}
951984
</Button>
952985

953986
<Box display="flex" alignItems="center" gap={1} sx={{ ml: "auto" }}>

0 commit comments

Comments
 (0)