import md5 from 'js-md5'

/**
 * 分段计算MD5
 * @param file {File}
 * @param options {Object} - onSuccess | onError
 */
export function generateMD5(file, options = {}) {
    const fileReader = new FileReader()
    fileReader.readAsBinaryString(file.file)
    fileReader.onload = (e) => {
        let fileBolb = e.target.result
        let fileMD5 = md5(fileBolb)
        console.log(`MD5计算完毕：${file.name} \nMD5：${fileMD5}`)
        // md5计算完毕
        if (options.onSuccess && typeof options.onSuccess == 'function') {
            options.onSuccess(fileMD5)
        }
    }

    fileReader.onerror = function () {
        console.log('MD5计算失败')
        if (options.onError && typeof options.onError == 'function') {
            options.onError()
        }
    }
}
