Skip to content

Commit bd17e26

Browse files
authored
Merge pull request #383 from qiniu/features/resumable-upload-v2
Features/resumable upload v2
2 parents 567882a + 61858a3 commit bd17e26

File tree

8 files changed

+420
-98
lines changed

8 files changed

+420
-98
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ node_js:
88

99
install:
1010
- npm install
11-
- npm install -g codecov
11+
- npm install -g codecov@3.8.1
1212
- npm install -g istanbul
1313

1414
before_script:

index.d.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,27 +285,50 @@ export declare namespace resume_up {
285285
/**
286286
* @default ''
287287
*/
288-
fname: string;
288+
fname?: string;
289289

290290
/**
291291
* @default {}
292292
*/
293-
params: any;
293+
params?: any;
294294

295295
/**
296296
* @default null
297297
*/
298298
mimeType?: string;
299299

300+
/**
301+
* @default null
302+
*/
303+
resumeRecordFile?: string
304+
305+
/**
306+
* @default null
307+
*/
308+
progressCallback?: (uploadBytes: number, totalBytes: number) => void
309+
310+
/**
311+
* @default v1
312+
*/
313+
version?: string
314+
315+
/**
316+
* @default 4 * 1024 * 1024
317+
*/
318+
partSize?: number
319+
300320
/**
301321
* 上传可选参数
302322
* @param fname 请求体中的文件的名称
303323
* @param params 额外参数设置,参数名称必须以x:开头
304324
* @param mimeType 指定文件的mimeType
305325
* @param resumeRecordFile
306326
* @param progressCallback
327+
* @param version 分片上传版本 目前支持v1/v2版本 默认v1
328+
* @param partSize 分片上传v2必传字段 默认大小为4MB 分片大小范围为1 MB - 1 GB
307329
*/
308-
constructor(fname?: string, params?: any, mimeType?: string, resumeRecordFile?: string, progressCallback?: (data: any) => void);
330+
constructor(fname?: string, params?: any, mimeType?: string, resumeRecordFile?: string, version?:string, partSize?:number,
331+
progressCallback?: (uploadBytes: number, totalBytes: number) => void);
309332
}
310333
}
311334

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
],
5151
"dependencies": {
5252
"agentkeepalive": "^4.0.2",
53+
"before": "^0.0.1",
5354
"block-stream2": "^2.0.0",
5455
"crc32": "^0.2.2",
5556
"destroy": "^1.0.4",

qiniu/conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exports.FormMimeUrl = 'application/x-www-form-urlencoded';
1717
exports.FormMimeJson = 'application/json';
1818
exports.FormMimeRaw = 'application/octet-stream';
1919
exports.RS_HOST = 'rs.qiniu.com';
20-
exports.RPC_TIMEOUT = 120000; // 120s
20+
exports.RPC_TIMEOUT = 600000; // 600s
2121
exports.UC_HOST = 'uc.qbox.me';
2222

2323
// proxy

qiniu/rpc.js

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ var urllib = require('urllib');
22
var conf = require('./conf');
33

44
exports.post = post;
5+
exports.put = put;
56
exports.postMultipart = postMultipart;
67
exports.postWithForm = postWithForm;
78
exports.postWithoutForm = postWithoutForm;
89

9-
function postMultipart (requestURI, requestForm, callbackFunc) {
10+
function postMultipart(requestURI, requestForm, callbackFunc) {
1011
return post(requestURI, requestForm, requestForm.headers(), callbackFunc);
1112
}
1213

13-
function postWithForm (requestURI, requestForm, token, callbackFunc) {
14+
function postWithForm(requestURI, requestForm, token, callbackFunc) {
1415
var headers = {
1516
'Content-Type': 'application/x-www-form-urlencoded'
1617
};
@@ -20,7 +21,7 @@ function postWithForm (requestURI, requestForm, token, callbackFunc) {
2021
return post(requestURI, requestForm, headers, callbackFunc);
2122
}
2223

23-
function postWithoutForm (requestURI, token, callbackFunc) {
24+
function postWithoutForm(requestURI, token, callbackFunc) {
2425
var headers = {
2526
'Content-Type': 'application/x-www-form-urlencoded'
2627
};
@@ -30,7 +31,7 @@ function postWithoutForm (requestURI, token, callbackFunc) {
3031
return post(requestURI, null, headers, callbackFunc);
3132
}
3233

33-
function post (requestURI, requestForm, headers, callbackFunc) {
34+
function post(requestURI, requestForm, headers, callbackFunc) {
3435
// var start = parseInt(Date.now() / 1000);
3536
headers = headers || {};
3637
headers['User-Agent'] = headers['User-Agent'] || conf.USER_AGENT;
@@ -42,7 +43,7 @@ function post (requestURI, requestForm, headers, callbackFunc) {
4243
dataType: 'json',
4344
timeout: conf.RPC_TIMEOUT,
4445
gzip: true
45-
// timing: true,
46+
// timing: true,
4647
};
4748

4849
if (conf.RPC_HTTP_AGENT) {
@@ -62,18 +63,47 @@ function post (requestURI, requestForm, headers, callbackFunc) {
6263
}
6364

6465
var req = urllib.request(requestURI, data, function (respErr, respBody,
65-
respInfo) {
66-
// var end = parseInt(Date.now() / 1000);
67-
// console.log((end - start) + " seconds");
68-
// console.log("queuing:\t" + respInfo.timing.queuing);
69-
// console.log("dnslookup:\t" + respInfo.timing.dnslookup);
70-
// console.log("connected:\t" + respInfo.timing.connected);
71-
// console.log("requestSent:\t" + respInfo.timing.requestSent);
72-
// console.log("waiting:\t" + respInfo.timing.waiting);
73-
// console.log("contentDownload:\t" + respInfo.timing.contentDownload);
74-
66+
respInfo) {
7567
callbackFunc(respErr, respBody, respInfo);
7668
});
7769

7870
return req;
7971
}
72+
73+
function put(requestURL, requestForm, headers, callbackFunc) {
74+
// var start = parseInt(Date.now() / 1000);
75+
headers = headers || {};
76+
headers['User-Agent'] = headers['User-Agent'] || conf.USER_AGENT;
77+
headers.Connection = 'keep-alive';
78+
79+
var data = {
80+
headers: headers,
81+
method: 'PUT',
82+
dataType: 'json',
83+
timeout: conf.RPC_TIMEOUT,
84+
gzip: true
85+
// timing: true,
86+
};
87+
88+
if (conf.RPC_HTTP_AGENT) {
89+
data.agent = conf.RPC_HTTP_AGENT;
90+
}
91+
92+
if (conf.RPC_HTTPS_AGENT) {
93+
data.httpsAgent = conf.RPC_HTTPS_AGENT;
94+
}
95+
96+
if (Buffer.isBuffer(requestForm) || typeof requestForm === 'string') {
97+
data.content = requestForm;
98+
} else if (requestForm) {
99+
data.stream = requestForm;
100+
} else {
101+
data.headers['Content-Length'] = 0;
102+
}
103+
104+
var req = urllib.request(requestURL, data, function (err, ret, info) {
105+
callbackFunc(err, ret, info);
106+
});
107+
108+
return req;
109+
}

0 commit comments

Comments
 (0)