2018-07-30 02:23:33 +00:00
|
|
|
const fs = require('fs-extra')
|
2019-04-07 21:00:42 +00:00
|
|
|
const _ = require('lodash')
|
2018-07-30 02:23:33 +00:00
|
|
|
const path = require('path')
|
2019-04-07 21:00:42 +00:00
|
|
|
const tar = require('tar-fs')
|
|
|
|
const zlib = require('zlib')
|
|
|
|
const stream = require('stream')
|
|
|
|
const Promise = require('bluebird')
|
|
|
|
const pipeline = Promise.promisify(stream.pipeline)
|
|
|
|
const pageHelper = require('../../../helpers/page.js')
|
|
|
|
const moment = require('moment')
|
|
|
|
|
|
|
|
/* global WIKI */
|
2018-07-30 02:23:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get file extension based on content type
|
|
|
|
*/
|
|
|
|
const getFileExtension = (contentType) => {
|
|
|
|
switch (contentType) {
|
|
|
|
case 'markdown':
|
|
|
|
return 'md'
|
|
|
|
case 'html':
|
|
|
|
return 'html'
|
|
|
|
default:
|
|
|
|
return 'txt'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 05:12:43 +00:00
|
|
|
module.exports = {
|
2018-07-22 20:25:39 +00:00
|
|
|
async activated() {
|
2018-07-30 02:23:33 +00:00
|
|
|
// not used
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2018-07-22 20:25:39 +00:00
|
|
|
async deactivated() {
|
2018-07-30 02:23:33 +00:00
|
|
|
// not used
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2018-07-22 20:25:39 +00:00
|
|
|
async init() {
|
2019-02-03 22:08:06 +00:00
|
|
|
WIKI.logger.info('(STORAGE/DISK) Initializing...')
|
2018-07-30 02:23:33 +00:00
|
|
|
await fs.ensureDir(this.config.path)
|
2019-02-03 22:08:06 +00:00
|
|
|
WIKI.logger.info('(STORAGE/DISK) Initialization completed.')
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2019-04-07 21:00:42 +00:00
|
|
|
async sync({ manual } = { manual: false }) {
|
|
|
|
if (this.config.createDailyBackups || manual) {
|
|
|
|
const dirPath = path.join(this.config.path, manual ? '_manual' : '_daily')
|
|
|
|
await fs.ensureDir(dirPath)
|
|
|
|
|
|
|
|
const dateFilename = moment().format(manual ? 'YYYYMMDD-HHmmss' : 'DD')
|
|
|
|
|
|
|
|
WIKI.logger.info(`(STORAGE/DISK) Creating backup archive...`)
|
|
|
|
await pipeline(
|
|
|
|
tar.pack(this.config.path, {
|
|
|
|
ignore: (filePath) => {
|
|
|
|
return filePath.indexOf('_daily') >= 0 || filePath.indexOf('_manual') >= 0
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
zlib.createGzip(),
|
|
|
|
fs.createWriteStream(path.join(dirPath, `wiki-${dateFilename}.tar.gz`))
|
|
|
|
)
|
|
|
|
WIKI.logger.info('(STORAGE/DISK) Backup archive created successfully.')
|
|
|
|
}
|
2019-02-03 22:08:06 +00:00
|
|
|
},
|
|
|
|
async created(page) {
|
|
|
|
WIKI.logger.info(`(STORAGE/DISK) Creating file ${page.path}...`)
|
2019-06-25 02:13:41 +00:00
|
|
|
let fileName = `${page.path}.${getFileExtension(page.contentType)}`
|
|
|
|
if (WIKI.config.lang.namespacing && WIKI.config.lang.code !== page.localeCode) {
|
|
|
|
fileName = `${page.localeCode}/${fileName}`
|
|
|
|
}
|
|
|
|
const filePath = path.join(this.config.path, fileName)
|
2019-02-10 23:06:03 +00:00
|
|
|
await fs.outputFile(filePath, page.injectMetadata(), 'utf8')
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2019-02-03 22:08:06 +00:00
|
|
|
async updated(page) {
|
|
|
|
WIKI.logger.info(`(STORAGE/DISK) Updating file ${page.path}...`)
|
2019-06-25 02:13:41 +00:00
|
|
|
let fileName = `${page.path}.${getFileExtension(page.contentType)}`
|
|
|
|
if (WIKI.config.lang.namespacing && WIKI.config.lang.code !== page.localeCode) {
|
|
|
|
fileName = `${page.localeCode}/${fileName}`
|
|
|
|
}
|
|
|
|
const filePath = path.join(this.config.path, fileName)
|
2019-02-10 23:06:03 +00:00
|
|
|
await fs.outputFile(filePath, page.injectMetadata(), 'utf8')
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2019-02-03 22:08:06 +00:00
|
|
|
async deleted(page) {
|
|
|
|
WIKI.logger.info(`(STORAGE/DISK) Deleting file ${page.path}...`)
|
2019-06-25 02:13:41 +00:00
|
|
|
let fileName = `${page.path}.${getFileExtension(page.contentType)}`
|
|
|
|
if (WIKI.config.lang.namespacing && WIKI.config.lang.code !== page.localeCode) {
|
|
|
|
fileName = `${page.localeCode}/${fileName}`
|
|
|
|
}
|
|
|
|
const filePath = path.join(this.config.path, fileName)
|
2018-07-30 02:23:33 +00:00
|
|
|
await fs.unlink(filePath)
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2019-02-03 22:08:06 +00:00
|
|
|
async renamed(page) {
|
|
|
|
WIKI.logger.info(`(STORAGE/DISK) Renaming file ${page.sourcePath} to ${page.destinationPath}...`)
|
2019-06-25 02:13:41 +00:00
|
|
|
let sourceFilePath = `${page.sourcePath}.${getFileExtension(page.contentType)}`
|
|
|
|
let destinationFilePath = `${page.destinationPath}.${getFileExtension(page.contentType)}`
|
|
|
|
|
|
|
|
if (WIKI.config.lang.namespacing && WIKI.config.lang.code !== page.localeCode) {
|
|
|
|
sourceFilePath = `${page.localeCode}/${sourceFilePath}`
|
|
|
|
destinationFilePath = `${page.localeCode}/${destinationFilePath}`
|
|
|
|
}
|
|
|
|
await fs.move(path.join(this.config.path, sourceFilePath), path.join(this.config.path, destinationFilePath), { overwrite: true })
|
2019-04-07 21:00:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HANDLERS
|
|
|
|
*/
|
|
|
|
async dump() {
|
|
|
|
WIKI.logger.info(`(STORAGE/DISK) Dumping all content to disk...`)
|
|
|
|
await pipeline(
|
|
|
|
WIKI.models.knex.column('path', 'localeCode', 'title', 'description', 'contentType', 'content', 'isPublished', 'updatedAt').select().from('pages').where({
|
|
|
|
isPrivate: false
|
|
|
|
}).stream(),
|
|
|
|
new stream.Transform({
|
|
|
|
objectMode: true,
|
|
|
|
transform: async (page, enc, cb) => {
|
2019-06-25 02:13:41 +00:00
|
|
|
let fileName = `${page.path}.${getFileExtension(page.contentType)}`
|
|
|
|
if (WIKI.config.lang.namespacing && WIKI.config.lang.code !== page.localeCode) {
|
|
|
|
fileName = `${page.localeCode}/${fileName}`
|
|
|
|
}
|
2019-04-07 21:00:42 +00:00
|
|
|
WIKI.logger.info(`(STORAGE/DISK) Dumping ${fileName}...`)
|
|
|
|
const filePath = path.join(this.config.path, fileName)
|
|
|
|
await fs.outputFile(filePath, pageHelper.injectPageMetadata(page), 'utf8')
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
WIKI.logger.info('(STORAGE/DISK) All content was dumped to disk successfully.')
|
|
|
|
},
|
|
|
|
async backup() {
|
|
|
|
return this.sync({ manual: true })
|
2018-07-08 05:12:43 +00:00
|
|
|
}
|
|
|
|
}
|