forked from LouiseMcMahon/nodebb-plugin-s3-uploads
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
271 lines (221 loc) · 7.73 KB
/
index.js
File metadata and controls
271 lines (221 loc) · 7.73 KB
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
'use strict';
const S3 = require('@aws-sdk/client-s3');
const mime = require('mime');
const uuid = require('uuid').v4;
const fs = require('fs');
const path = require('path');
const winston = require.main.require('winston');
const nconf = require.main.require('nconf');
const sharp = require.main.require('sharp');
const meta = require.main.require('./src/meta');
const db = require.main.require('./src/database');
const routeHelpers = require.main.require('./src/routes/helpers');
const fileModule = require.main.require('./src/file');
const Package = require('./package.json');
const plugin = module.exports;
const settings = {
accessKeyId: process.env.AWS_ACCESS_KEY_ID || false,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || false,
region: process.env.AWS_DEFAULT_REGION || 'us-east-1',
acl: process.env.S3_UPLOADS_ACL || '',
bucket: process.env.S3_UPLOADS_BUCKET || undefined,
endpoint: process.env.S3_UPLOADS_ENDPOINT || 's3.amazonaws.com',
host: process.env.S3_UPLOADS_HOST || 's3.amazonaws.com',
path: process.env.S3_UPLOADS_PATH || undefined,
};
let accessKeyIdFromDb = false;
let secretAccessKeyFromDb = false;
plugin.load = async function (params) {
await fetchSettings();
const adminRoute = '/admin/plugins/s3-uploads';
const { router, middleware } = params;
routeHelpers.setupAdminPageRoute(router, adminRoute, renderAdmin);
router.post(`/api${adminRoute}/s3settings`, middleware.applyCSRF, routeHelpers.tryRoute(s3settings));
router.post(`/api${adminRoute}/credentials`, middleware.applyCSRF, routeHelpers.tryRoute(credentials));
};
function renderAdmin(req, res) {
let forumPath = nconf.get('url');
if (forumPath.split('').reverse()[0] !== '/') {
forumPath += '/';
}
const data = {
title: 'S3 Uploads',
bucket: settings.bucket,
host: settings.host,
endpoint: settings.endpoint,
path: settings.path,
forumPath: forumPath,
region: settings.region,
acl: settings.acl,
accessKeyId: (accessKeyIdFromDb && settings.accessKeyId) || '',
secretAccessKey: (secretAccessKeyFromDb && settings.secretAccessKey) || '',
};
res.render('admin/plugins/s3-uploads', data);
}
async function fetchSettings() {
const newSettings = await db.getObjectFields(Package.name, Object.keys(settings));
accessKeyIdFromDb = !!newSettings.accessKeyId;
secretAccessKeyFromDb = !!newSettings.secretAccessKey;
settings.accessKeyId = newSettings.accessKeyId || process.env.AWS_ACCESS_KEY_ID || false;
settings.secretAccessKey = newSettings.secretAccessKey || process.env.AWS_SECRET_ACCESS_KEY || false;
settings.bucket = newSettings.bucket || process.env.S3_UPLOADS_BUCKET || '';
settings.host = newSettings.host || process.env.S3_UPLOADS_HOST || '';
settings.endpoint = newSettings.endpoint || process.env.S3_UPLOADS_ENDPOINT || '';
settings.path = newSettings.path || process.env.S3_UPLOADS_PATH || '';
settings.region = newSettings.region || process.env.AWS_DEFAULT_REGION || '';
settings.acl = newSettings.acl || process.env.S3_UPLOADS_ACL || '';
}
function constructS3() {
return new S3.S3Client({
region: settings.region,
endpoint: settings.endpoint,
credentials: {
accessKeyId: settings.accessKeyId,
secretAccessKey: settings.secretAccessKey,
},
});
}
plugin.activate = async function (data) {
if (data.id === 'nodebb-plugin-s3-uploads') {
await fetchSettings();
}
};
async function s3settings(req, res) {
const data = req.body;
const newSettings = {
bucket: (data.bucket || '').trim(),
endpoint: (data.endpoint || '').trim(),
host: (data.host || '').trim(),
path: (data.path || '').trim(),
region: (data.region || '').trim(),
acl: (data.acl || '').trim(),
};
await saveSettings(newSettings, res);
res.json('Saved!');
}
async function credentials(req, res) {
const data = req.body;
const newSettings = {
accessKeyId: (data.accessKeyId || '').trim(),
secretAccessKey: (data.secretAccessKey || '').trim(),
};
await saveSettings(newSettings, res);
res.json('Saved!');
}
async function saveSettings(settings) {
await db.setObject(Package.name, settings);
await fetchSettings();
}
function isExtensionAllowed(filename, allowed) {
const extension = path.extname(filename).toLowerCase();
return !(allowed.length > 0 && (!extension || extension === '.' || !allowed.includes(extension)));
}
plugin.uploadImage = async function (data) {
const { image } = data;
if (!image) {
winston.error('invalid image');
throw new Error('invalid image');
}
// check filesize vs. settings
if (image.size > parseInt(meta.config.maximumFileSize, 10) * 1024) {
winston.error(`error:file-too-big, ${meta.config.maximumFileSize}`);
throw new Error(`[[error:file-too-big, ${meta.config.maximumFileSize}]]`);
}
const type = image.url ? 'url' : 'file';
// uploading from a url
if (type === 'url') {
return await uploadFromUrl(image);
}
// regular file upload
if (!image.path) {
throw new Error('invalid image path');
}
const allowed = fileModule.allowedExtensions();
if (!isExtensionAllowed(image.name, allowed)) {
throw new Error(`[[error:invalid-file-type, ${allowed.join(', ')}]]`);
}
const buffer = await fs.promises.readFile(image.path);
return await uploadToS3(image.name, buffer);
};
async function uploadFromUrl(image) {
const allowed = fileModule.allowedExtensions();
if (!isExtensionAllowed(image.url, allowed)) {
throw new Error(`[[error:invalid-file-type, ${allowed.join(', ')}]]`);
}
const response = await fetch(image.url);
if (!response.ok) throw new Error(`Failed to fetch image from URL: ${response.statusText}`);
const arrayBuffer = await response.arrayBuffer();
const inputBuffer = Buffer.from(arrayBuffer);
const filename = image.url.split('/').pop();
const imageDimension = parseInt(meta.config.profileImageDimension, 10) || 128;
const resizedBuffer = await sharp(inputBuffer)
.resize(imageDimension, imageDimension, { fit: 'cover' });
return await uploadToS3(filename, resizedBuffer);
}
plugin.uploadFile = async function (data) {
const { file } = data;
if (!file) {
throw new Error('invalid file');
}
if (!file.path) {
throw new Error('invalid file path');
}
// check filesize vs. settings
if (file.size > parseInt(meta.config.maximumFileSize, 10) * 1024) {
winston.error(`error:file-too-big, ${meta.config.maximumFileSize}`);
throw new Error(`[[error:file-too-big, ${meta.config.maximumFileSize}]]`);
}
const allowed = fileModule.allowedExtensions();
if (!isExtensionAllowed(file.name, allowed)) {
throw new Error(`[[error:invalid-file-type, ${allowed.join(', ')}]]`);
}
const buffer = await fs.promises.readFile(file.path);
return await uploadToS3(file.name, buffer);
};
async function uploadToS3(filename, buffer) {
let s3Path;
if (settings.path && settings.path.length > 0) {
s3Path = settings.path;
if (!s3Path.match(/\/$/)) {
// Add trailing slash
s3Path += '/';
}
} else {
s3Path = '/';
}
const s3KeyPath = s3Path.replace(/^\//, ''); // S3 Key Path should not start with slash.
const params = {
Bucket: settings.bucket,
Key: s3KeyPath + uuid() + path.extname(filename),
Body: buffer,
ContentLength: buffer.length,
ContentType: mime.getType(filename),
};
if (settings.ACL) {
params.ACL = settings.ACL;
}
const s3Client = constructS3();
await s3Client.send(new S3.PutObjectCommand(params));
// amazon has https enabled, we use it by default
let host = `https://${params.Bucket}.s3.amazonaws.com`;
if (settings.host && settings.host.length > 0) {
host = settings.host;
// host must start with http or https
if (!host.startsWith('http')) {
host = `http://${host}`;
}
}
return {
name: filename,
url: `${host}/${params.Key}`,
};
}
plugin.admin = {};
plugin.admin.menu = function (custom_header) {
custom_header.plugins.push({
route: '/plugins/s3-uploads',
icon: 'fa-envelope-o',
name: 'S3 Uploads',
});
return custom_header;
};