From 1d497cb2bbb1d209daeef52cb91f1ecf8d12a2b2 Mon Sep 17 00:00:00 2001 From: fatchan Date: Sun, 1 Sep 2019 11:26:07 +0000 Subject: [PATCH] change dec/inc of files when making post --- controllers/forms/makepost.js | 4 +++- models/forms/makepost.js | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/controllers/forms/makepost.js b/controllers/forms/makepost.js index b9164668..97b56b42 100644 --- a/controllers/forms/makepost.js +++ b/controllers/forms/makepost.js @@ -1,7 +1,8 @@ 'use strict'; const makePost = require(__dirname+'/../../models/forms/makepost.js') - , deleteTempFiles = require(__dirname+'/../../helpers/files/deletetempfiles.js'); + , deleteTempFiles = require(__dirname+'/../../helpers/files/deletetempfiles.js') + , { Files } = require(__dirname+'/../../db/'); module.exports = async (req, res, next) => { @@ -79,6 +80,7 @@ module.exports = async (req, res, next) => { await makePost(req, res, next); } catch (err) { await deleteTempFiles(req).catch(e => console.error); + await Files.decrement(req.files.file.filter(x => x.filename != null).map(x => x.filename)).catch(e => console.error); return next(err); } diff --git a/models/forms/makepost.js b/models/forms/makepost.js index ef79518f..1b81d7f3 100644 --- a/models/forms/makepost.js +++ b/models/forms/makepost.js @@ -147,21 +147,23 @@ module.exports = async (req, res, next) => { for (let i = 0; i < res.locals.numFiles; i++) { const file = req.files.file[i]; let extension = path.extname(file.name) || file.name.substring(file.name.indexOf('.')); - const filename = file.sha256 + extension; + file.filename = file.sha256 + extension; //get metadata let processedFile = { hash: file.sha256, - filename: filename, + filename: file.filename, originalFilename: file.name, mimetype: file.mimetype, maintype: file.mimetype.split('/')[0], size: file.size, }; + await Files.increment(processedFile); + //check if already exists - const existsFull = await pathExists(`${uploadDirectory}img/${filename}`); - const existsThumb = await pathExists(`${uploadDirectory}img/thumb-${filename.split('.')[0]}.jpg`); + const existsFull = await pathExists(`${uploadDirectory}img/${processedFile.filename}`); + const existsThumb = await pathExists(`${uploadDirectory}img/thumb-${processedFile.hash}.jpg`); //handle video/image ffmpeg or graphicsmagick switch (processedFile.maintype) { @@ -174,10 +176,10 @@ module.exports = async (req, res, next) => { && processedFile.geometry.height <= 128 && processedFile.geometry.width <= 128); if (!existsFull) { - await imageUpload(file, filename, 'img'); + await imageUpload(file, processedFile.filename, 'img'); } if (!existsThumb && processedFile.hasThumb) { - await imageThumbnail(filename); + await imageThumbnail(processedFile.filename); } processedFile = fixGifs(processedFile); break; @@ -200,10 +202,10 @@ module.exports = async (req, res, next) => { processedFile.geometryString = `${processedFile.geometry.width}x${processedFile.geometry.height}` // 123 x 123 string processedFile.hasThumb = true; if (!existsFull) { - await videoUpload(file, filename, 'img'); + await videoUpload(file, processedFile.filename, 'img'); } if (!existsThumb) { - await videoThumbnail(filename, processedFile.geometry); + await videoThumbnail(processedFile.filename, processedFile.geometry); } break; default: @@ -225,7 +227,6 @@ module.exports = async (req, res, next) => { await remove(file.tempFilePath); files.push(processedFile); - await Files.increment(processedFile); } } // because express middleware is autistic i need to do this