handle all post errors and delete files properly

merge-requests/208/head
fatchan 5 years ago
parent b51056002e
commit 706cbf2845
  1. 11
      controllers/forms.js
  2. 35
      models/forms/make-post.js

@ -30,6 +30,7 @@ const express = require('express')
, checkPerms = require(__dirname+'/../helpers/hasperms.js') , checkPerms = require(__dirname+'/../helpers/hasperms.js')
, paramConverter = require(__dirname+'/../helpers/paramconverter.js') , paramConverter = require(__dirname+'/../helpers/paramconverter.js')
, banCheck = require(__dirname+'/../helpers/bancheck.js') , banCheck = require(__dirname+'/../helpers/bancheck.js')
, deletePostFiles = require(__dirname+'/../helpers/files/deletepostfiles.js')
, verifyCaptcha = require(__dirname+'/../helpers/captchaverify.js') , verifyCaptcha = require(__dirname+'/../helpers/captchaverify.js')
, actionChecker = require(__dirname+'/../helpers/actionchecker.js'); , actionChecker = require(__dirname+'/../helpers/actionchecker.js');
@ -206,7 +207,15 @@ router.post('/board/:board/post', Boards.exists, banCheck, paramConverter, verif
}) })
} }
makePost(req, res, next, numFiles); try {
await makePost(req, res, next, numFiles);
} catch (err) {
if (numFiles > 0) {
const fileNames = req.files.file.map(file => file.filename);
await deletePostFiles(fileNames).catch(err => console.error);
}
return next(err);
}
}); });

@ -26,23 +26,18 @@ const uuidv4 = require('uuid/v4')
, imageIdentify = require(__dirname+'/../../helpers/files/image-identify.js') , imageIdentify = require(__dirname+'/../../helpers/files/image-identify.js')
, videoThumbnail = require(__dirname+'/../../helpers/files/video-thumbnail.js') , videoThumbnail = require(__dirname+'/../../helpers/files/video-thumbnail.js')
, videoIdentify = require(__dirname+'/../../helpers/files/video-identify.js') , videoIdentify = require(__dirname+'/../../helpers/files/video-identify.js')
, formatSize = require(__dirname+'/../../helpers/files/format-size.js') , formatSize = require(__dirname+'/../../helpers/files/format-size.js');
, deletePostFiles = require(__dirname+'/../../helpers/files/deletepostfiles.js');
module.exports = async (req, res, next, numFiles) => { module.exports = async (req, res, next, numFiles) => {
// check if this is responding to an existing thread // check if this is responding to an existing thread
let redirect = `/${req.params.board}` let redirect = `/${req.params.board}`
let salt = ''; let salt = null;
let thread; let thread = null;
const hasPerms = permsCheck(req, res); const hasPerms = permsCheck(req, res);
const forceAnon = res.locals.board.settings.forceAnon; const forceAnon = res.locals.board.settings.forceAnon;
if (req.body.thread) { if (req.body.thread) {
try {
thread = await Posts.getPost(req.params.board, req.body.thread, true); thread = await Posts.getPost(req.params.board, req.body.thread, true);
} catch (err) {
return next(err);
}
if (!thread || thread.thread != null) { if (!thread || thread.thread != null) {
return res.status(400).render('message', { return res.status(400).render('message', {
'title': 'Bad request', 'title': 'Bad request',
@ -85,9 +80,7 @@ module.exports = async (req, res, next, numFiles) => {
const file = req.files.file[i]; const file = req.files.file[i];
const uuid = uuidv4(); const uuid = uuidv4();
const filename = uuid + path.extname(file.name); const filename = uuid + path.extname(file.name);
file.filename = filename; //for error to delete failed files
// try to save, thumbnail and get metadata
try {
//upload file //upload file
await fileUpload(req, res, file, filename, 'img'); await fileUpload(req, res, file, filename, 'img');
@ -100,7 +93,7 @@ module.exports = async (req, res, next, numFiles) => {
size: file.size, size: file.size,
}; };
//handle video vs image ffmpeg vs graphicsmagick //handle video/image ffmpeg or graphicsmagick
const mainType = file.mimetype.split('/')[0]; const mainType = file.mimetype.split('/')[0];
switch (mainType) { switch (mainType) {
case 'image': case 'image':
@ -124,8 +117,6 @@ module.exports = async (req, res, next, numFiles) => {
return next(err); return next(err);
} }
//make thumbnail
//handle gifs with multiple geometry and size //handle gifs with multiple geometry and size
if (Array.isArray(processedFile.geometry)) { if (Array.isArray(processedFile.geometry)) {
processedFile.geometry = processedFile.geometry[0]; processedFile.geometry = processedFile.geometry[0];
@ -137,14 +128,6 @@ module.exports = async (req, res, next, numFiles) => {
processedFile.geometryString = processedFile.geometryString[0]; processedFile.geometryString = processedFile.geometryString[0];
} }
files.push(processedFile); files.push(processedFile);
} catch (err) {
if (files.length > 0) {
const fileNames = files.map(file => file.filenname);
await deletePostFiles(fileNames);
}
return next(err);
}
} }
} }
@ -165,7 +148,6 @@ module.exports = async (req, res, next, numFiles) => {
let subject = hasPerms || !forceAnon || !req.body.thread ? req.body.subject : null; let subject = hasPerms || !forceAnon || !req.body.thread ? req.body.subject : null;
//forceanon only allow sage email //forceanon only allow sage email
let email = hasPerms || !forceAnon || req.body.email === 'sage' ? req.body.email : null; let email = hasPerms || !forceAnon || req.body.email === 'sage' ? req.body.email : null;
let name = res.locals.board.settings.defaultName; let name = res.locals.board.settings.defaultName;
let tripcode = null; let tripcode = null;
let capcode = null; let capcode = null;
@ -231,15 +213,10 @@ module.exports = async (req, res, next, numFiles) => {
}); });
} }
let postId; const postId = await Posts.insertOne(req.params.board, data, thread);
try {
postId = await Posts.insertOne(req.params.board, data, thread);
if (!data.thread) { //if we just added a new thread, prune any old ones if (!data.thread) { //if we just added a new thread, prune any old ones
await Posts.pruneOldThreads(req.params.board, res.locals.board.settings.threadLimit); await Posts.pruneOldThreads(req.params.board, res.locals.board.settings.threadLimit);
} }
} catch (err) {
return next(err);
}
const successRedirect = `/${req.params.board}/thread/${req.body.thread || postId}#${postId}`; const successRedirect = `/${req.params.board}/thread/${req.body.thread || postId}#${postId}`;

Loading…
Cancel
Save