-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathextend.js
More file actions
89 lines (78 loc) · 2.6 KB
/
extend.js
File metadata and controls
89 lines (78 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
feather.file = require('./lib/file.js');
feather.console = require('./lib/console.js');
var util = require('./lib/util.js');
for(var method in util){
feather.util[method] = util[method];
}
fis.util.upload = function(url, opt, data, content, subpath, callback, l){
if(typeof content === 'string'){
content = new Buffer(content, 'utf8');
} else if(!(content instanceof Buffer)){
fis.log.error('unable to upload content [' + (typeof content) + ']');
}
data = data || {};
var endl = '\r\n';
var boundary = '-----np' + Math.random();
var collect = [];
fis.util.map(data, function(key, value){
collect.push('--' + boundary + endl);
collect.push('Content-Disposition: form-data; name="' + key + '"' + endl);
collect.push(endl);
collect.push(value + endl);
});
collect.push('--' + boundary + endl);
collect.push('Content-Disposition: form-data; name="file"; filename="' + subpath + '"' + endl);
collect.push(endl);
collect.push(content);
collect.push('--' + boundary + '--' + endl);
var length = 0;
collect.forEach(function(ele){
length += ele.length;
});
opt = opt || {};
opt.method = opt.method || 'POST';
opt.headers = {
'Content-Type': 'multipart/form-data; boundary=' + boundary
};
if(l){
opt.headers['Content-length'] = length;
}
opt = fis.util.parseUrl(url, opt);
var http = opt.protocol === 'https:' ? require('https') : require('http');
var req = http.request(opt, function(res){
var status = res.statusCode;
var body = '';
res
.on('data', function(chunk){
body += chunk;
})
.on('end', function(){
if(status >= 200 && status < 300 || status === 304){
callback(null, body);
} else {
if(status == 411 && !l){
fis.util.upload(url, opt, data, content, subpath, callback, true);
}else{
callback(status);
}
}
})
.on('error', function(err){
callback(err.message || err);
});
});
req.on('error', function(err){
if(!l){
fis.util.upload(url, opt, data, content, subpath, callback, true);
}else{
throw new Error('deploy error: ' + err.message);
}
});
collect.forEach(function(d){
req.write(d);
if(d instanceof Buffer){
req.write(endl);
}
});
req.end();
}