mirror of https://gitgud.io/fatchan/jschan.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
4.2 KiB
130 lines
4.2 KiB
'use strict';
|
|
|
|
const path = require('path')
|
|
, { remove, pathExists } = require('fs-extra')
|
|
, { globalLimits, checkRealMimeTypes } = require(__dirname+'/../../configs/main.js')
|
|
, uploadDirectory = require(__dirname+'/../../helpers/files/uploadDirectory.js')
|
|
, moveUpload = require(__dirname+'/../../helpers/files/moveupload.js')
|
|
, mimeTypes = require(__dirname+'/../../helpers/files/mimetypes.js')
|
|
, imageIdentify = require(__dirname+'/../../helpers/files/imageidentify.js')
|
|
, deleteTempFiles = require(__dirname+'/../../helpers/files/deletetempfiles.js')
|
|
, dynamicResponse = require(__dirname+'/../../helpers/dynamic.js')
|
|
, { Boards } = require(__dirname+'/../../db/')
|
|
, buildQueue = require(__dirname+'/../../queue.js');
|
|
|
|
module.exports = async (req, res, next) => {
|
|
|
|
const redirect = `/${req.params.board}/manage/banners.html`
|
|
|
|
// check all mime types before we try saving anything
|
|
for (let i = 0; i < res.locals.numFiles; i++) {
|
|
if (!mimeTypes.allowed(req.files.file[i].mimetype, {
|
|
//banners can be static image or animated (gif, apng, etc)
|
|
image: true,
|
|
animatedImage: true,
|
|
video: false,
|
|
audio: false,
|
|
other: false
|
|
})) {
|
|
await deleteTempFiles(req).catch(e => console.error);
|
|
return dynamicResponse(req, res, 400, 'message', {
|
|
'title': 'Bad request',
|
|
'message': `Invalid file type for ${req.files.file[i].name}. Mimetype ${req.files.file[i].mimetype} not allowed.`,
|
|
'redirect': redirect
|
|
});
|
|
}
|
|
}
|
|
|
|
// check for any mismatching supposed mimetypes from the actual file mimetype
|
|
if (checkRealMimeTypes) {
|
|
for (let i = 0; i < res.locals.numFiles; i++) {
|
|
if (!(await mimeTypes.realMimeCheck(req.files.file[i]))) {
|
|
deleteTempFiles(req).catch(e => console.error);
|
|
return dynamicResponse(req, res, 400, 'message', {
|
|
'title': 'Bad request',
|
|
'message': `Mime type mismatch for file "${req.files.file[i].name}"`,
|
|
'redirect': redirect
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
const filenames = [];
|
|
for (let i = 0; i < res.locals.numFiles; i++) {
|
|
const file = req.files.file[i];
|
|
const filename = file.sha256 + path.extname(file.name);
|
|
file.filename = filename;
|
|
|
|
//check if already exists
|
|
const exists = await pathExists(`${uploadDirectory}/banner/${req.params.board}/${filename}`);
|
|
|
|
if (exists) {
|
|
await remove(file.tempFilePath);
|
|
continue;
|
|
}
|
|
|
|
//add to list after checking it doesnt already exist
|
|
filenames.push(filename);
|
|
|
|
//get metadata from tempfile
|
|
const imageData = await imageIdentify(req.files.file[i].tempFilePath, null, true);
|
|
let geometry = imageData.size;
|
|
if (Array.isArray(geometry)) {
|
|
geometry = geometry[0];
|
|
}
|
|
|
|
//make sure its 300x100 banner
|
|
if (geometry.width > globalLimits.bannerFiles.width
|
|
|| geometry.height > globalLimits.bannerFiles.height
|
|
|| (globalLimits.bannerFiles.forceAspectRatio === true
|
|
&& (geometry.width/geometry.height !== 3))) {
|
|
await deleteTempFiles(req).catch(e => console.error);
|
|
return dynamicResponse(req, res, 400, 'message', {
|
|
'title': 'Bad request',
|
|
'message': `Invalid file ${file.name}. Max banner dimensions are ${globalLimits.bannerFiles.width}x${globalLimits.bannerFiles.height}${globalLimits.bannerFiles.forceAspectRatio === true ? ' and must be a 3:1 aspect ratio' : '' }.`,
|
|
'redirect': redirect
|
|
});
|
|
}
|
|
|
|
//then upload it
|
|
await moveUpload(file, filename, `banner/${req.params.board}`);
|
|
|
|
//and delete the temp file
|
|
await remove(file.tempFilePath);
|
|
|
|
}
|
|
|
|
deleteTempFiles(req).catch(e => console.error);
|
|
|
|
// no new banners
|
|
if (filenames.length === 0) {
|
|
return dynamicResponse(req, res, 400, 'message', {
|
|
'title': 'Bad request',
|
|
'message': `Banner${res.locals.numFiles > 1 ? 's' : ''} already exist${res.locals.numFiles > 1 ? '' : 's'}`,
|
|
'redirect': redirect
|
|
});
|
|
}
|
|
|
|
// add banners to the db
|
|
await Boards.addBanners(req.params.board, filenames);
|
|
|
|
// add banners to board in memory
|
|
res.locals.board.banners = res.locals.board.banners.concat(filenames);
|
|
|
|
if (filenames.length > 0) {
|
|
//add public banners page to build queue
|
|
buildQueue.push({
|
|
'task': 'buildBanners',
|
|
'options': {
|
|
'board': res.locals.board,
|
|
}
|
|
});
|
|
}
|
|
|
|
return dynamicResponse(req, res, 200, 'message', {
|
|
'title': 'Success',
|
|
'message': `Uploaded ${filenames.length} new banners.`,
|
|
'redirect': redirect
|
|
});
|
|
|
|
}
|
|
|