This repository was archived by the owner on Feb 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
183 lines (162 loc) · 4.84 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
/*
* File: index.js
* Author: Antoine Dray
* Description: Streaming multer storage engine for OVH Object Storage.
*/
const path = require('path');
const request = require('request');
function collect(storage, req, file, cb) {
cb.call(storage, null, {
version: storage.version,
username: storage.username,
password: storage.password,
authURL: storage.authURL,
tenantId: storage.tenantId,
region: storage.region,
container: storage.container
});
}
function OVHObjectStorage(opts) {
switch (typeof opts.version) {
case 'number':
case 'undefined':
this.version = opts.version || 2;
break;
default: throw new TypeError('Expected opts.version to be number')
}
switch (typeof opts.username) {
case 'string': this.username = opts.username; break
default: throw new TypeError('Expected opts.username to be string')
}
switch (typeof opts.password) {
case 'string': this.password = opts.password; break
default: throw new TypeError('Expected opts.password to be string')
}
switch (typeof opts.authURL) {
case 'string': this.authURL = opts.authURL; break
default: throw new TypeError('Expected opts.authURL to be string')
}
switch (typeof opts.tenantId) {
case 'string': this.tenantId = opts.tenantId; break
default: throw new TypeError('Expected opts.tenantId to be string')
}
switch (typeof opts.region) {
case 'string': this.region = opts.region; break
default: throw new TypeError('Expected opts.region to be string')
}
switch (typeof opts.container) {
case 'string': this.container = opts.container; break
default: throw new TypeError('Expected opts.container to be string')
}
}
function connect(opts) {
var json = {};
if (otps.version === 3) {
json = {
auth: {
identity: {
methods: [
"password"
],
password: {
user: {
name: opts.username,
domain: {
name: "Default"
},
password: opts.password
}
},
tenantId: opts.tenantId
}
}
};
}
else {
json = {
auth: {
passwordCredentials: {
username: opts.username,
password: opts.password
},
tenantName: opts.tenantId,
tenantId: opts.tenantId
}
};
}
return new Promise((resolve, reject) => {
var tokenEndpoint = '/tokens';
if (opts.version === 3) {
var tokenEndpoint = '/auth' + tokenEndpoint;
}
request({
method: 'POST',
uri: opts.authURL + tokenEndpoint,
json: json,
headers: { 'Accept': 'application/json' }
}, function(err, res, body) {
if (err)
return reject(err);
if (body.error)
return reject(new Error(body.error.message));
if (!body.access)
return reject(new Error('Connection response incomplete'));
if (otps.version === 3) {
const token = res.headers['x-subject-token'];
const catalog = body.token.catalog.find(c => c.type === 'object-store');
const endpoint = catalog.endpoints.find(e => e.region === opts.region);
} else {
const token = body.access.token;
const catalog = body.access.serviceCatalog
.find(c => c.type === 'object-store');
const endpoint = catalog.endpoints
.find(e => e.region === opts.region);
}
resolve({ token, endpoint });
});
});
}
function formatTargetURL(publicURL, container, filename) {
const baseURL = publicURL + '/' + container + '/';
return baseURL + filename;
}
function createFilename(filename) {
const extention = path.extname(filename);
const random = (Math.random() + 1).toString(36).substring(7);
return random + '-' + Date.now() + extention;
}
OVHObjectStorage.prototype._handleFile = function(req, file, cb) {
collect(this, req, file, function (err, opts) {
if (err) return cb(err)
connect(opts).then((res) => {
const publicURL = (opts.version === 3)
? res.endpoint.url
: res.endpoint.publicURL;
const filename = createFilename(file.originalname);
const params = {
targetURL: formatTargetURL(publicURL, opts.container, filename),
headers: {
"X-Auth-Token": res.token.id,
"Accept": "application/json"
}
}
file.stream.pipe(
request({
method: 'PUT',
uri: params.targetURL,
headers: params.headers
}, function (error, response, body) {
if (error)
return cb(err);
cb(null, {
filename: filename,
url: params.targetURL
});
})
);
}).catch(err => { return cb(err) });
})
}
module.exports = function(opts) {
return new OVHObjectStorage(opts)
}