forked from hmqgg/nodebb-plugin-azurestorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.js
336 lines (288 loc) · 8.16 KB
/
library.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
"use strict";
var plugin = {},
azure = require("azure-storage"),
mime = require("mime"),
uniqid = require("uniqid"),
fs = require("fs"),
request = require("request"),
path = require("path"),
im = require('imagemagick-stream'),
async = require.main.require('async'),
winston = module.parent.require("winston"),
nconf = module.parent.require('nconf'),
meta = module.parent.require("./meta"),
db = module.parent.require("./database");
var settings = {
"accessKeyId": false,
"secretAccessKey": false,
"container": process.env.AZURE_STORAGE_CONTAINER || undefined,
"host": process.env.AZURE_STORAGE_HOSTNAME || "blob.core.windows.net",
"path": process.env.AZURE_STORAGE_PATH || undefined
};
var accessKeyIdFromDb = false;
var secretAccessKeyFromDb = false;
var blobSvc = null;
plugin.activate = function (data) {
if (data.id === 'nodebb-plugin-azurestorage') {
fetchSettings();
}
};
plugin.deactivate = function (data) {
if (data.id === 'nodebb-plugin-azurestorage') {
blobSvc = null;
}
};
plugin.load = function (params, callback) {
fetchSettings(function (err) {
if (err) {
return winston.error(err.message);
}
var adminRoute = "/admin/plugins/azurestorage";
params.router.get(adminRoute, params.middleware.applyCSRF, params.middleware.admin.buildHeader, renderAdmin);
params.router.get("/api" + adminRoute, params.middleware.applyCSRF, renderAdmin);
params.router.post("/api" + adminRoute + "/assettings", assettings);
params.router.post("/api" + adminRoute + "/credentials", credentials);
callback();
});
};
function renderAdmin(req, res) {
// Regenerate csrf token
var token = req.csrfToken();
var forumPath = nconf.get('url');
if(forumPath.split("").reverse()[0] != "/" ){
forumPath = forumPath + "/";
}
var data = {
container: settings.container,
host: settings.host,
path: settings.path,
forumPath: forumPath,
accessKeyId: (accessKeyIdFromDb && settings.accessKeyId) || "",
secretAccessKey: (accessKeyIdFromDb && settings.secretAccessKey) || "",
csrf: token
};
res.render("admin/plugins/azurestorage", data);
}
function fetchSettings(callback) {
db.getObjectFields("nodebb-plugin-azurestorage", Object.keys(settings), function (err, newSettings) {
if (err) {
winston.error(err.message);
if (typeof callback === "function") {
callback(err);
}
return;
}
accessKeyIdFromDb = false;
secretAccessKeyFromDb = false;
if (newSettings.accessKeyId) {
settings.accessKeyId = newSettings.accessKeyId;
accessKeyIdFromDb = true;
} else {
settings.accessKeyId = false;
}
if (newSettings.secretAccessKey) {
settings.secretAccessKey = newSettings.secretAccessKey;
secretAccessKeyFromDb = false;
} else {
settings.secretAccessKey = false;
}
if (!newSettings.container) {
settings.container = process.env.AZURE_STORAGE_CONTAINER || "";
} else {
settings.container = newSettings.container;
}
if (!newSettings.host) {
settings.host = process.env.AZURE_STORAGE_HOSTNAME || "";
} else {
settings.host = newSettings.host;
}
if (!newSettings.path) {
settings.path = process.env.AZURE_STORAGE_PATH || "";
} else {
settings.path = newSettings.path;
}
if (settings.accessKeyId && settings.secretAccessKey) {
blobSvc = azure.createBlobService(settings.accessKeyId, settings.secretAccessKey);
}
if (typeof callback === "function") {
callback();
}
});
}
function Blob() {
if (!blobSvc) {
blobSvc = azure.createBlobService(); // using env AZURE_STORAGE_ACCOUNT / AZURE_STORAGE_ACCESS_KEY
}
return blobSvc;
}
function makeError(err) {
if (err instanceof Error) {
err.message = "nodebb-plugin-azurestorage:: " + err.message;
} else {
err = new Error("nodebb-plugin-azurestorage:: " + err);
}
winston.error(err.message);
return err;
}
function assettings(req, res, next) {
var data = req.body;
var newSettings = {
container: data.container || "",
host: data.host || "",
path: data.path || ""
};
saveSettings(newSettings, res, next);
}
function credentials(req, res, next) {
var data = req.body;
var newSettings = {
accessKeyId: data.accessKeyId || "",
secretAccessKey: data.secretAccessKey || ""
};
saveSettings(newSettings, res, next);
}
function saveSettings(settings, res, next) {
db.setObject("nodebb-plugin-azurestorage", settings, function (err) {
if (err) {
return next(makeError(err));
}
fetchSettings();
res.json("Saved!");
});
}
plugin.uploadImage = function (data, callback) {
async.waterfall([
function(next) {
var image = data.image;
if (!image) {
return next(new Error("[[error:invalid-image]]"));
}
if (image.size > parseInt(meta.config.maximumFileSize, 10) * 1024) {
winston.error("error:file-too-big, " + meta.config.maximumFileSize );
return next(new Error("[[error:file-too-big, " + meta.config.maximumFileSize + "]]"));
}
next(null, image);
},
function(_image, next) {
var image = _image;
var type = image.url ? "url" : "file";
var allowedMimeTypes = ['image/png', 'image/jpeg', 'image/gif'];
if (type === "file") {
if (!image.path) {
return next(new Error("invalid image path"));
}
if (allowedMimeTypes.indexOf(mime.getType(image.path)) === -1) {
return next(new Error("invalid mime type"));
}
var rs = fs.createReadStream(image.path);
next(null, rs, image.name);
}
else {
if (allowedMimeTypes.indexOf(mime.getType(image.url)) === -1) {
return next(new Error("invalid mime type"));
}
var filename = image.url.split("/").pop();
var imageDimension = parseInt(meta.config.profileImageDimension, 10) || 128;
var resize = im().resize(imageDimension + "^", imageDimension + "^");
var imstream = request(image.url).pipe(resize);
next(null, imstream, image.name);
}
},
function(_rs, _name, next) {
uploadToAzureStorage(_name, _rs, next);
}
], callback);
};
plugin.uploadFile = function (data, callback) {
async.waterfall([
function(next) {
var file = data.file;
if (!file) {
return next(new Error("[[error:invalid-file]]"));
}
if (!file.path) {
return next(new Error("[[error:invalid-file-path]]"));
}
next(null, file);
},
function(_file, next) {
var file = _file;
if (file.size > parseInt(meta.config.maximumFileSize, 10) * 1024) {
winston.error("error:file-too-big, " + meta.config.maximumFileSize );
return next(new Error("[[error:file-too-big, " + meta.config.maximumFileSize + "]]"));
}
next(null, file);
},
function(_file, next) {
var rs = fs.createReadStream(_file.path);
next(null, rs, _file.name);
},
function(_rs, _name, next) {
uploadToAzureStorage(_name, _rs, next);
}
], callback);
};
function uploadToAzureStorage(filename, rs, callback) {
async.waterfall([
function(next) {
var azPath;
if (settings.path && 0 < settings.path.length) {
azPath = settings.path;
if (!azPath.match(/\/$/)) {
// Add trailing slash
azPath = azPath + "/";
}
}
else {
azPath = "/";
}
var azKeyPath = azPath.replace(/^\//, "");
var ename = path.extname(filename);
var oname = path.basename(filename, ename);
var key = azKeyPath + uniqid.time(oname + '-') + ename ;
next(null, key);
},
function(key, next)
{
var host = "https://" + settings.accessKeyId +".blob.core.windows.net/" + settings.container;
if (settings.host && 0 < settings.host.length) {
host = settings.host;
if (!host.startsWith("http")) {
host = "http://" + host;
}
}
next(null, key, host);
},
function(key, host, next)
{
var options = {
contentSettings: {
contentType: mime.getType(filename)
}
};
rs.pipe(Blob().createWriteStreamToBlockBlob(settings.container, key, function(err, result, resp) {
if (err) {
return next(err);
}
})).on('finish', function(err) {
if (err) {
return (next(makeError(err)));
}
var response = {
name: filename,
url: host + "/" + key
};
next(null, response);
});
}
], callback);
}
plugin.menu = function (custom_header, callback) {
custom_header.plugins.push({
"route": "/plugins/azurestorage",
"icon": "fa-envelope-o",
"name": "Azure Storage"
});
callback(null, custom_header);
};
module.exports = plugin;