mirror of
https://github.com/zadam/trilium.git
synced 2025-11-01 19:05:59 +01:00
use archiver.js to export instead of yazl, #1938
This commit is contained in:
@@ -12,7 +12,7 @@ const protectedSessionService = require('../protected_session');
|
||||
const sanitize = require("sanitize-filename");
|
||||
const fs = require("fs");
|
||||
const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR;
|
||||
const yazl = require("yazl");
|
||||
const archiver = require('archiver');
|
||||
|
||||
/**
|
||||
* @param {TaskContext} taskContext
|
||||
@@ -20,7 +20,9 @@ const yazl = require("yazl");
|
||||
* @param {string} format - 'html' or 'markdown'
|
||||
*/
|
||||
function exportToZip(taskContext, branch, format, res) {
|
||||
const zipFile = new yazl.ZipFile();
|
||||
const archive = archiver('zip', {
|
||||
zlib: { level: 9 } // Sets the compression level.
|
||||
});
|
||||
|
||||
const noteIdToMeta = {};
|
||||
|
||||
@@ -268,7 +270,7 @@ ${content}
|
||||
|
||||
content = prepareContent(noteMeta.title, content, noteMeta);
|
||||
|
||||
zipFile.addBuffer(content, filePathPrefix + noteMeta.dataFileName);
|
||||
archive.append(content, { name: filePathPrefix + noteMeta.dataFileName });
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -280,7 +282,7 @@ ${content}
|
||||
if (noteMeta.dataFileName) {
|
||||
const content = prepareContent(noteMeta.title, note.getContent(), noteMeta);
|
||||
|
||||
zipFile.addBuffer(content, filePathPrefix + noteMeta.dataFileName, {mtime: dateUtils.parseDateTime(note.utcDateModified)});
|
||||
archive.append(content, { name: filePathPrefix + noteMeta.dataFileName, date: dateUtils.parseDateTime(note.utcDateModified) });
|
||||
}
|
||||
|
||||
taskContext.increaseProgressCount();
|
||||
@@ -288,7 +290,8 @@ ${content}
|
||||
if (noteMeta.children && noteMeta.children.length > 0) {
|
||||
const directoryPath = filePathPrefix + noteMeta.dirFileName;
|
||||
|
||||
zipFile.addEmptyDirectory(directoryPath, {mtime: dateUtils.parseDateTime(note.utcDateModified)});
|
||||
// create directory
|
||||
archive.append('', { name: directoryPath + '/', date: dateUtils.parseDateTime(note.utcDateModified) });
|
||||
|
||||
for (const childMeta of noteMeta.children) {
|
||||
saveNote(childMeta, directoryPath + '/');
|
||||
@@ -335,7 +338,7 @@ ${content}
|
||||
</html>`;
|
||||
const prettyHtml = html.prettyPrint(fullHtml, {indent_size: 2});
|
||||
|
||||
zipFile.addBuffer(prettyHtml, navigationMeta.dataFileName);
|
||||
archive.append(prettyHtml, { name: navigationMeta.dataFileName });
|
||||
}
|
||||
|
||||
function saveIndex(rootMeta, indexMeta) {
|
||||
@@ -367,13 +370,13 @@ ${content}
|
||||
</frameset>
|
||||
</html>`;
|
||||
|
||||
zipFile.addBuffer(fullHtml, indexMeta.dataFileName);
|
||||
archive.append(fullHtml, { name: indexMeta.dataFileName });
|
||||
}
|
||||
|
||||
function saveCss(rootMeta, cssMeta) {
|
||||
const cssContent = fs.readFileSync(RESOURCE_DIR + '/libraries/ckeditor/ckeditor-content.css');
|
||||
|
||||
zipFile.addBuffer(cssContent, cssMeta.dataFileName);
|
||||
archive.append(cssContent, { name: cssMeta.dataFileName });
|
||||
}
|
||||
|
||||
const existingFileNames = format === 'html' ? ['navigation', 'index'] : [];
|
||||
@@ -422,7 +425,7 @@ ${content}
|
||||
|
||||
const metaFileJson = JSON.stringify(metaFile, null, '\t');
|
||||
|
||||
zipFile.addBuffer(metaFileJson, "!!!meta.json");
|
||||
archive.append(metaFileJson, { name: "!!!meta.json" });
|
||||
|
||||
saveNote(rootMeta, '');
|
||||
|
||||
@@ -438,8 +441,8 @@ ${content}
|
||||
res.setHeader('Content-Disposition', utils.getContentDisposition(zipFileName));
|
||||
res.setHeader('Content-Type', 'application/zip');
|
||||
|
||||
zipFile.outputStream.pipe(res);
|
||||
zipFile.end();
|
||||
archive.pipe(res);
|
||||
archive.finalize();
|
||||
|
||||
taskContext.taskSucceeded();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user