Skip to content

Commit ff8c451

Browse files
committed
更新上传方式
1 parent c7434a0 commit ff8c451

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

bin/index.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const SparkMD5 = require('spark-md5');
55
const chalk = require('chalk');
66
const prompts = require('prompts');
77
const path = require('path');
8-
const FormData = require('form-data');
98
require('winston-daily-rotate-file');
109
const logger = require('../lib/log');
1110
const ProgressBar = require('progress');
@@ -29,26 +28,24 @@ process.on('uncaughtException', error => {
2928
const upload = async (filePath, parts = []) => {
3029
const totalChunk = Math.ceil(fileSize / CHUNK_SIZE);
3130

32-
const bar = new ProgressBar(':bar [:current/:total] :percent', { total: totalChunk });
31+
const bar = new ProgressBar(':bar [:current/:total] :percent ', { total: totalChunk });
3332
const uploadChunk = async (currentChunk, currentChunkIndex, parts, isRetry) => {
3433
if (parts.some(({ partNumber, size }) => partNumber === currentChunkIndex && size === currentChunk.length)) {
3534
bar.tick();
3635
return Promise.resolve();
3736
}
3837

39-
const form = new FormData();
40-
form.append('chunk', currentChunk, {
41-
filename: requestUrl.replace(/^http(s)?:\/\/.+?\/.+?\/.+?\//, '')
42-
});
4338
try {
4439
await _uploadChunk(requestUrl, {
4540
uploadId,
4641
version,
4742
partNumber: currentChunkIndex,
4843
size: currentChunk.length,
49-
form
44+
currentChunk
5045
}, {
51-
headers: form.getHeaders(),
46+
headers: {
47+
'Content-Type': 'application/octet-stream'
48+
},
5249
Authorization
5350
});
5451
bar.tick();
@@ -163,7 +160,7 @@ const getFileMD5 = async (filePath) => {
163160
console.log(`\n开始计算 MD5\n`)
164161
logger.info('开始计算 MD5')
165162

166-
const bar = new ProgressBar(':bar [:current/:total] :percent', { total: totalChunk });
163+
const bar = new ProgressBar(':bar [:current/:total] :percent ', { total: totalChunk });
167164
await new Promise(resolve => {
168165
stream = fs.createReadStream(filePath, { highWaterMark: CHUNK_SIZE })
169166
stream.on('data', chunk => {

lib/request.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,17 @@ const uploadChunk = (requestUrl, {
3636
version,
3737
partNumber,
3838
size,
39-
form,
39+
currentChunk,
4040
}, {
4141
headers,
4242
Authorization
4343
}) => {
44-
return axios.post(`${requestUrl}?version=${version}&uploadId=${uploadId}&partNumber=${partNumber}&size=${size}&action=part-upload`, form,
45-
{
46-
headers: { Authorization, ...headers }
47-
}
48-
)
44+
return axios({
45+
method: 'post',
46+
url: `${requestUrl}?version=${version}&uploadId=${uploadId}&partNumber=${partNumber}&size=${size}&action=part-upload`,
47+
data: currentChunk,
48+
headers: { Authorization, ...headers }
49+
})
4950
}
5051

5152
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "coding-generic",
3-
"version": "1.0.4",
3+
"version": "1.0.7",
44
"description": "",
55
"main": "index.js",
66
"bin": {

0 commit comments

Comments
 (0)