fix: verify database dir

This commit is contained in:
Bobby Wibowo 2022-06-22 14:23:54 +07:00
parent 6b0f03e72c
commit f37313a84c
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
3 changed files with 18 additions and 9 deletions

View File

@ -54,21 +54,27 @@ const verify = [
self.customPages
]
self.init = async () => {
// Check & create directories
if (['better-sqlite3', 'sqlite3'].includes(config.database.client)) {
verify.unshift(path.resolve('database'))
}
self.initSync = () => {
// Check & create directories (synchronous)
for (const p of verify) {
try {
await self.access(p)
fs.accessSync(p)
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
} else {
const mkdir = await self.mkdir(p)
if (mkdir) logger.log(`Created directory: ${p}`)
fs.mkdirSync(p)
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 => {
@ -79,7 +85,9 @@ self.init = async () => {
))
await self.rmdir(root)
}))
if (uuidDirs.length) logger.log(`Purged ${uuidDirs.length} unfinished chunks`)
if (uuidDirs.length) {
logger.log(`Purged ${uuidDirs.length} unfinished chunks`)
}
}
module.exports = self

View File

@ -41,6 +41,7 @@ logger.log('Starting lolisafe\u2026')
const safe = express()
const paths = require('./controllers/pathsController')
paths.initSync()
const utils = require('./controllers/utilsController')
const album = require('./routes/album')
@ -251,8 +252,8 @@ safe.use('/api', api)
// Init database
await require('./controllers/utils/initDatabase.js')(utils.db)
// Verify paths, create missing ones, clean up temp ones
await paths.init()
// 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')

View File

@ -21,7 +21,7 @@ const map = {
}
;(async () => {
if (config.database && ['better-sqlite3', 'sqlite3'].includes(config.database.client)) {
if (['better-sqlite3', 'sqlite3'].includes(config.database.client)) {
try {
await paths.access(config.database.connection.filename)
} catch (err) {