|
|
@ -118,25 +118,23 @@ uploadsController.uploadCategoryPicture = async function (req, res, next) {
|
|
|
|
return next(new Error('[[error:invalid-json]]'));
|
|
|
|
return next(new Error('[[error:invalid-json]]'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (validateUpload(res, uploadedFile, allowedImageTypes)) {
|
|
|
|
await validateUpload(uploadedFile, allowedImageTypes);
|
|
|
|
const filename = `category-${params.cid}${path.extname(uploadedFile.name)}`;
|
|
|
|
const filename = `category-${params.cid}${path.extname(uploadedFile.name)}`;
|
|
|
|
await uploadImage(filename, 'category', uploadedFile, req, res, next);
|
|
|
|
await uploadImage(filename, 'category', uploadedFile, req, res, next);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
uploadsController.uploadFavicon = async function (req, res, next) {
|
|
|
|
uploadsController.uploadFavicon = async function (req, res, next) {
|
|
|
|
const uploadedFile = req.files.files[0];
|
|
|
|
const uploadedFile = req.files.files[0];
|
|
|
|
const allowedTypes = ['image/x-icon', 'image/vnd.microsoft.icon'];
|
|
|
|
const allowedTypes = ['image/x-icon', 'image/vnd.microsoft.icon'];
|
|
|
|
|
|
|
|
|
|
|
|
if (validateUpload(res, uploadedFile, allowedTypes)) {
|
|
|
|
await validateUpload(uploadedFile, allowedTypes);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const imageObj = await file.saveFileToLocal('favicon.ico', 'system', uploadedFile.path);
|
|
|
|
const imageObj = await file.saveFileToLocal('favicon.ico', 'system', uploadedFile.path);
|
|
|
|
res.json([{ name: uploadedFile.name, url: imageObj.url }]);
|
|
|
|
res.json([{ name: uploadedFile.name, url: imageObj.url }]);
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
next(err);
|
|
|
|
next(err);
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
file.delete(uploadedFile.path);
|
|
|
|
file.delete(uploadedFile.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -145,25 +143,24 @@ uploadsController.uploadTouchIcon = async function (req, res, next) {
|
|
|
|
const allowedTypes = ['image/png'];
|
|
|
|
const allowedTypes = ['image/png'];
|
|
|
|
const sizes = [36, 48, 72, 96, 144, 192, 512];
|
|
|
|
const sizes = [36, 48, 72, 96, 144, 192, 512];
|
|
|
|
|
|
|
|
|
|
|
|
if (validateUpload(res, uploadedFile, allowedTypes)) {
|
|
|
|
await validateUpload(uploadedFile, allowedTypes);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const imageObj = await file.saveFileToLocal('touchicon-orig.png', 'system', uploadedFile.path);
|
|
|
|
const imageObj = await file.saveFileToLocal('touchicon-orig.png', 'system', uploadedFile.path);
|
|
|
|
// Resize the image into squares for use as touch icons at various DPIs
|
|
|
|
// Resize the image into squares for use as touch icons at various DPIs
|
|
|
|
for (const size of sizes) {
|
|
|
|
for (const size of sizes) {
|
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
await image.resizeImage({
|
|
|
|
await image.resizeImage({
|
|
|
|
path: uploadedFile.path,
|
|
|
|
path: uploadedFile.path,
|
|
|
|
target: path.join(nconf.get('upload_path'), 'system', `touchicon-${size}.png`),
|
|
|
|
target: path.join(nconf.get('upload_path'), 'system', `touchicon-${size}.png`),
|
|
|
|
width: size,
|
|
|
|
width: size,
|
|
|
|
height: size,
|
|
|
|
height: size,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json([{ name: uploadedFile.name, url: imageObj.url }]);
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
next(err);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
file.delete(uploadedFile.path);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json([{ name: uploadedFile.name, url: imageObj.url }]);
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
next(err);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
file.delete(uploadedFile.path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -172,15 +169,14 @@ uploadsController.uploadMaskableIcon = async function (req, res, next) {
|
|
|
|
const uploadedFile = req.files.files[0];
|
|
|
|
const uploadedFile = req.files.files[0];
|
|
|
|
const allowedTypes = ['image/png'];
|
|
|
|
const allowedTypes = ['image/png'];
|
|
|
|
|
|
|
|
|
|
|
|
if (validateUpload(res, uploadedFile, allowedTypes)) {
|
|
|
|
await validateUpload(uploadedFile, allowedTypes);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const imageObj = await file.saveFileToLocal('maskableicon-orig.png', 'system', uploadedFile.path);
|
|
|
|
const imageObj = await file.saveFileToLocal('maskableicon-orig.png', 'system', uploadedFile.path);
|
|
|
|
res.json([{ name: uploadedFile.name, url: imageObj.url }]);
|
|
|
|
res.json([{ name: uploadedFile.name, url: imageObj.url }]);
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
next(err);
|
|
|
|
next(err);
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
file.delete(uploadedFile.path);
|
|
|
|
file.delete(uploadedFile.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -219,20 +215,16 @@ uploadsController.uploadOgImage = async function (req, res, next) {
|
|
|
|
async function upload(name, req, res, next) {
|
|
|
|
async function upload(name, req, res, next) {
|
|
|
|
const uploadedFile = req.files.files[0];
|
|
|
|
const uploadedFile = req.files.files[0];
|
|
|
|
|
|
|
|
|
|
|
|
if (validateUpload(res, uploadedFile, allowedImageTypes)) {
|
|
|
|
await validateUpload(uploadedFile, allowedImageTypes);
|
|
|
|
const filename = name + path.extname(uploadedFile.name);
|
|
|
|
const filename = name + path.extname(uploadedFile.name);
|
|
|
|
await uploadImage(filename, 'system', uploadedFile, req, res, next);
|
|
|
|
await uploadImage(filename, 'system', uploadedFile, req, res, next);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function validateUpload(res, uploadedFile, allowedTypes) {
|
|
|
|
async function validateUpload(uploadedFile, allowedTypes) {
|
|
|
|
if (!allowedTypes.includes(uploadedFile.type)) {
|
|
|
|
if (!allowedTypes.includes(uploadedFile.type)) {
|
|
|
|
file.delete(uploadedFile.path);
|
|
|
|
file.delete(uploadedFile.path);
|
|
|
|
res.json({ error: `[[error:invalid-image-type, ${allowedTypes.join(', ')}]]` });
|
|
|
|
throw new Error(`[[error:invalid-image-type, ${allowedTypes.join(', ')}]]`);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function uploadImage(filename, folder, uploadedFile, req, res, next) {
|
|
|
|
async function uploadImage(filename, folder, uploadedFile, req, res, next) {
|
|
|
|