Skip to content

Commit cb744a0

Browse files
committed
feat: file size format
1 parent 09b027f commit cb744a0

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/lib/file.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export const FileUtil = {
117117
throw new Error("SparkMD5 not found");
118118
}
119119
const reader = stream.getReader();
120-
const spark = new SparkMD5.ArrayBuffer();
120+
const spark: any = new SparkMD5.ArrayBuffer();
121121

122122
return new Promise((resolve, reject) => {
123123
function processChunk() {
@@ -142,4 +142,11 @@ export const FileUtil = {
142142
processChunk();
143143
});
144144
},
145+
formatSize: (bytes: number) => {
146+
if (bytes === 0) return '0 Bytes';
147+
const k = 1024;
148+
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
149+
const i = Math.floor(Math.log(bytes) / Math.log(k));
150+
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
151+
},
145152
};

0 commit comments

Comments
 (0)