mirror of
https://github.com/zadam/trilium.git
synced 2025-11-01 02:45:54 +01:00
export subtree to tar file
This commit is contained in:
@@ -2,56 +2,67 @@
|
||||
|
||||
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');
|
||||
const attributes = require('../../services/attributes');
|
||||
const html = require('html');
|
||||
const auth = require('../../services/auth');
|
||||
const wrap = require('express-promise-wrap').wrap;
|
||||
const tar = require('tar-stream');
|
||||
const sanitize = require("sanitize-filename");
|
||||
|
||||
router.get('/:noteId/to/:directory', auth.checkApiAuth, wrap(async (req, res, next) => {
|
||||
router.get('/:noteId/', auth.checkApiAuth, wrap(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);
|
||||
|
||||
const noteTreeId = await sql.getValue('SELECT noteTreeId FROM note_tree WHERE noteId = ?', [noteId]);
|
||||
|
||||
await exportNote(noteTreeId, completeExportDir);
|
||||
const pack = tar.pack();
|
||||
|
||||
res.send({});
|
||||
const name = await exportNote(noteTreeId, '', pack);
|
||||
|
||||
pack.finalize();
|
||||
|
||||
res.setHeader('Content-Disposition', 'attachment; filename="' + name + '.tar"');
|
||||
res.setHeader('Content-Type', 'application/tar');
|
||||
|
||||
pack.pipe(res);
|
||||
}));
|
||||
|
||||
async function exportNote(noteTreeId, dir) {
|
||||
async function exportNote(noteTreeId, directory, pack) {
|
||||
const noteTree = await sql.getRow("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]);
|
||||
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteTree.noteId]);
|
||||
|
||||
const pos = (noteTree.notePosition + '').padStart(4, '0');
|
||||
const content = note.type === 'text' ? html.prettyPrint(note.content, {indent_size: 2}) : note.content;
|
||||
|
||||
fs.writeFileSync(dir + '/' + pos + '-' + note.title + '.html', html.prettyPrint(note.content, {indent_size: 2}));
|
||||
const childFileName = directory + sanitize(note.title);
|
||||
|
||||
console.log(childFileName);
|
||||
|
||||
pack.entry({ name: childFileName + ".dat", size: content.length }, content);
|
||||
|
||||
const metadata = await getMetadata(note);
|
||||
|
||||
pack.entry({ name: childFileName + ".meta", size: metadata.length }, metadata);
|
||||
|
||||
const children = await sql.getRows("SELECT * FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [note.noteId]);
|
||||
|
||||
if (children.length > 0) {
|
||||
const childrenDir = dir + '/' + pos + '-' + note.title;
|
||||
|
||||
fs.mkdirSync(childrenDir);
|
||||
|
||||
for (const child of children) {
|
||||
await exportNote(child.noteTreeId, childrenDir);
|
||||
await exportNote(child.noteTreeId, childFileName + "/", pack);
|
||||
}
|
||||
}
|
||||
|
||||
return childFileName;
|
||||
}
|
||||
|
||||
async function getMetadata(note) {
|
||||
const meta = {
|
||||
title: note.title,
|
||||
type: note.type,
|
||||
mime: note.mime,
|
||||
attributes: await attributes.getNoteAttributeMap(note.noteId)
|
||||
};
|
||||
|
||||
return JSON.stringify(meta, null, '\t')
|
||||
}
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user