diff --git a/controllers/pathsController.js b/controllers/pathsController.js index 636a085..eb5beba 100644 --- a/controllers/pathsController.js +++ b/controllers/pathsController.js @@ -1,31 +1,9 @@ -const { promisify } = require('util') -const fs = require('fs') +const jetpack = require('fs-jetpack') const path = require('path') const config = require('./../config') -const logger = require('./../logger') const self = {} -// Promisify these fs functions -const fsFuncs = [ - 'access', - 'copyFile', - 'lstat', - 'mkdir', - 'readdir', - 'readFile', - 'rename', - 'rmdir', - 'stat', - 'symlink', - 'unlink', - 'writeFile' -] - -for (const fsFunc of fsFuncs) { - self[fsFunc] = promisify(fs[fsFunc]) -} - self.uploads = path.resolve(config.uploads.folder) self.chunks = config.uploads.chunksFolder ? path.resolve(config.uploads.chunksFolder) @@ -47,7 +25,10 @@ self.errorRoot = path.resolve(config.errorPages.rootDir) const verify = [ self.uploads, - self.chunks, + { + path: self.chunks, + criteria: { empty: true } + }, self.thumbs, self.zips, self.logs, @@ -60,34 +41,13 @@ if (['better-sqlite3', 'sqlite3'].includes(config.database.client)) { self.initSync = () => { // Check & create directories (synchronous) - for (const p of verify) { - try { - fs.accessSync(p) - } catch (err) { - if (err.code !== 'ENOENT') { - throw err - } else { - fs.mkdirSync(p) - logger.log(`Created directory: ${p}`) - } + for (const obj of verify) { + if (typeof obj === 'object') { + jetpack.dir(obj.path, obj.criteria) + } else { + jetpack.dir(obj) } } } -self.purgeChunks = async () => { - // Purge any leftover in chunks directory - const uuidDirs = await self.readdir(self.chunks) - await Promise.all(uuidDirs.map(async uuid => { - const root = path.join(self.chunks, uuid) - const chunks = await self.readdir(root) - await Promise.all(chunks.map(chunk => - self.unlink(path.join(root, chunk)) - )) - await self.rmdir(root) - })) - if (uuidDirs.length) { - logger.log(`Purged ${uuidDirs.length} unfinished chunks`) - } -} - module.exports = self diff --git a/lolisafe.js b/lolisafe.js index 75b3d79..f50f885 100644 --- a/lolisafe.js +++ b/lolisafe.js @@ -244,9 +244,6 @@ safe.use('/api', api) // Init database await require('./controllers/utils/initDatabase')(utils.db) - // Purge any leftover in chunks directory, do not wait - paths.purgeChunks() - if (!Array.isArray(config.pages) || !config.pages.length) { logger.error('Config file does not have any frontend pages enabled') process.exit(1)