-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
146 lines (134 loc) · 3.49 KB
/
client.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**!
* sfs-client - lib/client.js
*
* Copyright(c) 2014
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
"use strict";
/**
* Module dependencies.
*/
var debug = require('debug')('sfs-client');
var urllib = require('urllib');
var formstream = require('formstream');
var fs = require('fs');
var thunkfiy = require('thunkify-wrap');
function Client(options) {
this._nodes = options.nodes;
this._nodeIndex = 0;
this._credentials = new Buffer(options.credentials.join(':')).toString('base64');
this._timeout = options.timeout || 600000;
thunkfiy(this);
}
Client.prototype._request = function (url, args, callback) {
var node = this._nodes[this._nodeIndex++];
if (this._nodeIndex >= this._nodes.length) {
this._nodeIndex = 0;
}
url = 'http://' + node.ip + ':' + node.port + url;
debug('%s %s', args.type, url);
args.headers = args.headers || {};
args.headers.Authorization = 'Basic ' + this._credentials;
args.timeout = args.timeout || this._timeout;
urllib.request(url, args, callback);
};
Client.prototype.upload = function (filepath, options, callback) {
var url = '/store';
var form = formstream();
form.file('file', filepath);
form.field('filename', options.key)
.field('shasum', options.shasum);
var args = {
type: 'POST',
headers: form.headers(),
stream: form,
dataType: 'json',
};
this._request(url, args, function (err, result) {
if (err) {
return callback(err);
}
result = result || {};
if (!result.ok) {
err = new Error('Upload ' + filepath + ' to ' + options.key + ' fail: ' + result.message);
return callback(err);
}
callback(null, {key: options.key});
});
};
Client.prototype.uploadBuffer = function (buf, options, callback) {
var url = '/store';
var form = formstream();
form.buffer('file', buf, options.key);
form.field('filename', options.key)
.field('shasum', options.shasum);
var args = {
type: 'POST',
headers: form.headers(),
stream: form,
dataType: 'json',
};
this._request(url, args, function (err, result) {
if (err) {
return callback(err);
}
result = result || {};
if (!result.ok) {
err = new Error('Upload buffer to ' + options.key + ' fail: ' + result.message);
return callback(err);
}
callback(null, {key: options.key});
});
};
Client.prototype.download = function (key, savePath, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
options = options || {};
if (key[0] === '/') {
key = key.substring(1);
}
var url = '/file/' + key;
var ws = fs.createWriteStream(savePath);
var args = {
type: 'GET',
writeStream: ws,
timeout: options.timeout,
};
this._request(url, args, callback);
};
Client.prototype.downloadStream = function (key, writeStream, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
options = options || {};
if (key[0] === '/') {
key = key.substring(1);
}
var url = '/file/' + key;
var args = {
type: 'GET',
writeStream: writeStream,
timeout: options.timeout,
};
this._request(url, args, callback);
};
Client.prototype.remove = function (key, callback) {
if (key[0] === '/') {
key = key.substring(1);
}
var url = '/file/' + key;
var args = {
type: 'DELETE',
dataType: 'json',
};
this._request(url, args, callback);
};
exports.create = function (options) {
return new Client(options);
};