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() {
|
2020-02-09 06:02:09 +00:00
|
|
|
return [...super.services, 'models', 'app']
|
2020-02-08 11:20:15 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* 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-09 11:55:07 +00:00
|
|
|
for (const page of pages) { //for all pages of the user, generate a copy of the data
|
2020-02-08 17:24:15 +00:00
|
|
|
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
|
2020-02-09 02:40:27 +00:00
|
|
|
const page0 = await user.get_root_page() //topmost page
|
2020-02-08 17:24:15 +00:00
|
|
|
const temp_dir = path.resolve(os.tmpdir(), uuid())
|
|
|
|
|
|
|
|
await fs.mkdir(temp_dir)
|
2020-02-09 06:02:09 +00:00
|
|
|
await this._markdown_recurse(page0, temp_dir)
|
2020-02-09 02:40:27 +00:00
|
|
|
|
|
|
|
/* Archive directory */
|
2020-02-08 18:19:25 +00:00
|
|
|
const exec = require('child_process').exec;
|
2020-02-09 06:02:09 +00:00
|
|
|
exec('cd ' + path.resolve(temp_dir) + '; zip -r archive.zip *', (error) => {
|
2020-02-08 18:19:25 +00:00
|
|
|
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-09 06:02:09 +00:00
|
|
|
const fs = require('fs').promises
|
2020-02-08 17:24:15 +00:00
|
|
|
const Page = this.models.get('api:Page')
|
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) {
|
2020-02-09 06:02:09 +00:00
|
|
|
const markdown = await this._generate_markdown(page)
|
|
|
|
await fs.writeFile(path.resolve(temp_dir, page.Name.replace(/\s/g, '_')) + '.md', markdown)
|
2020-02-08 17:24:15 +00:00
|
|
|
if (page.ChildPageIds && page.ChildPageIds.length) {
|
|
|
|
const sub_temp_dir = path.resolve(temp_dir, page.Name.replace(/\s/g, '_'))
|
2020-02-09 06:02:09 +00:00
|
|
|
// console.log(sub_temp_dir)
|
2020-02-08 17:24:15 +00:00
|
|
|
await fs.mkdir(sub_temp_dir)
|
2020-02-09 06:02:09 +00:00
|
|
|
await this._markdown_recurse(page, sub_temp_dir)
|
2020-02-08 17:24:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
2020-02-09 11:55:07 +00:00
|
|
|
const pages = await Page.find({ OrgUserId: user._id, ParentId: { $ne: '0' } })
|
2020-02-09 02:40:27 +00:00
|
|
|
const mapping = {} //keeps track of html file UUID and file name
|
|
|
|
const page0 = await user.get_root_page() //topmost page
|
2020-02-08 18:47:03 +00:00
|
|
|
|
2020-02-09 02:40:27 +00:00
|
|
|
for (const page of pages) {
|
|
|
|
const html_name = page.Name.replace(/\s/g, '_') + '.html'
|
|
|
|
mapping[page.UUID] = html_name
|
|
|
|
}
|
2020-02-09 09:19:20 +00:00
|
|
|
|
|
|
|
const sidebar_html = await this._generate_sidebar(mapping, user) //generates the sidebar contents
|
2020-02-09 06:02:09 +00:00
|
|
|
|
2020-02-09 02:40:27 +00:00
|
|
|
const temp_dir = path.resolve(os.tmpdir(), uuid()) //make temporary directory
|
2020-02-08 18:47:03 +00:00
|
|
|
await fs.mkdir(temp_dir)
|
2020-02-09 02:40:27 +00:00
|
|
|
/* Generates html files from template */
|
2020-02-09 06:02:09 +00:00
|
|
|
for (const page of pages) {
|
2020-02-09 02:40:27 +00:00
|
|
|
const fs = require('fs').promises
|
2020-02-09 06:02:09 +00:00
|
|
|
const contents = (await fs.readFile(path.resolve(this.app.directories.assets, 'export-template.html'))).toString('utf8')
|
|
|
|
const content_html = await this._generate_html(page)
|
2020-02-09 09:19:20 +00:00
|
|
|
const page_html = contents.replace('%%CONTENTS%%', content_html).replace('%%SIDEBAR%%', sidebar_html)
|
2020-02-09 02:40:27 +00:00
|
|
|
await fs.writeFile(path.resolve(temp_dir, mapping[page.UUID]), page_html)
|
|
|
|
}
|
|
|
|
/* Archive directory */
|
2020-02-08 18:47:03 +00:00
|
|
|
const exec = require('child_process').exec;
|
2020-02-09 09:19:20 +00:00
|
|
|
exec('cd ' + path.resolve(temp_dir) + '; zip -r archive.zip *', (error) => {
|
2020-02-08 18:47:03 +00:00
|
|
|
if (error) {
|
|
|
|
console.error(`exec error: ${error}`);
|
|
|
|
return res.error(500);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return res.sendFile(`${path.resolve(temp_dir, 'archive.zip')}`)
|
|
|
|
})
|
|
|
|
}
|
2020-02-09 09:19:20 +00:00
|
|
|
async _generate_sidebar(mapping, user) {// the absolutely dreadful method to create, sidebar
|
2020-02-09 06:02:09 +00:00
|
|
|
const Page = this.models.get('api:Page')
|
2020-02-09 09:19:20 +00:00
|
|
|
const Node = this.models.get('api:Node')
|
|
|
|
const virt_root = await user.get_root_page()
|
|
|
|
return await this._sidebar_recurse(mapping, virt_root)
|
2020-02-09 11:55:07 +00:00
|
|
|
|
2020-02-09 09:19:20 +00:00
|
|
|
}
|
|
|
|
async _sidebar_recurse(mapping, parent) {
|
|
|
|
const Page = this.models.get('api:Page')
|
|
|
|
const Node = this.models.get('api:Node')
|
2020-02-09 11:55:07 +00:00
|
|
|
const children = await Page.find({ ParentId: parent.UUID })
|
2020-02-09 09:19:20 +00:00
|
|
|
let base = ''
|
|
|
|
for (const child of children) {
|
|
|
|
if (child.ChildPageIds.length === 0) {
|
|
|
|
base += '<a class="nav-link ml-2" href="' + mapping[child.UUID] + '">' + child.Name + '</a>' //mapping[child.UUID] gives filename
|
|
|
|
} else {
|
|
|
|
base += '<a class="nav-link ml-2" href="' + mapping[child.UUID] + '">' + child.Name + '</a>'
|
|
|
|
base += '<div class="nav nav-pills flex-column ml-2">'
|
|
|
|
base += await this._sidebar_recurse(mapping, child)
|
|
|
|
base += '</div>'
|
2020-02-08 18:47:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-09 09:19:20 +00:00
|
|
|
return base
|
2020-02-08 18:47:03 +00:00
|
|
|
}
|
2020-02-09 02:40:27 +00:00
|
|
|
async _generate_html(page) { //generate html page from page and node data
|
2020-02-09 09:19:20 +00:00
|
|
|
const Page = this.models.get('api:Page')
|
2020-02-08 18:47:03 +00:00
|
|
|
const Node = this.models.get('api:Node')
|
|
|
|
const nodes = await Node.find({ PageId: page.UUID })
|
2020-02-09 02:40:27 +00:00
|
|
|
return nodes.map(x => { return x.to_html() }).join('\n')
|
2020-02-08 18:47:03 +00:00
|
|
|
}
|
2020-02-08 11:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = Export
|