mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
tar export can now solve naming conflict
This commit is contained in:
parent
62dbd4062a
commit
fcf3fe8dcd
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
const html = require('html');
|
const html = require('html');
|
||||||
const tar = require('tar-stream');
|
const tar = require('tar-stream');
|
||||||
|
const path = require('path');
|
||||||
const sanitize = require("sanitize-filename");
|
const sanitize = require("sanitize-filename");
|
||||||
const mimeTypes = require('mime-types');
|
const mimeTypes = require('mime-types');
|
||||||
const TurndownService = require('turndown');
|
const TurndownService = require('turndown');
|
||||||
@ -21,12 +22,31 @@ async function exportToTar(branch, format, res) {
|
|||||||
const name = await exportNoteInner(branch, '');
|
const name = await exportNoteInner(branch, '');
|
||||||
|
|
||||||
function getUniqueFilename(fileName) {
|
function getUniqueFilename(fileName) {
|
||||||
|
const lcFileName = fileName.toLowerCase();
|
||||||
|
|
||||||
|
if (lcFileName in existingPaths) {
|
||||||
|
let index;
|
||||||
|
let newName;
|
||||||
|
|
||||||
|
do {
|
||||||
|
index = existingPaths[lcFileName]++;
|
||||||
|
|
||||||
|
newName = lcFileName + "_" + index;
|
||||||
|
}
|
||||||
|
while (newName in existingPaths);
|
||||||
|
|
||||||
|
return fileName + "_" + index;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
existingPaths[lcFileName] = 1;
|
||||||
|
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function exportNoteInner(branch, directory, existingNames) {
|
async function exportNoteInner(branch, directory, existingNames) {
|
||||||
const note = await branch.getNote();
|
const note = await branch.getNote();
|
||||||
const baseFileName = directory + sanitize(note.title);
|
const baseFileName = getUniqueFilename(directory + sanitize(note.title));
|
||||||
|
|
||||||
if (exportedNoteIds.includes(note.noteId)) {
|
if (exportedNoteIds.includes(note.noteId)) {
|
||||||
saveMetadataFile(baseFileName, {
|
saveMetadataFile(baseFileName, {
|
||||||
@ -74,8 +94,9 @@ async function exportToTar(branch, format, res) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
metadata.dataFilename = saveDataFile(baseFileName, note);
|
||||||
|
|
||||||
saveMetadataFile(baseFileName, metadata);
|
saveMetadataFile(baseFileName, metadata);
|
||||||
saveDataFile(baseFileName, note);
|
|
||||||
|
|
||||||
exportedNoteIds.push(note.noteId);
|
exportedNoteIds.push(note.noteId);
|
||||||
|
|
||||||
@ -92,7 +113,7 @@ async function exportToTar(branch, format, res) {
|
|||||||
return baseFileName;
|
return baseFileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveDataFile(childFileName, note) {
|
function saveDataFile(baseFilename, note) {
|
||||||
let content = note.content;
|
let content = note.content;
|
||||||
let extension;
|
let extension;
|
||||||
|
|
||||||
@ -115,11 +136,17 @@ async function exportToTar(branch, format, res) {
|
|||||||
|| "dat";
|
|| "dat";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!childFileName.toLowerCase().endsWith(extension)) {
|
let filename = baseFilename;
|
||||||
childFileName += "." + extension;
|
|
||||||
|
if (!filename.toLowerCase().endsWith(extension)) {
|
||||||
|
filename += "." + extension;
|
||||||
}
|
}
|
||||||
|
|
||||||
pack.entry({name: childFileName, size: content.length}, content);
|
filename = getUniqueFilename(filename);
|
||||||
|
|
||||||
|
pack.entry({name: filename, size: content.length}, content);
|
||||||
|
|
||||||
|
return path.basename(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getExceptionalExtension(mime) {
|
function getExceptionalExtension(mime) {
|
||||||
@ -131,7 +158,9 @@ async function exportToTar(branch, format, res) {
|
|||||||
function saveMetadataFile(baseFileName, metadata) {
|
function saveMetadataFile(baseFileName, metadata) {
|
||||||
const metadataJson = JSON.stringify(metadata, null, '\t');
|
const metadataJson = JSON.stringify(metadata, null, '\t');
|
||||||
|
|
||||||
pack.entry({name: getUniqueFilename(baseFileName + ".meta"), size: metadataJson.length}, metadataJson);
|
const fileName = getUniqueFilename(baseFileName + ".meta");
|
||||||
|
|
||||||
|
pack.entry({name: fileName, size: metadataJson.length}, metadataJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveDirectory(baseFileName) {
|
function saveDirectory(baseFileName) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user