-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
uploads.js
272 lines (238 loc) · 8.35 KB
/
uploads.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
'use strict';
const path = require('path');
const nconf = require('nconf');
const fs = require('fs');
const meta = require('../../meta');
const posts = require('../../posts');
const file = require('../../file');
const image = require('../../image');
const plugins = require('../../plugins');
const pagination = require('../../pagination');
const allowedImageTypes = ['image/png', 'image/jpeg', 'image/pjpeg', 'image/jpg', 'image/gif', 'image/svg+xml'];
const uploadsController = module.exports;
uploadsController.get = async function (req, res, next) {
const currentFolder = path.join(nconf.get('upload_path'), req.query.dir || '');
if (!currentFolder.startsWith(nconf.get('upload_path'))) {
return next(new Error('[[error:invalid-path]]'));
}
const itemsPerPage = 20;
const page = parseInt(req.query.page, 10) || 1;
try {
let files = await fs.promises.readdir(currentFolder);
files = files.filter(filename => filename !== '.gitignore');
const itemCount = files.length;
var start = Math.max(0, (page - 1) * itemsPerPage);
var stop = start + itemsPerPage;
files = files.slice(start, stop);
files = await filesToData(currentFolder, files);
// Float directories to the top
files.sort(function (a, b) {
if (a.isDirectory && !b.isDirectory) {
return -1;
} else if (!a.isDirectory && b.isDirectory) {
return 1;
} else if (!a.isDirectory && !b.isDirectory) {
return a.mtime < b.mtime ? -1 : 1;
}
return 0;
});
// Add post usage info if in /files
if (req.query.dir === '/files') {
const usage = await posts.uploads.getUsage(files);
files.forEach(function (file, idx) {
file.inPids = usage[idx].map(pid => parseInt(pid, 10));
});
}
res.render('admin/manage/uploads', {
currentFolder: currentFolder.replace(nconf.get('upload_path'), ''),
showPids: files.length && files[0].hasOwnProperty('inPids'),
files: files,
breadcrumbs: buildBreadcrumbs(currentFolder),
pagination: pagination.create(page, Math.ceil(itemCount / itemsPerPage), req.query),
});
} catch (err) {
next(err);
}
};
function buildBreadcrumbs(currentFolder) {
var crumbs = [];
var parts = currentFolder.replace(nconf.get('upload_path'), '').split(path.sep);
var currentPath = '';
parts.forEach(function (part) {
var dir = path.join(currentPath, part);
crumbs.push({
text: part || 'Uploads',
url: part ?
(nconf.get('relative_path') + '/admin/manage/uploads?dir=' + dir) :
nconf.get('relative_path') + '/admin/manage/uploads',
});
currentPath = dir;
});
return crumbs;
}
async function filesToData(currentDir, files) {
return await Promise.all(files.map(file => getFileData(currentDir, file)));
}
async function getFileData(currentDir, file) {
const stat = await fs.promises.stat(path.join(currentDir, file));
let filesInDir = [];
if (stat.isDirectory()) {
filesInDir = await fs.promises.readdir(path.join(currentDir, file));
}
const url = nconf.get('upload_url') + currentDir.replace(nconf.get('upload_path'), '') + '/' + file;
return {
name: file,
path: path.join(currentDir, file).replace(nconf.get('upload_path'), ''),
url: url,
fileCount: Math.max(0, filesInDir.length - 1), // ignore .gitignore
size: stat.size,
sizeHumanReadable: (stat.size / 1024).toFixed(1) + 'KiB',
isDirectory: stat.isDirectory(),
isFile: stat.isFile(),
mtime: stat.mtimeMs,
};
}
uploadsController.uploadCategoryPicture = async function (req, res, next) {
const uploadedFile = req.files.files[0];
let params = null;
try {
params = JSON.parse(req.body.params);
} catch (e) {
file.delete(uploadedFile.path);
return next(new Error('[[error:invalid-json]]'));
}
if (validateUpload(res, uploadedFile, allowedImageTypes)) {
const filename = 'category-' + params.cid + path.extname(uploadedFile.name);
await uploadImage(filename, 'category', uploadedFile, req, res, next);
}
};
uploadsController.uploadFavicon = async function (req, res, next) {
const uploadedFile = req.files.files[0];
const allowedTypes = ['image/x-icon', 'image/vnd.microsoft.icon'];
if (validateUpload(res, uploadedFile, allowedTypes)) {
try {
const imageObj = await file.saveFileToLocal('favicon.ico', 'system', uploadedFile.path);
res.json([{ name: uploadedFile.name, url: imageObj.url }]);
} catch (err) {
next(err);
} finally {
file.delete(uploadedFile.path);
}
}
};
uploadsController.uploadTouchIcon = async function (req, res, next) {
const uploadedFile = req.files.files[0];
const allowedTypes = ['image/png'];
const sizes = [36, 48, 72, 96, 144, 192, 512];
if (validateUpload(res, uploadedFile, allowedTypes)) {
try {
const imageObj = await file.saveFileToLocal('touchicon-orig.png', 'system', uploadedFile.path);
// Resize the image into squares for use as touch icons at various DPIs
for (const size of sizes) {
/* eslint-disable no-await-in-loop */
await image.resizeImage({
path: uploadedFile.path,
target: path.join(nconf.get('upload_path'), 'system', 'touchicon-' + size + '.png'),
width: size,
height: size,
});
}
res.json([{ name: uploadedFile.name, url: imageObj.url }]);
} catch (err) {
next(err);
} finally {
file.delete(uploadedFile.path);
}
}
};
uploadsController.uploadMaskableIcon = async function (req, res, next) {
const uploadedFile = req.files.files[0];
const allowedTypes = ['image/png'];
if (validateUpload(res, uploadedFile, allowedTypes)) {
try {
const imageObj = await file.saveFileToLocal('maskableicon-orig.png', 'system', uploadedFile.path);
res.json([{ name: uploadedFile.name, url: imageObj.url }]);
} catch (err) {
next(err);
} finally {
file.delete(uploadedFile.path);
}
}
};
uploadsController.uploadLogo = async function (req, res, next) {
await upload('site-logo', req, res, next);
};
uploadsController.uploadFile = async function (req, res, next) {
const uploadedFile = req.files.files[0];
let params;
try {
params = JSON.parse(req.body.params);
} catch (e) {
file.delete(uploadedFile.path);
return next(new Error('[[error:invalid-json]]'));
}
try {
const data = await file.saveFileToLocal(uploadedFile.name, params.folder, uploadedFile.path);
res.json([{ url: data.url }]);
} catch (err) {
next(err);
} finally {
file.delete(uploadedFile.path);
}
};
uploadsController.uploadDefaultAvatar = async function (req, res, next) {
await upload('avatar-default', req, res, next);
};
uploadsController.uploadOgImage = async function (req, res, next) {
await upload('og:image', req, res, next);
};
async function upload(name, req, res, next) {
const uploadedFile = req.files.files[0];
if (validateUpload(res, uploadedFile, allowedImageTypes)) {
const filename = name + path.extname(uploadedFile.name);
await uploadImage(filename, 'system', uploadedFile, req, res, next);
}
}
function validateUpload(res, uploadedFile, allowedTypes) {
if (!allowedTypes.includes(uploadedFile.type)) {
file.delete(uploadedFile.path);
res.json({ error: '[[error:invalid-image-type, ' + allowedTypes.join(', ') + ']]' });
return false;
}
return true;
}
async function uploadImage(filename, folder, uploadedFile, req, res, next) {
let imageData;
try {
if (plugins.hasListeners('filter:uploadImage')) {
imageData = await plugins.fireHook('filter:uploadImage', { image: uploadedFile, uid: req.uid, folder: folder });
} else {
imageData = await file.saveFileToLocal(filename, folder, uploadedFile.path);
}
if (path.basename(filename, path.extname(filename)) === 'site-logo' && folder === 'system') {
const uploadPath = path.join(nconf.get('upload_path'), folder, 'site-logo-x50.png');
await image.resizeImage({
path: uploadedFile.path,
target: uploadPath,
height: 50,
});
await meta.configs.set('brand:emailLogo', path.join(nconf.get('upload_url'), 'system/site-logo-x50.png'));
const size = await image.size(uploadedFile.path);
await meta.configs.setMultiple({
'brand:logo:width': size.width,
'brand:logo:height': size.height,
});
} else if (path.basename(filename, path.extname(filename)) === 'og:image' && folder === 'system') {
const size = await image.size(uploadedFile.path);
await meta.configs.setMultiple({
'og:image:width': size.width,
'og:image:height': size.height,
});
}
res.json([{ name: uploadedFile.name, url: imageData.url.startsWith('http') ? imageData.url : nconf.get('relative_path') + imageData.url }]);
} catch (err) {
next(err);
} finally {
file.delete(uploadedFile.path);
}
}