export subtree to filesystem

This commit is contained in:
azivner 2017-12-02 21:48:22 -05:00
parent 0521deb304
commit 119d085856
4 changed files with 55 additions and 1 deletions

View file

@ -28,6 +28,7 @@
"ini": "^1.3.4",
"request": "^2.83.0",
"request-promise": "^4.2.2",
"rimraf": "^2.6.2",
"scrypt": "^6.0.3",
"serve-favicon": "~2.4.5",
"session-file-store": "^1.1.2",

49
routes/api/export.js Normal file
View file

@ -0,0 +1,49 @@
"use strict";
const express = require('express');
const router = express.Router();
const rimraf = require('rimraf');
const fs = require('fs');
const sql = require('../../services/sql');
const data_dir = require('../../services/data_dir');
router.get('/:noteId/to/:directory', async (req, res, next) => {
const noteId = req.params.noteId;
const directory = req.params.directory.replace(/[^0-9a-zA-Z_-]/gi, '');
if (!fs.existsSync(data_dir.EXPORT_DIR)) {
fs.mkdirSync(data_dir.EXPORT_DIR);
}
const completeExportDir = data_dir.EXPORT_DIR + '/' + directory;
if (fs.existsSync(completeExportDir)) {
rimraf.sync(completeExportDir);
}
fs.mkdirSync(completeExportDir);
await exportNote(noteId, completeExportDir);
res.send({});
});
async function exportNote(noteId, dir) {
const note = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]);
fs.writeFileSync(dir + '/' + note.note_title + '.html', note.note_text);
const children = await sql.getResults("SELECT * FROM notes_tree WHERE note_pid = ? AND is_deleted = 0", [noteId]);
if (children.length > 0) {
const childrenDir = dir + '/' + note.note_title;
fs.mkdirSync(childrenDir);
for (const child of children) {
await exportNote(child.note_id, childrenDir);
}
}
}
module.exports = router;

View file

@ -17,6 +17,7 @@ const loginApiRoute = require('./api/login');
const eventLogRoute = require('./api/event_log');
const recentNotesRoute = require('./api/recent_notes');
const appInfoRoute = require('./api/app_info');
const exportRoute = require('./api/export');
function register(app) {
app.use('/', indexRoute);
@ -37,6 +38,7 @@ function register(app) {
app.use('/api/event-log', eventLogRoute);
app.use('/api/recent-notes', recentNotesRoute);
app.use('/api/app-info', appInfoRoute);
app.use('/api/export', exportRoute);
}
module.exports = {

View file

@ -12,10 +12,12 @@ if (!fs.existsSync(TRILIUM_DATA_DIR)) {
const DOCUMENT_PATH = TRILIUM_DATA_DIR + "/document.db";
const BACKUP_DIR = TRILIUM_DATA_DIR + "/backup";
const LOG_DIR = TRILIUM_DATA_DIR + "/log";
const EXPORT_DIR = TRILIUM_DATA_DIR + "/export";
module.exports = {
TRILIUM_DATA_DIR,
DOCUMENT_PATH,
BACKUP_DIR,
LOG_DIR
LOG_DIR,
EXPORT_DIR
};