diff --git a/bin/index.js b/bin/index.js index cc0b2ee..b167bb1 100755 --- a/bin/index.js +++ b/bin/index.js @@ -1,7 +1,7 @@ #!/usr/bin/env node const fs = require('fs'); -const SparkMD5 = require('spark-md5'); +const crypto = require('crypto'); const chalk = require('chalk'); const prompts = require('prompts'); const path = require('path'); @@ -47,7 +47,7 @@ const upload = async (filePath, parts = []) => { version, partNumber: currentChunkIndex, size: currentChunk.length, - currentChunk + currentChunk }, { headers: { 'Content-Type': 'application/octet-stream' @@ -111,10 +111,6 @@ const upload = async (filePath, parts = []) => { return; } - - - - const merge = async () => { console.log(chalk.cyan('正在合并分片,请稍等...')) return await _mergeAllChunks(requestUrl, { @@ -126,7 +122,6 @@ const upload = async (filePath, parts = []) => { Authorization }); } - try { const res = await withRetry(merge, 3, 500); @@ -169,7 +164,6 @@ const getFileMD5Success = async (filePath) => { logger.error(error.message); logger.error(error.stack); console.log(chalk.red((error.response && error.response.data) || error.message)); - return; } } @@ -179,7 +173,7 @@ const getFileMD5 = async (filePath) => { chunkSize = Math.ceil(fileSize / MAX_CHUNK); totalChunk = Math.ceil(fileSize / chunkSize); } - const spark = new SparkMD5.ArrayBuffer(); + const hash = crypto.createHash('md5'); try { console.log(`\n开始计算 MD5\n`) logger.info('开始计算 MD5') @@ -189,14 +183,14 @@ const getFileMD5 = async (filePath) => { stream = fs.createReadStream(filePath, { highWaterMark: chunkSize }) stream.on('data', chunk => { bar.tick(); - spark.append(chunk) + hash.update(chunk) }) stream.on('error', error => { reject('读取文件分片异常,请重新执行命令继续上传'); }) stream.on('end', async () => { - md5 = spark.end(); - spark.destroy(); + md5 = hash.digest('hex'); + hash.destroy(); console.log(`\n文件 MD5:${md5}\n`) await getFileMD5Success(filePath); resolve(); @@ -208,7 +202,6 @@ const getFileMD5 = async (filePath) => { console.log(chalk.red((error.response && error.response.data) || error.message)); logger.error(error.message); logger.error(error.stack); - return; } } diff --git a/package.json b/package.json index 3aba59a..f130477 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coding-generic", - "version": "1.2.0", + "version": "1.3.0", "description": "", "main": "index.js", "bin": { @@ -17,7 +17,6 @@ "form-data": "^3.0.0", "progress": "^2.0.3", "prompts": "^2.3.2", - "spark-md5": "^3.0.1", "winston": "^3.3.3", "winston-daily-rotate-file": "^4.5.0", "yargs": "^16.0.3" diff --git a/yarn.lock b/yarn.lock index f742e0d..2ee214d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -708,11 +708,6 @@ sisteransi@^1.0.5: resolved "https://registry.npm.taobao.org/sisteransi/download/sisteransi-1.0.5.tgz#134d681297756437cc05ca01370d3a7a571075ed" integrity sha1-E01oEpd1ZDfMBcoBNw06elcQde0= -spark-md5@^3.0.1: - version "3.0.1" - resolved "https://registry.npm.taobao.org/spark-md5/download/spark-md5-3.0.1.tgz#83a0e255734f2ab4e5c466e5a2cfc9ba2aa2124d" - integrity sha1-g6DiVXNPKrTlxGblos/JuiqiEk0= - sshpk@^1.7.0: version "1.16.1" resolved "https://registry.npm.taobao.org/sshpk/download/sshpk-1.16.1.tgz#fb661c0bef29b39db40769ee39fa70093d6f6877"