2020-02-08 11:20:15 +00:00
|
|
|
const Controller = require('libflitter/controller/Controller')
|
2020-02-08 17:24:15 +00:00
|
|
|
const os = require('os');
|
|
|
|
const fs = require('fs').promises;
|
|
|
|
const uuid = require('uuid/v4');
|
|
|
|
const path = require('path');
|
2020-02-08 11:20:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Export Controller
|
|
|
|
* -------------------------------------------------------------
|
|
|
|
* Put some description here!
|
|
|
|
*/
|
|
|
|
class Export extends Controller {
|
|
|
|
static get services() {
|
|
|
|
return [...super.services, 'models']
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Serve the main page.
|
|
|
|
*/
|
2020-02-08 17:24:15 +00:00
|
|
|
async json_export(req, res) {
|
2020-02-08 11:20:15 +00:00
|
|
|
const Page = this.models.get('api:Page')
|
2020-02-08 13:56:01 +00:00
|
|
|
const Node = this.models.get('api:Node')
|
2020-02-08 17:24:15 +00:00
|
|
|
const user = req.user
|
|
|
|
const pages = await Page.find({ OrgUserId: user._id })
|
|
|
|
|
2020-02-08 11:20:15 +00:00
|
|
|
const ExportData = {}
|
2020-02-08 17:24:15 +00:00
|
|
|
for (const page of pages) {
|
|
|
|
const nodes = await Node.find({ PageId: page.UUID })
|
2020-02-08 13:12:26 +00:00
|
|
|
|
2020-02-08 17:24:15 +00:00
|
|
|
const PageData = { ...page }
|
|
|
|
PageData.Nodes = nodes.map(node => { return { ...node } })
|
2020-02-08 11:20:15 +00:00
|
|
|
ExportData[page.Name] = PageData
|
|
|
|
}
|
|
|
|
return res.api(ExportData)
|
|
|
|
}
|
2020-02-08 17:24:15 +00:00
|
|
|
async markdown_export(req, res) {
|
|
|
|
const Page = this.models.get('api:Page')
|
|
|
|
const Node = this.models.get('api:Node')
|
|
|
|
const user = req.user
|
|
|
|
const page0 = await Page.findOne({ OrgUserId: user._id, ParentId: '0' }) //Imaginary root
|
|
|
|
const temp_dir = path.resolve(os.tmpdir(), uuid())
|
|
|
|
|
|
|
|
await fs.mkdir(temp_dir)
|
|
|
|
this._markdown_recurse(page0, temp_dir)
|
2020-02-08 18:19:25 +00:00
|
|
|
|
|
|
|
const exec = require('child_process').exec;
|
|
|
|
exec('zip -r ' + path.resolve(temp_dir, 'archive.zip') + ' ' + path.resolve(temp_dir), (error) => {
|
|
|
|
if (error) {
|
|
|
|
console.error(`exec error: ${error}`);
|
|
|
|
return res.error(500);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.sendFile(`${path.resolve(temp_dir, 'archive.zip')}`)
|
|
|
|
})
|
2020-02-08 17:24:15 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 18:01:17 +00:00
|
|
|
async _markdown_recurse(root, temp_dir) { //recursive function for creating directory and generating markdown pages
|
2020-02-08 17:24:15 +00:00
|
|
|
console.log('root', root)
|
|
|
|
const Page = this.models.get('api:Page')
|
|
|
|
//const child = await root.childPages
|
2020-02-08 18:01:17 +00:00
|
|
|
const child = await Page.find({ UUID: { $in: root.ChildPageIds } })
|
|
|
|
|
2020-02-08 17:24:15 +00:00
|
|
|
for (const page of child) {
|
|
|
|
const markdown = this._generate_markdown(page)
|
|
|
|
if (page.ChildPageIds && page.ChildPageIds.length) {
|
|
|
|
const sub_temp_dir = path.resolve(temp_dir, page.Name.replace(/\s/g, '_'))
|
|
|
|
await fs.mkdir(sub_temp_dir)
|
|
|
|
this._markdown_recurse(page, sub_temp_dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 18:01:17 +00:00
|
|
|
async _generate_markdown(page) { //generate mark down
|
2020-02-08 17:24:15 +00:00
|
|
|
const Node = this.models.get('api:Node')
|
|
|
|
const nodes = await Node.find({ PageId: page.UUID })
|
2020-02-08 18:01:17 +00:00
|
|
|
return nodes.map(x => {
|
|
|
|
if (x.Type === 'click_link') {
|
|
|
|
return '[' + x.Value.Value + ']' + '(' + x.Value.Value + ')'
|
|
|
|
}
|
|
|
|
return x.Value.Value
|
|
|
|
}).join('\n')
|
2020-02-08 17:24:15 +00:00
|
|
|
}
|
2020-02-08 18:47:03 +00:00
|
|
|
async html_export(req, res) {
|
|
|
|
const Page = this.models.get('api:Page')
|
|
|
|
const Node = this.models.get('api:Node')
|
|
|
|
const user = req.user
|
|
|
|
const page0 = await Page.findOne({ OrgUserId: user._id, ParentId: '0' }) //Imaginary root
|
|
|
|
const temp_dir = path.resolve(os.tmpdir(), uuid())
|
|
|
|
|
|
|
|
await fs.mkdir(temp_dir)
|
|
|
|
this._html_recurse(page0, temp_dir)
|
|
|
|
|
|
|
|
const exec = require('child_process').exec;
|
|
|
|
exec('zip -r ' + path.resolve(temp_dir, 'archive.zip') + ' ' + path.resolve(temp_dir), (error) => {
|
|
|
|
if (error) {
|
|
|
|
console.error(`exec error: ${error}`);
|
|
|
|
return res.error(500);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.sendFile(`${path.resolve(temp_dir, 'archive.zip')}`)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
async _html_recurse(root, temp_dir) { //recursive function for creating directory and generating html pages
|
|
|
|
console.log('root', root)
|
|
|
|
const Page = this.models.get('api:Page')
|
|
|
|
//const child = await root.childPages
|
|
|
|
const child = await Page.find({ UUID: { $in: root.ChildPageIds } })
|
|
|
|
|
|
|
|
for (const page of child) {
|
|
|
|
const html = this._generate_html(page)
|
|
|
|
if (page.ChildPageIds && page.ChildPageIds.length) {
|
|
|
|
const sub_temp_dir = path.resolve(temp_dir, page.Name.replace(/\s/g, '_'))
|
|
|
|
await fs.mkdir(sub_temp_dir)
|
|
|
|
this._html_recurse(page, sub_temp_dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async _generate_html(page) { //generate html
|
|
|
|
const Node = this.models.get('api:Node')
|
|
|
|
const nodes = await Node.find({ PageId: page.UUID })
|
|
|
|
return nodes.map(x => {return x.to_html()}).join('\n')
|
|
|
|
}
|
2020-02-08 11:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = Export
|