Notes/src/services/export/single.ts

131 lines
4.1 KiB
TypeScript
Raw Normal View History

2018-11-24 20:58:38 +01:00
"use strict";
2024-02-19 22:12:00 +02:00
import mimeTypes = require('mime-types');
import html = require('html');
import utils = require('../utils');
import mdService = require('./md');
import becca = require('../../becca/becca');
import TaskContext = require('../task_context');
import BBranch = require('../../becca/entities/bbranch');
import { Response } from 'express';
function exportSingleNote(taskContext: TaskContext, branch: BBranch, format: "html" | "markdown", res: Response) {
2020-06-20 12:31:38 +02:00
const note = branch.getNote();
2018-11-24 20:58:38 +01:00
if (note.type === 'image' || note.type === 'file') {
return [400, `Note type '${note.type}' cannot be exported as single file.`];
2018-11-24 20:58:38 +01:00
}
if (format !== 'html' && format !== 'markdown') {
return [400, `Unrecognized format '${format}'`];
2018-11-24 20:58:38 +01:00
}
let payload, extension, mime;
2020-06-20 12:31:38 +02:00
let content = note.getContent();
2024-02-19 22:12:00 +02:00
if (typeof content !== "string") {
2024-04-11 19:49:01 +03:00
throw new Error("Unsupported content type for export.");
2024-02-19 22:12:00 +02:00
}
2019-02-06 21:29:23 +01:00
2018-11-24 20:58:38 +01:00
if (note.type === 'text') {
if (format === 'html') {
content = inlineAttachments(content);
if (!content.toLowerCase().includes("<html")) {
content = `<html><head><meta charset="utf-8"></head><body>${content}</body></html>`;
}
payload = content.length < 100_000
? html.prettyPrint(content, {indent_size: 2})
: content;
2018-11-24 20:58:38 +01:00
extension = 'html';
mime = 'text/html';
}
else if (format === 'markdown') {
payload = mdService.toMarkdown(content);
2018-11-24 20:58:38 +01:00
extension = 'md';
2019-02-25 21:57:11 +01:00
mime = 'text/x-markdown'
2018-11-24 20:58:38 +01:00
}
}
else if (note.type === 'code') {
payload = content;
2018-11-24 20:58:38 +01:00
extension = mimeTypes.extension(note.mime) || 'code';
mime = note.mime;
}
2022-12-06 23:01:42 +01:00
else if (note.type === 'relationMap' || note.type === 'canvas' || note.type === 'search') {
payload = content;
2018-11-24 20:58:38 +01:00
extension = 'json';
mime = 'application/json';
}
const fileName = `${note.title}.${extension}`;
2018-11-24 20:58:38 +01:00
res.setHeader('Content-Disposition', utils.getContentDisposition(fileName));
res.setHeader('Content-Type', `${mime}; charset=UTF-8`);
2018-11-24 20:58:38 +01:00
res.send(payload);
2019-02-10 22:30:55 +01:00
2019-10-18 22:27:38 +02:00
taskContext.increaseProgressCount();
taskContext.taskSucceeded();
2018-11-24 20:58:38 +01:00
}
2024-02-19 22:12:00 +02:00
function inlineAttachments(content: string) {
2023-07-13 23:54:47 +02:00
content = content.replace(/src="[^"]*api\/images\/([a-zA-Z0-9_]+)\/?[^"]+"/g, (match, noteId) => {
const note = becca.getNote(noteId);
if (!note || !note.mime.startsWith('image/')) {
return match;
}
const imageContent = note.getContent();
if (!Buffer.isBuffer(imageContent)) {
return match;
}
const base64Content = imageContent.toString('base64');
const srcValue = `data:${note.mime};base64,${base64Content}`;
return `src="${srcValue}"`;
});
content = content.replace(/src="[^"]*api\/attachments\/([a-zA-Z0-9_]+)\/image\/?[^"]+"/g, (match, attachmentId) => {
const attachment = becca.getAttachment(attachmentId);
if (!attachment || !attachment.mime.startsWith('image/')) {
return match;
}
const attachmentContent = attachment.getContent();
if (!Buffer.isBuffer(attachmentContent)) {
return match;
}
const base64Content = attachmentContent.toString('base64');
const srcValue = `data:${attachment.mime};base64,${base64Content}`;
return `src="${srcValue}"`;
});
content = content.replace(/href="[^"]*#root[^"]*attachmentId=([a-zA-Z0-9_]+)\/?"/g, (match, attachmentId) => {
const attachment = becca.getAttachment(attachmentId);
if (!attachment) {
return match;
}
const attachmentContent = attachment.getContent();
if (!Buffer.isBuffer(attachmentContent)) {
return match;
}
const base64Content = attachmentContent.toString('base64');
const hrefValue = `data:${attachment.mime};base64,${base64Content}`;
return `href="${hrefValue}" download="${utils.escapeHtml(attachment.title)}"`;
});
return content;
}
2024-02-19 23:08:43 +02:00
export = {
2018-11-24 20:58:38 +01:00
exportSingleNote
2020-06-20 12:31:38 +02:00
};