trilium/src/services/export/tar.js

246 lines
7.5 KiB
JavaScript
Raw Normal View History

"use strict";
const html = require('html');
const repository = require('../repository');
2018-11-25 03:58:38 +08:00
const tar = require('tar-stream');
2018-12-01 05:43:03 +08:00
const path = require('path');
2018-11-24 21:44:56 +08:00
const mimeTypes = require('mime-types');
const TurndownService = require('turndown');
const packageInfo = require('../../../package.json');
const utils = require('../utils');
const sanitize = require("sanitize-filename");
2018-11-24 21:44:56 +08:00
/**
2019-02-11 05:30:55 +08:00
* @param {ExportContext} exportContext
* @param {Branch} branch
* @param {string} format - 'html' or 'markdown'
2018-11-24 21:44:56 +08:00
*/
2019-02-11 05:30:55 +08:00
async function exportToTar(exportContext, branch, format, res) {
let turndownService = format === 'markdown' ? new TurndownService() : null;
2018-11-25 17:26:45 +08:00
2018-11-25 03:58:38 +08:00
const pack = tar.pack();
const noteIdToMeta = {};
function getUniqueFilename(existingFileNames, fileName) {
const lcFileName = fileName.toLowerCase();
2018-11-25 17:26:45 +08:00
if (lcFileName in existingFileNames) {
let index;
let newName;
do {
index = existingFileNames[lcFileName]++;
newName = lcFileName + "_" + index;
}
while (newName in existingFileNames);
return fileName + "_" + index;
}
else {
existingFileNames[lcFileName] = 1;
return fileName;
}
2018-11-25 17:26:45 +08:00
}
function getDataFileName(note, baseFileName, existingFileNames) {
let extension;
if (note.type === 'text' && format === 'markdown') {
extension = 'md';
}
2018-12-01 05:43:03 +08:00
else if (note.mime === 'application/x-javascript' || note.mime === 'text/javascript') {
extension = 'js';
}
else {
extension = mimeTypes.extension(note.mime) || "dat";
}
let fileName = baseFileName;
2018-12-01 05:43:03 +08:00
const existingExtension = path.extname(fileName).toLowerCase();
2018-12-01 05:43:03 +08:00
// if the note is already named with extension (e.g. "jquery.js"), then it's silly to append exact same extension again
if (existingExtension !== extension) {
fileName += "." + extension;
}
return getUniqueFilename(existingFileNames, fileName);
}
async function getNote(branch, existingFileNames) {
const note = await branch.getNote();
if (await note.hasLabel('excludeFromExport')) {
return;
}
const baseFileName = sanitize(branch.prefix ? (branch.prefix + ' - ' + note.title) : note.title);
if (note.noteId in noteIdToMeta) {
const fileName = getUniqueFilename(existingFileNames, baseFileName + ".clone");
return {
2018-11-26 21:47:46 +08:00
isClone: true,
noteId: note.noteId,
prefix: branch.prefix,
dataFileName: fileName
};
}
const meta = {
2018-11-26 21:47:46 +08:00
isClone: false,
noteId: note.noteId,
title: note.title,
notePosition: branch.notePosition,
prefix: branch.prefix,
isExpanded: branch.isExpanded,
type: note.type,
mime: note.mime,
// we don't export dateCreated and dateModified of any entity since that would be a bit misleading
attributes: (await note.getOwnedAttributes()).map(attribute => {
return {
type: attribute.type,
name: attribute.name,
value: attribute.value,
isInheritable: attribute.isInheritable,
position: attribute.position
};
}),
links: (await note.getLinks()).map(link => {
return {
type: link.type,
targetNoteId: link.targetNoteId
}
})
};
2019-02-11 05:30:55 +08:00
exportContext.increaseProgressCount();
2018-11-24 21:44:56 +08:00
if (note.type === 'text') {
meta.format = format;
2018-11-24 21:44:56 +08:00
}
noteIdToMeta[note.noteId] = meta;
const childBranches = await note.getChildBranches();
// if it's a leaf then we'll export it even if it's empty
2019-02-07 04:29:23 +08:00
if ((await note.getContent()).length > 0 || childBranches.length === 0) {
meta.dataFileName = getDataFileName(note, baseFileName, existingFileNames);
}
if (childBranches.length > 0) {
meta.dirFileName = getUniqueFilename(existingFileNames, baseFileName);
meta.children = [];
2018-11-27 05:22:16 +08:00
// namespace is shared by children in the same note
const childExistingNames = {};
for (const childBranch of childBranches) {
2018-11-27 05:22:16 +08:00
const note = await getNote(childBranch, childExistingNames);
2018-11-24 21:44:56 +08:00
// can be undefined if export is disabled for this note
if (note) {
meta.children.push(note);
}
2018-11-24 21:44:56 +08:00
}
}
return meta;
}
2019-02-07 04:29:23 +08:00
async function prepareContent(note, format) {
const content = await note.getContent();
if (format === 'html') {
2019-02-07 04:29:23 +08:00
if (!content.toLowerCase().includes("<html")) {
note.content = '<html><head><meta charset="utf-8"></head><body>' + content + '</body></html>';
}
2019-02-07 04:29:23 +08:00
return html.prettyPrint(content, {indent_size: 2});
}
else if (format === 'markdown') {
2019-02-07 04:29:23 +08:00
return turndownService.turndown(content);
}
else {
2019-02-07 04:29:23 +08:00
return content;
2018-11-25 17:26:45 +08:00
}
}
// noteId => file path
const notePaths = {};
async function saveNote(noteMeta, path) {
2018-11-26 21:47:46 +08:00
if (noteMeta.isClone) {
const content = "Note is present at " + notePaths[noteMeta.noteId];
2018-11-27 05:22:16 +08:00
pack.entry({name: path + noteMeta.dataFileName, size: content.length}, content);
return;
2018-11-24 21:44:56 +08:00
}
const note = await repository.getNote(noteMeta.noteId);
2018-11-27 05:22:16 +08:00
notePaths[note.noteId] = path + (noteMeta.dataFileName || noteMeta.dirFileName);
if (noteMeta.dataFileName) {
2019-02-07 04:29:23 +08:00
const content = await prepareContent(note, noteMeta.format);
2018-11-24 21:44:56 +08:00
2018-11-27 05:22:16 +08:00
pack.entry({name: path + noteMeta.dataFileName, size: content.length}, content);
2018-11-24 21:44:56 +08:00
}
2019-02-11 05:30:55 +08:00
exportContext.increaseProgressCount();
if (noteMeta.children && noteMeta.children.length > 0) {
2018-11-27 05:22:16 +08:00
const directoryPath = path + noteMeta.dirFileName;
pack.entry({name: directoryPath, type: 'directory'});
for (const childMeta of noteMeta.children) {
2018-11-27 05:22:16 +08:00
await saveNote(childMeta, directoryPath + '/');
}
}
}
const metaFile = {
formatVersion: 1,
appVersion: packageInfo.version,
files: [
await getNote(branch, [])
]
};
for (const noteMeta of Object.values(noteIdToMeta)) {
// filter out relations and links which are not inside this export
noteMeta.attributes = noteMeta.attributes.filter(attr => attr.type !== 'relation' || attr.value in noteIdToMeta);
noteMeta.links = noteMeta.links.filter(link => link.targetNoteId in noteIdToMeta);
}
if (!metaFile.files[0]) { // corner case of disabled export for exported note
res.sendStatus(400);
return;
}
const metaFileJson = JSON.stringify(metaFile, null, '\t');
pack.entry({name: "!!!meta.json", size: metaFileJson.length}, metaFileJson);
await saveNote(metaFile.files[0], '');
pack.finalize();
const note = await branch.getNote();
const tarFileName = (branch.prefix ? (branch.prefix + " - ") : "") + note.title + ".tar";
res.setHeader('Content-Disposition', utils.getContentDisposition(tarFileName));
res.setHeader('Content-Type', 'application/tar');
pack.pipe(res);
2019-02-11 05:30:55 +08:00
exportContext.exportFinished();
}
module.exports = {
exportToTar
};