fix: errors in Util caused by separating into different classes improperly

This commit is contained in:
Zephyrrus 2020-07-08 02:32:12 +03:00
parent 5d61b4d000
commit eccbb1ca93
4 changed files with 45 additions and 26 deletions

View File

@ -9,6 +9,7 @@ const start = async () => {
const files = fs.readdirSync(path.join(__dirname, '..', '..', process.env.UPLOAD_FOLDER));
for (const fileName of files) {
console.log(`Generating thumb for '${fileName}`);
// eslint-disable-next-line no-await-in-loop
await ThumbUtil.generateThumbnails(fileName);
}
};

View File

@ -22,6 +22,11 @@ class Log {
else console.log(chalk.red(args)); // eslint-disable-line no-console
}
static debug(args) {
if (this.checkIfArrayOrObject(args)) dump(args);
else console.log(chalk.gray(args)); // eslint-disable-line no-console
}
/*
static dump(args) {
dump(args);

View File

@ -8,19 +8,22 @@ const log = require('./Log');
class ThumbUtil {
static imageExtensions = ['.jpg', '.jpeg', '.gif', '.png', '.webp'];
static videoExtensions = ['.webm', '.mp4', '.wmv', '.avi', '.mov'];
static thumbPath = path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs');
static squareThumbPath = path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', 'square');
static videoPreviewPath = path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', 'preview');
static thumbPath = path.join(__dirname, '../../../', process.env.UPLOAD_FOLDER, 'thumbs');
static squareThumbPath = path.join(__dirname, '../../../', process.env.UPLOAD_FOLDER, 'thumbs', 'square');
static videoPreviewPath = path.join(__dirname, '../../../', process.env.UPLOAD_FOLDER, 'thumbs', 'preview');
static generateThumbnails(filename) {
const ext = path.extname(filename).toLowerCase();
const output = `${filename.slice(0, -ext.length)}.png`;
const previewOutput = `${filename.slice(0, -ext.length)}.webm`;
if (ThumbUtil.imageExtensions.includes(ext)) return this.generateThumbnailForImage(filename, output);
if (ThumbUtil.videoExtensions.includes(ext)) return this.generateThumbnailForVideo(filename, previewOutput);
if (ThumbUtil.imageExtensions.includes(ext)) return ThumbUtil.generateThumbnailForImage(filename, output);
if (ThumbUtil.videoExtensions.includes(ext)) return ThumbUtil.generateThumbnailForVideo(filename, previewOutput);
return null;
}
@ -46,28 +49,28 @@ class ThumbUtil {
timestamps: [0],
filename: '%b.png',
folder: ThumbUtil.squareThumbPath,
size: '64x64'
size: '64x64',
})
.on('error', error => log.error(error.message));
.on('error', (error) => log.error(error.message));
ffmpeg(filePath)
.thumbnail({
timestamps: [0],
filename: '%b.png',
folder: ThumbUtil.thumbPath,
size: '150x?'
size: '150x?',
})
.on('error', error => log.error(error.message));
.on('error', (error) => log.error(error.message));
try {
await previewUtil({
input: filePath,
width: 150,
output: path.join(ThumbUtil.videoPreviewPath, output),
log: console.log
log: log.debug,
});
} catch (e) {
console.error(e);
log.error(e);
}
}
@ -77,16 +80,22 @@ class ThumbUtil {
const ext = path.extname(filename).toLowerCase();
if (!ThumbUtil.imageExtensions.includes(ext) && !ThumbUtil.videoExtensions.includes(ext)) return null;
if (ThumbUtil.imageExtensions.includes(ext)) return { thumb: `${filename.slice(0, -ext.length)}.png` };
if (ThumbUtil.videoExtensions.includes(ext))
if (ThumbUtil.videoExtensions.includes(ext)) {
return {
thumb: `${filename.slice(0, -ext.length)}.png`,
preview: `${filename.slice(0, -ext.length)}.webm`
preview: `${filename.slice(0, -ext.length)}.webm`,
};
}
}
static async removeThumbs(thumbName) {
await jetpack.removeAsync(path.join(ThumbUtil.thumbPath, thumbName));
await jetpack.removeAsync(ThumbUtil.squareThumbPath, thumbName);
static async removeThumbs({ thumb, preview }) {
if (thumb) {
await jetpack.removeAsync(path.join(ThumbUtil.thumbPath, thumb));
await jetpack.removeAsync(path.join(ThumbUtil.squareThumbPath, thumb));
}
if (preview) {
await jetpack.removeAsync(path.join(ThumbUtil.videoPreviewPath, preview));
}
}
}

View File

@ -1,3 +1,4 @@
/* eslint-disable no-await-in-loop */
const jetpack = require('fs-jetpack');
const randomstring = require('randomstring');
const path = require('path');
@ -9,9 +10,9 @@ const db = require('knex')({
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
filename: path.join(__dirname, '..', '..', '..', 'database.sqlite')
filename: path.join(__dirname, '../../../database.sqlite'),
},
useNullAsDefault: process.env.DB_CLIENT === 'sqlite' ? true : false
useNullAsDefault: process.env.DB_CLIENT === 'sqlite',
});
const moment = require('moment');
const crypto = require('crypto');
@ -51,11 +52,10 @@ class Util {
static getUniqueFilename(name) {
const retry = (i = 0) => {
const filename =
randomstring.generate({
length: parseInt(process.env.GENERATED_FILENAME_LENGTH, 10),
capitalization: 'lowercase'
}) + path.extname(name).toLowerCase();
const filename = randomstring.generate({
length: parseInt(process.env.GENERATED_FILENAME_LENGTH, 10),
capitalization: 'lowercase',
}) + path.extname(name).toLowerCase();
// TODO: Change this to look for the file in the db instead of in the filesystem
const exists = jetpack.exists(path.join(Util.uploadPath, filename));
@ -71,7 +71,7 @@ class Util {
const retry = async (i = 0) => {
const identifier = randomstring.generate({
length: parseInt(process.env.GENERATED_ALBUM_LENGTH, 10),
capitalization: 'lowercase'
capitalization: 'lowercase',
});
const exists = await db
.table('links')
@ -138,7 +138,9 @@ class Util {
.table('files')
.where({ id: fileAlbum.fileId })
.first();
if (!file) continue;
await this.deleteFile(file.name, true);
}
} catch (error) {
@ -211,13 +213,15 @@ class Util {
'..',
process.env.UPLOAD_FOLDER,
'zips',
`${album.userId}-${album.id}.zip`
)
`${album.userId}-${album.id}.zip`,
),
);
} catch (error) {
log.error(error);
}
}
static generateThumbnails = ThumbUtil.generateThumbnails;
}
module.exports = Util;