From ec0e51a7b89ba0cfd8a3e3f16dad4a66a23c6660 Mon Sep 17 00:00:00 2001 From: Bobby Date: Thu, 6 Oct 2022 02:44:31 +0700 Subject: [PATCH] feat: Constants --- controllers/utils/Constants.js | 7 +++++++ controllers/utilsController.js | 19 ++++++++----------- scripts/thumbs.js | 5 +++-- 3 files changed, 18 insertions(+), 13 deletions(-) create mode 100644 controllers/utils/Constants.js diff --git a/controllers/utils/Constants.js b/controllers/utils/Constants.js new file mode 100644 index 0000000..2f5e83e --- /dev/null +++ b/controllers/utils/Constants.js @@ -0,0 +1,7 @@ +const self = { + IMAGE_EXTS: ['.gif', '.jpeg', '.jpg', '.png', '.svg', '.tif', '.tiff', '.webp'], + VIDEO_EXTS: ['.3g2', '.3gp', '.asf', '.avchd', '.avi', '.divx', '.evo', '.flv', '.h264', '.h265', '.hevc', '.m2p', '.m2ts', '.m4v', '.mk3d', '.mkv', '.mov', '.mp4', '.mpeg', '.mpg', '.mxf', '.ogg', '.ogv', '.ps', '.qt', '.rmvb', '.ts', '.vob', '.webm', '.wmv'], + AUDIO_EXTS: ['.flac', '.mp3', '.wav', '.wma'] +} + +module.exports = self diff --git a/controllers/utilsController.js b/controllers/utilsController.js index 32a860a..6381b42 100644 --- a/controllers/utilsController.js +++ b/controllers/utilsController.js @@ -11,6 +11,7 @@ const sharp = require('sharp') const si = require('systeminformation') const paths = require('./pathsController') const perms = require('./permissionController') +const Constants = require('./utils/Constants') const ClientError = require('./utils/ClientError') const ServerError = require('./utils/ServerError') const SimpleDataStore = require('./utils/SimpleDataStore') @@ -38,10 +39,6 @@ const self = { idMaxTries: config.uploads.maxTries || 1, - imageExts: ['.gif', '.jpeg', '.jpg', '.png', '.svg', '.tif', '.tiff', '.webp'], - videoExts: ['.3g2', '.3gp', '.asf', '.avchd', '.avi', '.divx', '.evo', '.flv', '.h264', '.h265', '.hevc', '.m2p', '.m2ts', '.m4v', '.mk3d', '.mkv', '.mov', '.mp4', '.mpeg', '.mpg', '.mxf', '.ogg', '.ogv', '.ps', '.qt', '.rmvb', '.ts', '.vob', '.webm', '.wmv'], - audioExts: ['.flac', '.mp3', '.wav', '.wma'], - stripTagsBlacklistedExts: Array.isArray(config.uploads.stripTags.blacklistExtensions) ? config.uploads.stripTags.blacklistExtensions : [], @@ -290,8 +287,8 @@ const cloudflarePurgeCacheQueue = cloudflareAuth && fastq.promise(async chunk => self.mayGenerateThumb = extname => { extname = extname.toLowerCase() - return (config.uploads.generateThumbs.image && self.imageExts.includes(extname)) || - (config.uploads.generateThumbs.video && self.videoExts.includes(extname)) + return (config.uploads.generateThumbs.image && Constants.IMAGE_EXTS.includes(extname)) || + (config.uploads.generateThumbs.video && Constants.VIDEO_EXTS.includes(extname)) } // Expand if necessary (should be case-insensitive) @@ -455,7 +452,7 @@ self.generateThumbs = async (name, extname, force) => { const input = path.join(paths.uploads, name) // If image extension - if (self.imageExts.includes(extname)) { + if (Constants.IMAGE_EXTS.includes(extname)) { const resizeOptions = { width: self.thumbsSize, height: self.thumbsSize, @@ -489,7 +486,7 @@ self.generateThumbs = async (name, extname, force) => { }) .toFile(thumbname) } - } else if (self.videoExts.includes(extname)) { + } else if (Constants.VIDEO_EXTS.includes(extname)) { const metadata = await self.ffprobe(input) const duration = parseInt(metadata.format.duration) @@ -556,13 +553,13 @@ self.stripTags = async (name, extname) => { let isError try { - if (self.imageExts.includes(extname)) { + if (Constants.IMAGE_EXTS.includes(extname)) { const tmpName = `tmp-${name}` tmpPath = path.join(paths.uploads, tmpName) await jetpack.renameAsync(fullPath, tmpName) await sharp(tmpPath) .toFile(fullPath) - } else if (config.uploads.stripTags.video && self.videoExts.includes(extname)) { + } else if (config.uploads.stripTags.video && Constants.VIDEO_EXTS.includes(extname)) { const tmpName = `tmp-${name}` tmpPath = path.join(paths.uploads, tmpName) await jetpack.renameAsync(fullPath, tmpName) @@ -605,7 +602,7 @@ self.unlinkFile = async (filename, predb) => { const identifier = filename.split('.')[0] const extname = self.extname(filename, true) - if (self.imageExts.includes(extname) || self.videoExts.includes(extname)) { + if (Constants.IMAGE_EXTS.includes(extname) || Constants.VIDEO_EXTS.includes(extname)) { await jetpack.removeAsync(path.join(paths.thumbs, `${identifier}.png`)) } } diff --git a/scripts/thumbs.js b/scripts/thumbs.js index dbf3f32..8783129 100644 --- a/scripts/thumbs.js +++ b/scripts/thumbs.js @@ -2,12 +2,13 @@ const jetpack = require('fs-jetpack') const path = require('path') const paths = require('./../controllers/pathsController') const utils = require('./../controllers/utilsController') +const Constants = require('./../controllers/utils/Constants') const self = { mode: null, mayGenerateThumb: extname => { - return ([1, 3].includes(self.mode) && utils.imageExts.includes(extname)) || - ([2, 3].includes(self.mode) && utils.videoExts.includes(extname)) + return ([1, 3].includes(self.mode) && Constants.IMAGE_EXTS.includes(extname)) || + ([2, 3].includes(self.mode) && Constants.VIDEO_EXTS.includes(extname)) }, getFiles: async directory => { const names = await jetpack.listAsync(directory)