/* eslint no-useless-escape:0 */
import * as SparkMD5 from 'spark-md5';
export const qiniuUrl = (key, url) => `${url}/${key}`;
export const fileSize = (bytes) => {
    const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
    if (bytes === 0)
        return 'N/A';
    const i = Math.floor(Math.log(bytes) / Math.log(1024));
    return `${(bytes / 1024 ** i).toFixed(1)}${sizes[i]}`;
};
export const qiniuImageView = (url, mode, height) => `${url}?imageView2/${mode}/h/${height}`;
export const computeChecksumMd5 = (file) => {
    return new Promise((resolve, reject) => {
        const chunkSize = 2097152; // Read in chunks of 2MB
        const spark = new SparkMD5.ArrayBuffer();
        const fileReader = new FileReader();
        let cursor = 0; // current cursor in file
        fileReader.onerror = (e) => {
            reject(e);
        };
        // read chunk starting at `cursor` into memory
        function processChunk(chunk_start) {
            const chunk_end = Math.min(file.size, chunk_start + chunkSize);
            fileReader.readAsArrayBuffer(file.slice(chunk_start, chunk_end));
        }
        fileReader.onload = (e) => {
            spark.append(e.target.result); // Accumulate chunk to md5 computation
            cursor += chunkSize; // Move past this chunk
            if (cursor < file.size) {
                processChunk(cursor);
            }
            else {
                resolve(btoa(spark.end(true)));
            }
        };
        processChunk(0);
    });
};
//# sourceMappingURL=utils.jsx.map