forked from WalktheChat/skipper-aliyunoss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
191 lines (170 loc) · 5.14 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* Module dependencies
*/
var path = require('path');
var Writable = require('stream').Writable;
var Transform = require('stream').Transform;
var concat = require('concat-stream');
var _ = require('lodash');
_.defaultsDeep = require('merge-defaults');
var oss = require('ali-oss');
var mime = require('mime');
var co = require('co');
/**
* skipper-aliyunoss
*
* @param {Object} globalOpts
* @return {Object}
*/
module.exports = function SkipperS3(globalOpts) {
globalOpts = globalOpts || {};
var adapter = {
read: function (fd, cb) {
var prefix = fd;
var store = oss({
accessKeyId: globalOpts.key,
accessKeySecret: globalOpts.secret,
bucket: globalOpts.bucket,
region: globalOpts.region || undefined,
secure: true
});
// Build a noop transform stream that will pump the S3 output through
var __transform__ = new Transform();
__transform__._transform = function (chunk, encoding, callback) {
return callback(null, chunk);
};
co(store.getStream(prefix))
.then(function (getStreamResult) {
// check whether we got an actual file stream:
if (getStreamResult.res.status < 300) {
getStreamResult.stream.once('error', function (err) {
__transform__.emit('error', err);
});
getStreamResult.stream.pipe(__transform__);
} else {
var err = new Error();
err.status = getStreamResult.res.status;
err.headers = getStreamResult.res.headers;
err.message = 'Non-200 status code returned from Aliyun for requested file.';
__transform__.emit('error', err);
}
})
.catch(function (err) {
__transform__.emit('error', err);
});
if (cb) {
var firedCb;
__transform__.once('error', function (err) {
if (firedCb) return;
firedCb = true;
cb(err);
});
__transform__.pipe(concat(function (data) {
if (firedCb) return;
firedCb = true;
cb(null, data);
}));
}
return __transform__;
},
rm: function (fd, cb) {
var store = oss({
accessKeyId: globalOpts.key,
accessKeySecret: globalOpts.secret,
bucket: globalOpts.bucket,
region: globalOpts.region || undefined,
secure: true
});
co(store.delete(fd))
.then(function (result) {
if (result.status == 200) {
cb();
} else {
cb({
statusCode: result.status,
message: result
});
}
})
.catch(function (err) {
cb(err);
});
},
ls: function (dirname, cb) {
var store = oss({
accessKeyId: globalOpts.key,
accessKeySecret: globalOpts.secret,
bucket: globalOpts.bucket,
region: globalOpts.region || undefined,
secure: true
});
// Strip leading slash from dirname to form prefix
var prefix = dirname.replace(/^\//, '');
co(store.list({prefix: prefix}))
.then(function (result) {
if (result && result.objects) {
cb(null, result.objects.map(function (object) {
return object.name;
}));
} else {
cb(null, []);
}
})
.catch(function (err) {
cb(err, null);
});
},
receive: AliyunReceiver
};
return adapter;
/**
* A simple receiver for Skipper that writes Upstreams to
* S3 to the configured bucket at the configured path.
*
* Includes a garbage-collection mechanism for failed
* uploads.
*
* @param {Object} options
* @return {Stream.Writable}
*/
function AliyunReceiver(options) {
options = options || {};
options = _.defaults(options, globalOpts);
var headers = options.headers || {};
var receiver__ = Writable({
objectMode: true
});
receiver__._write = function onFile(__newFile, encoding, next) {
var store = oss({
accessKeyId: options.key,
accessKeySecret: options.secret,
bucket: options.bucket,
region: globalOpts.region || undefined,
secure: true
});
var mimeType = headers['content-type'] || mime.lookup(__newFile.fd);
co(store.putStream(__newFile.fd, __newFile, {mime: mimeType}))
.then(function (result) {
__newFile.filename = __newFile.fd;
__newFile.fd = result.url;
return co(store.head(__newFile.filename));
})
.then(function (result) {
__newFile.extra = result.res.headers;
__newFile.byteCount = result.res.headers["content-length"];
receiver__.emit('writefile', __newFile);
next();
})
.catch(function (err) {
return next({
incoming: __newFile,
code: 'E_WRITE',
stack: typeof err === 'object' ? err.stack : new Error(err),
name: typeof err === 'object' ? err.name : err,
message: typeof err === 'object' ? err.message : err
});
});
};
return receiver__;
}
};