feat: asset add/rename/remove + dump action for git and disk modules

This commit is contained in:
NGPixel
2019-10-19 17:39:45 -04:00
parent 73aa870abc
commit d987058336
7 changed files with 128 additions and 30 deletions

View File

@@ -88,12 +88,41 @@ module.exports = {
await fs.move(path.join(this.config.path, sourceFilePath), path.join(this.config.path, destinationFilePath), { overwrite: true })
},
/**
* ASSET UPLOAD
*
* @param {Object} asset Asset to upload
*/
async assetUploaded (asset) {
WIKI.logger.info(`(STORAGE/DISK) Creating new file ${asset.path}...`)
await fs.outputFile(path.join(this.config.path, asset.path), asset.data)
},
/**
* ASSET DELETE
*
* @param {Object} asset Asset to delete
*/
async assetDeleted (asset) {
WIKI.logger.info(`(STORAGE/DISK) Deleting file ${asset.path}...`)
await fs.remove(path.join(this.config.path, asset.path))
},
/**
* ASSET RENAME
*
* @param {Object} asset Asset to rename
*/
async assetRenamed (asset) {
WIKI.logger.info(`(STORAGE/DISK) Renaming file from ${asset.path} to ${asset.destinationPath}...`)
await fs.move(path.join(this.config.path, asset.path), path.join(this.config.path, asset.destinationPath), { overwrite: true })
},
/**
* HANDLERS
*/
async dump() {
WIKI.logger.info(`(STORAGE/DISK) Dumping all content to disk...`)
// -> Pages
await pipeline(
WIKI.models.knex.column('path', 'localeCode', 'title', 'description', 'contentType', 'content', 'isPublished', 'updatedAt').select().from('pages').where({
isPrivate: false
@@ -105,13 +134,30 @@ module.exports = {
if (WIKI.config.lang.code !== page.localeCode) {
fileName = `${page.localeCode}/${fileName}`
}
WIKI.logger.info(`(STORAGE/DISK) Dumping ${fileName}...`)
WIKI.logger.info(`(STORAGE/DISK) Dumping page ${fileName}...`)
const filePath = path.join(this.config.path, fileName)
await fs.outputFile(filePath, pageHelper.injectPageMetadata(page), 'utf8')
cb()
}
})
)
// -> Assets
const assetFolders = await WIKI.models.assetFolders.getAllPaths()
await pipeline(
WIKI.models.knex.column('filename', 'folderId', 'data').select().from('assets').join('assetData', 'assets.id', '=', 'assetData.id').stream(),
new stream.Transform({
objectMode: true,
transform: async (asset, enc, cb) => {
const filename = (asset.folderId && asset.folderId > 0) ? `${_.get(assetFolders, asset.folderId)}/${asset.filename}` : asset.filename
WIKI.logger.info(`(STORAGE/DISK) Dumping asset ${filename}...`)
await fs.outputFile(path.join(this.config.path, filename), asset.data)
cb()
}
})
)
WIKI.logger.info('(STORAGE/DISK) All content was dumped to disk successfully.')
},
async backup() {