Skip to content

Commit 12a602f

Browse files
committed
将uploader在commandx中实现
1 parent 1dceb79 commit 12a602f

File tree

4 files changed

+98
-5
lines changed

4 files changed

+98
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@
1818
"url": "https://github.com/aliyun-node/commands/issues"
1919
},
2020
"homepage": "https://github.com/aliyun-node/commands#readme",
21-
"dependencies": {}
21+
"dependencies": {
22+
"formstream": "^1.0.0",
23+
"urllib": "^2.2.2"
24+
}
2225
}

upload_file

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
#!/bin/bash
22

3-
UPLOADER=`which uploader`
3+
export ENABLE_NODE_LOG=NO
4+
5+
NODE=`which node`
46

57
if [ -f "$HOME/.nodepath" ]; then
6-
UPLOADER=`cat $HOME/.nodepath`/uploader
8+
NODE=`cat $HOME/.nodepath`/node;
79
fi
810

911
FULL_PATH=$2
1012
TARGET_PATH=$2
1113

1214
GZIP=`which gzip`
1315

14-
if [ $GZIP ]; then
16+
if [ $GZIP ] && [ -f "$FULL_PATH" ]; then
1517
$GZIP -c $FULL_PATH > $FULL_PATH".gz"
1618
TARGET_PATH=$FULL_PATH".gz"
1719
fi
1820

21+
UPLOADER=`pwd`/uploader.js
22+
1923
#uploader server filepath token id
20-
ENABLE_NODE_LOG=NO $UPLOADER $1 $TARGET_PATH $3 $4
24+
$NODE UPLOADER $1 $TARGET_PATH $3 $4

uploader.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
3+
// ./uploader server filepath token id
4+
var fs = require('fs');
5+
var os = require('os');
6+
var crypto = require('crypto');
7+
var urllib = require('urllib');
8+
var formstream = require('formstream');
9+
var tunnel = require('tunnel-agent');
10+
11+
var argv = process.argv.slice(2);
12+
13+
var server = argv[0];
14+
var filepath = argv[1];
15+
var token = argv[2];
16+
var id = argv[3];
17+
18+
// check args
19+
if (!server || !filepath || !token || !id) {
20+
console.log('参数错误');
21+
console.log('\t usage: ./uploader server filepath token id');
22+
process.exit(1);
23+
}
24+
25+
// check filepath
26+
fs.stat(filepath, function (err, stat) {
27+
if (err) {
28+
console.log('文件不存在');
29+
console.log(err.message);
30+
process.exit(1);
31+
return;
32+
}
33+
34+
if (stat.size <= 0) {
35+
console.log('空文件');
36+
process.exit(1);
37+
return;
38+
}
39+
40+
var form = formstream();
41+
form.file('file', filepath, filepath, stat.size);
42+
43+
var nonce = '' + parseInt((Math.random() * 100000000000), 10);
44+
// get signature
45+
var shasum = crypto.createHash('sha1');
46+
shasum.update([os.hostname(), token, nonce, id].join(''));
47+
var sign = shasum.digest('hex');
48+
49+
var url = 'http://' + server + '/files/' + id + '?nonce=' + nonce + '&sign=' + sign;
50+
51+
var gateway = process.env.GATEWAY;
52+
if (gateway) {
53+
// upload to gateway
54+
url = 'http://' + gateway + '/file?target=' + encodeURIComponent(url);
55+
}
56+
57+
var agent = false;
58+
if (process.env.http_proxy) {
59+
var parts = process.env.http_proxy.split(':');
60+
agent = tunnel.httpOverHttp({
61+
proxy: {
62+
host: parts[0],
63+
port: parts[1]
64+
}
65+
});
66+
}
67+
68+
var opts = {
69+
dataType: 'json',
70+
type: 'POST',
71+
timeout: 60000 * 20, // 20分钟超时
72+
headers: form.headers(),
73+
stream: form,
74+
agent: agent
75+
};
76+
77+
urllib.request(url, opts, function (err, data, res) {
78+
if (err) {
79+
throw err;
80+
}
81+
82+
console.log(JSON.stringify(data));
83+
process.exit(0);
84+
});
85+
});

0 commit comments

Comments
 (0)