deal with hash instead of spliting filename in various locations, and make use of post file delete helper

merge-requests/208/head
fatchan 5 years ago
parent 13c226f066
commit ddfe6241b5
  1. 13
      helpers/files/deletepostfiles.js
  2. 6
      helpers/files/imagethumbnail.js
  3. 6
      helpers/files/videothumbnail.js
  4. 36
      models/forms/deletepostsfiles.js
  5. 7
      models/forms/makepost.js
  6. 2
      views/mixins/catalogtile.pug
  7. 2
      views/mixins/post.pug
  8. 6
      views/pages/thread.pug

@ -3,15 +3,14 @@
const { remove } = require('fs-extra')
, uploadDirectory = require(__dirname+'/uploadDirectory.js')
module.exports = (fileNames) => {
module.exports = (files) => {
//delete all the psot files and thumbs using the filenames
return Promise.all(fileNames.map(async filename => {
//dont question it.
//delete all the files and thumbs
return Promise.all(files.map(async file => {
return Promise.all([
remove(`${uploadDirectory}img/${filename}`),
remove(`${uploadDirectory}img/thumb-${filename.split('.')[0]}.jpg`)
]).catch(e => console.error) //ignore for now
remove(`${uploadDirectory}img/${file.filename}`),
remove(`${uploadDirectory}img/thumb-${file.hash}.jpg`)
]).catch(e => console.error)
}));
}

@ -2,12 +2,12 @@ const gm = require('gm')
, configs = require(__dirname+'/../../configs/main.json')
, uploadDirectory = require(__dirname+'/uploadDirectory.js');
module.exports = (filename) => {
module.exports = (file) => {
return new Promise((resolve, reject) => {
gm(`${uploadDirectory}img/${filename}`)
gm(`${uploadDirectory}img/${file.filename}`)
.resize(128, 128)
.write(`${uploadDirectory}img/thumb-${filename.split('.')[0]}.jpg`, function (err) {
.write(`${uploadDirectory}img/thumb-${file.hash}.jpg`, function (err) {
if (err) {
return reject(err);
}

@ -2,17 +2,17 @@ const ffmpeg = require('fluent-ffmpeg')
, configs = require(__dirname+'/../../configs/main.json')
, uploadDirectory = require(__dirname+'/uploadDirectory.js');
module.exports = (filename, geometry) => {
module.exports = (file, geometry) => {
return new Promise((resolve, reject) => {
ffmpeg(`${uploadDirectory}img/${filename}`)
ffmpeg(`${uploadDirectory}img/${file.filename}`)
.on('end', () => {
return resolve();
})
.screenshots({
timestamps: ['1%'],//1% should remedy black first frames or fade-ins
count: 1,
filename: `thumb-${filename.split('.')[0]}.jpg`,
filename: `thumb-${file.hash}.jpg`,
folder: `${uploadDirectory}img/`,
size: geometry.width > geometry.height ? '128x?' : '?x128'
//keep aspect ratio, but also making sure taller/wider thumbs dont exceed 128 in either dimension

@ -1,50 +1,48 @@
'use strict';
const { remove } = require('fs-extra')
, { Files } = require(__dirname+'/../../db/')
, uploadDirectory = require(__dirname+'/../../helpers/files/uploadDirectory.js')
const { Files } = require(__dirname+'/../../db/')
, deletePostFiles = require(__dirname+'/../../helpers/files/deletepostfiles.js');
module.exports = async (posts, unlinkOnly) => {
//get filenames from all the posts
let fileNames = [];
let files = [];
for (let i = 0; i < posts.length; i++) {
const post = posts[i];
if (post.files.length > 0) {
fileNames = fileNames.concat(post.files.map(x => x.filename));
files = files.concat(post.files.map(file => {
return {
filename: file.filename,
hash: file.hash
};
}));
}
}
fileNames = [...new Set(fileNames)];
files = [...new Set(files)];
if (fileNames.length == 0) {
if (files.length == 0) {
return {
message: 'No files found'
};
}
if (fileNames.length > 0) {
await Files.decrement(fileNames);
if (files.length > 0) {
await Files.decrement(files.map(x => x.filename));
}
if (unlinkOnly) {
return {
message:`Unlinked ${fileNames.length} file(s) across ${posts.length} post(s)`,
message:`Unlinked ${files.length} file(s) across ${posts.length} post(s)`,
action:'$set',
query: {
'files': []
}
};
} else {
//delete all the files using the filenames
await Promise.all(fileNames.map(async filename => {
//dont question it.
return Promise.all([
remove(`${uploadDirectory}img/${filename}`),
remove(`${uploadDirectory}img/thumb-${filename.split('.')[0]}.jpg`)
])
}));
//delete all the files
await deletePostFiles(files);
return {
message:`Deleted ${fileNames.length} file(s) from server`,
message:`Deleted ${files.length} file(s) from server`,
//NOTE: only deletes from selected posts. other posts with same image will 404
action:'$set',
query: {

@ -155,7 +155,6 @@ module.exports = async (req, res, next) => {
filename: file.filename,
originalFilename: file.name,
mimetype: file.mimetype,
maintype: file.mimetype.split('/')[0],
size: file.size,
};
@ -166,7 +165,7 @@ module.exports = async (req, res, next) => {
const existsThumb = await pathExists(`${uploadDirectory}img/thumb-${processedFile.hash}.jpg`);
//handle video/image ffmpeg or graphicsmagick
switch (processedFile.maintype) {
switch (processedFile.mimetype.split('/')[0]) {
case 'image':
const imageData = await imageIdentify(req.files.file[i].tempFilePath, null, true);
processedFile.geometry = imageData.size // object with width and height pixels
@ -179,7 +178,7 @@ module.exports = async (req, res, next) => {
await imageUpload(file, processedFile.filename, 'img');
}
if (!existsThumb && processedFile.hasThumb) {
await imageThumbnail(processedFile.filename);
await imageThumbnail(processedFile);
}
processedFile = fixGifs(processedFile);
break;
@ -205,7 +204,7 @@ module.exports = async (req, res, next) => {
await videoUpload(file, processedFile.filename, 'img');
}
if (!existsThumb) {
await videoThumbnail(processedFile.filename, processedFile.geometry);
await videoThumbnail(processedFile, processedFile.geometry);
}
break;
default:

@ -17,7 +17,7 @@ mixin catalogtile(board, post, index)
img.catalog-thumb(src='/img/spoiler.png' width='64' height='64')
else
if post.files[0].hasThumb
img.catalog-thumb(src=`/img/thumb-${post.files[0].filename.split('.')[0]}.jpg` width='64' height='64')
img.catalog-thumb(src=`/img/thumb-${post.files[0].hash}.jpg` width='64' height='64')
else
img.catalog-thumb(src=`/img/${post.files[0].filename}` width='64' height='64')
if post.message

@ -57,7 +57,7 @@ mixin post(post, truncate, manage=false, globalmanage=false, ban=false)
if post.spoiler
img.file-thumb(src='/img/spoiler.png' width='128' height='128')
else if file.hasThumb
img.file-thumb(src=`/img/thumb-${file.filename.split('.')[0]}.jpg` height=file.geometry.thumbheight width=file.geometry.thumbwidth)
img.file-thumb(src=`/img/thumb-${file.hash}.jpg` height=file.geometry.thumbheight width=file.geometry.thumbwidth)
else
img.file-thumb(src=`/img/${file.filename}` height=file.geometry.height width=file.geometry.width)
if post.message

@ -11,10 +11,10 @@ block head
if thread.files.length > 0
if thread.spoiler
meta(property='og:image', content='/img/spoiler.png')
else if thread.files[0].hasThumb
meta(property='og:image', content='/img/thumb-'+thread.files[0].filename.split('.')[0]+'.jpg')
else if thread.files[0].hasThumb === true
meta(property='og:image', content=`/img/thumb-${thread.files[0].hash}.jpg`)
else
meta(property='og:image', content='/img/'+thread.files[0].filename)
meta(property='og:image', content=`/img/${thread.files[0].filename}`)
block content
include ../includes/boardheader.pug

Loading…
Cancel
Save