/* eslint no-useless-escape:0 */ import * as SparkMD5 from 'spark-md5'; import storage from './storage'; export const qiniuUrl = (key: string, url: string): string => `${url}/${key}`; export const fileSize = (bytes: number): string => { 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: string, mode: number, height: number): string => `${url}?imageView2/${mode}/h/${height}`; export const computeChecksumMd5 = (file: 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: number): void { const chunk_end = Math.min(file.size, chunk_start + chunkSize); fileReader.readAsArrayBuffer(file.slice(chunk_start, chunk_end)); } fileReader.onload = (e: any) => { 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); }); }; export const saveNecessaryData = (token: string, grpcHost: string, clientID: string) => { storage.clientID = clientID; storage.grpcHost = grpcHost; storage.token = token; }