mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2024-12-14 00:16:21 +00:00
feat: use fs-jetpack in pathsController.js
This commit is contained in:
parent
f3bf3f6500
commit
0eb4c243c3
@ -1,31 +1,9 @@
|
|||||||
const { promisify } = require('util')
|
const jetpack = require('fs-jetpack')
|
||||||
const fs = require('fs')
|
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const config = require('./../config')
|
const config = require('./../config')
|
||||||
const logger = require('./../logger')
|
|
||||||
|
|
||||||
const self = {}
|
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.uploads = path.resolve(config.uploads.folder)
|
||||||
self.chunks = config.uploads.chunksFolder
|
self.chunks = config.uploads.chunksFolder
|
||||||
? path.resolve(config.uploads.chunksFolder)
|
? path.resolve(config.uploads.chunksFolder)
|
||||||
@ -47,7 +25,10 @@ self.errorRoot = path.resolve(config.errorPages.rootDir)
|
|||||||
|
|
||||||
const verify = [
|
const verify = [
|
||||||
self.uploads,
|
self.uploads,
|
||||||
self.chunks,
|
{
|
||||||
|
path: self.chunks,
|
||||||
|
criteria: { empty: true }
|
||||||
|
},
|
||||||
self.thumbs,
|
self.thumbs,
|
||||||
self.zips,
|
self.zips,
|
||||||
self.logs,
|
self.logs,
|
||||||
@ -60,34 +41,13 @@ if (['better-sqlite3', 'sqlite3'].includes(config.database.client)) {
|
|||||||
|
|
||||||
self.initSync = () => {
|
self.initSync = () => {
|
||||||
// Check & create directories (synchronous)
|
// Check & create directories (synchronous)
|
||||||
for (const p of verify) {
|
for (const obj of verify) {
|
||||||
try {
|
if (typeof obj === 'object') {
|
||||||
fs.accessSync(p)
|
jetpack.dir(obj.path, obj.criteria)
|
||||||
} catch (err) {
|
|
||||||
if (err.code !== 'ENOENT') {
|
|
||||||
throw err
|
|
||||||
} else {
|
} else {
|
||||||
fs.mkdirSync(p)
|
jetpack.dir(obj)
|
||||||
logger.log(`Created directory: ${p}`)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
module.exports = self
|
||||||
|
@ -244,9 +244,6 @@ safe.use('/api', api)
|
|||||||
// Init database
|
// Init database
|
||||||
await require('./controllers/utils/initDatabase')(utils.db)
|
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) {
|
if (!Array.isArray(config.pages) || !config.pages.length) {
|
||||||
logger.error('Config file does not have any frontend pages enabled')
|
logger.error('Config file does not have any frontend pages enabled')
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
|
Loading…
Reference in New Issue
Block a user