-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
167 lines (143 loc) · 4.22 KB
/
index.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var request = require('request'),
fs = require('fs'),
SimpleTokenClient = require('simple-token-client'),
os = require('os'),
pathUtil = require('path'),
uuid = require('node-uuid'),
urlUtil = require('url');
function StorageApi (config) {
this.config = config;
this.downloadDir = os.tmpdir();
this.tokenClient = new SimpleTokenClient(config);
}
function jsonResponseHandler (done) {
return function (err, res, body) {
if(err) {
return done(err);
}
try {
body = JSON.parse(body) || body;
}
catch (ex) {
return done(new Error('response is not json'));
}
if(res.statusCode !== 200) {
err = new Error(body.errmsg);
err.code = body.errcode;
return done(err);
}
done(null, body);
};
}
function postFile (url, options, formData, files, done) {
var opts = {
url: url,
proxy: false
};
opts = _.merge(opts, options);
var req = request['post'](opts, jsonResponseHandler(done));
var form = req.form();
_.each(formData, function (val, key) {
form.append(key, val);
});
_.each(files, function (val, key) {
form.append(key, fs.createReadStream(val.path), {filename: val.filename});
});
}
StorageApi.prototype.uploadFile = function (bucket, path, file, userinfo, done) {
var self = this;
self.tokenClient.getToken(function (err, token) {
if(err) {
return done(new Error('failed to get access token:' + err.message));
}
postFile(self.config.endpoint + '/api/v1/file',
{
headers : {
Authorization: 'Bearer ' + token
}
},
{
bucket: bucket,
path: path,
userinfo: JSON.stringify(userinfo)
},
{
file: {
path: file.path,
filename: file.originalFilename
}
},
done);
});
};
var filenamePattern = /filename=\"(.*)\"/gi;
StorageApi.prototype.downloadFile = function (url, done) {
var self = this;
self.tokenClient.getToken(function (err, token) {
if(err) {
return done(new Error('failed to get access token:' + err.message));
}
request({
url: url,
headers: {
Authorization: 'Bearer ' + token
}
}).on('response', function (res) {
if(res.statusCode !== 200) {
return done(new Error('download file get status:' + res.statusCode));
}
var contentDisposition = res.headers['content-disposition'];
var filename = contentDisposition ? (filenamePattern.exec(contentDisposition) || [])[1] : '';
var extname = pathUtil.extname(filename || '');
var saveAs = pathUtil.join(self.downloadDir, 'storage-api-' + uuid.v4() + extname);
var writeStream = fs.createWriteStream(saveAs);
res.pipe(writeStream);
writeStream
.on('finish', function () {
done(null, saveAs);
})
.on('error', done);
})
.on('error', done);
});
};
StorageApi.prototype.fileUri = function (file) {
var urlObj = urlUtil.parse(this.config.endpoint);
var bucket = file.metadata.bucket;
var path = file.metadata.path;
if (!bucket || !path) {
throw new Error('no bucket or path');
}
delete urlObj.host;
urlObj.hostname = bucket + '.' + urlObj.hostname;
urlObj.pathname = path;
return urlUtil.format(urlObj);
};
StorageApi.prototype.queryFile = function (options, done) {
var self = this;
self.tokenClient.getToken(function (err, token) {
if(err) {
return done(new Error('failed to get access token:' + err.message));
}
var url = self.config.endpoint + '/api/v1/file';
url += '?skip=' + options.skip + '&limit=' + options.limit +
'&where=' + JSON.stringify(options.where) + '&sort=' + (options.sort ? JSON.stringify(options.sort) : '');
var tokenClient = new SimpleTokenClient(framework.config.connections.storage);
request({
url: url,
json: true,
headers: {
Authorization: 'Bearer ' + token
}
},function (error, response, body) {
if (error) {
return done(new Error('failed to query file:' + err.message));
}
if(response.statusCode !== 200) {
return done(new Error('failed to query file, status:' + response.statusCode + ', body:' + body));
}
done(null, body);
});
});
};
module.exports = StorageApi;