change dec/inc of files when making post

merge-requests/208/head
fatchan 5 years ago
parent eaf39d3032
commit 1d497cb2bb
  1. 4
      controllers/forms/makepost.js
  2. 19
      models/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);
}

@ -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

Loading…
Cancel
Save