Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
使用内建 crypto 算 md5
  • Loading branch information
Kagashino committed Feb 5, 2021
commit e14a56c07688f0e80745e6e11a31d95fb1b905bf
19 changes: 6 additions & 13 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -47,7 +47,7 @@ const upload = async (filePath, parts = []) => {
version,
partNumber: currentChunkIndex,
size: currentChunk.length,
currentChunk
currentChunk
}, {
headers: {
'Content-Type': 'application/octet-stream'
Expand Down Expand Up @@ -111,10 +111,6 @@ const upload = async (filePath, parts = []) => {
return;
}





const merge = async () => {
console.log(chalk.cyan('正在合并分片,请稍等...'))
return await _mergeAllChunks(requestUrl, {
Expand All @@ -126,7 +122,6 @@ const upload = async (filePath, parts = []) => {
Authorization
});
}


try {
const res = await withRetry(merge, 3, 500);
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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')
Expand All @@ -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();
Expand All @@ -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;
}
}

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down