Merge branch 'main' into confluence-importer

This commit is contained in:
Philipinho
2026-07-01 23:05:52 +01:00
42 changed files with 1237 additions and 199 deletions
@@ -16,6 +16,7 @@ import {
computeLocalPath,
getExportExtension,
getPageTitle,
getSafePageTitle,
PageExportTree,
replaceInternalLinks,
updateAttachmentUrlsToLocalPaths,
@@ -314,7 +315,7 @@ export class ExportService {
updateAttachmentUrlsToLocalPaths(updatedJsonContent);
}
const pageTitle = getPageTitle(page.title);
const pageTitle = getSafePageTitle(page.title);
const pageExportContent = await this.exportPage(format, {
...page,
content: updatedJsonContent,
+9 -1
View File
@@ -6,6 +6,7 @@ import { validate as isValidUUID } from 'uuid';
import * as path from 'path';
import { Page } from '@docmost/db/types/entity.types';
import { isAttachmentNode } from '../../common/helpers/prosemirror/utils';
import { sanitizeFileName } from '../../common/helpers';
export type PageExportTree = Record<string, Page[]>;
@@ -27,6 +28,13 @@ export function getPageTitle(title: string) {
return title ? title : 'untitled';
}
export function getSafePageTitle(title: string): string {
const sanitized = sanitizeFileName(getPageTitle(title), {
preserveSpaces: true,
});
return sanitized || 'untitled';
}
export function updateAttachmentUrlsToLocalPaths(prosemirrorJson: any) {
const doc = jsonToNode(prosemirrorJson);
if (!doc) return null;
@@ -167,7 +175,7 @@ export function computeLocalPath(
const children = tree[parentPageId] || [];
for (const page of children) {
const title = encodeURIComponent(getPageTitle(page.title));
const title = encodeURIComponent(getSafePageTitle(page.title));
const localPath = `${currentPath}${title}`;
slugIdToPath[page.slugId] = `${localPath}${getExportExtension(format)}`;
@@ -102,8 +102,10 @@ export class ImportService {
throw new BadRequestException(message);
}
const { title, prosemirrorJson } =
this.extractTitleAndRemoveHeading(prosemirrorState);
const { title, prosemirrorJson } = this.extractTitleAndRemoveHeading(
prosemirrorState,
{ anyHeadingLevel: true },
);
const pageTitle = title || fileName;
@@ -246,18 +248,29 @@ export class ImportService {
return null;
}
extractTitleAndRemoveHeading(prosemirrorState: any) {
extractTitleAndRemoveHeading(
prosemirrorState: any,
opts?: { anyHeadingLevel?: boolean },
) {
let title: string | null = null;
const content = prosemirrorState.content ?? [];
const firstNode = content[0];
if (
content.length > 0 &&
content[0].type === 'heading' &&
content[0].attrs?.level === 1
) {
title = content[0].content?.[0]?.text ?? null;
content.shift();
const isTitleHeading =
firstNode?.type === 'heading' &&
(opts?.anyHeadingLevel || firstNode.attrs?.level === 1);
if (isTitleHeading) {
const headingText = (firstNode.content ?? [])
.map((node: any) => node.text ?? '')
.join('')
.trim();
if (headingText) {
title = headingText;
content.shift();
}
}
// ensure at least one paragraph
@@ -32,31 +32,39 @@ export function getFileTaskFolderPath(
}
}
/**
* Extracts a ZIP archive.
*/
const COMPRESSION_HEADROOM = 10;
const MIN_EXTRACTED_BYTES = 256 * 1024 * 1024;
const MAX_ENTRIES = 250_000;
type SizeBudget = { used: number; max: number };
export async function extractZip(
source: string,
target: string,
): Promise<void> {
return extractZipInternal(source, target, true);
const { size: compressedSize } = await fs.promises.stat(source);
const max = Math.max(
compressedSize * COMPRESSION_HEADROOM,
MIN_EXTRACTED_BYTES,
);
return extractZipInternal(source, target, true, { used: 0, max });
}
/**
* Internal helper to extract a ZIP, with optional single-nested-ZIP handling.
* @param source Path to the ZIP file
* @param target Directory to extract into
* @param allowNested Whether to check and unwrap one level of nested ZIP
*/
function extractZipInternal(
source: string,
target: string,
allowNested: boolean,
budget: SizeBudget,
): Promise<void> {
return new Promise((resolve, reject) => {
yauzl.open(
source,
{ lazyEntries: true, decodeStrings: false, autoClose: true },
{
lazyEntries: true,
decodeStrings: false,
autoClose: true,
validateEntrySizes: true,
},
(err, zipfile) => {
if (err) return reject(err);
@@ -74,6 +82,15 @@ function extractZipInternal(
? source.slice(0, -4) + '.inner.zip'
: source + '.inner.zip';
budget.used += entry.uncompressedSize;
if (budget.used > budget.max) {
return reject(
new Error(
'Import archive exceeds the allowed extracted size limit',
),
);
}
zipfile.openReadStream(entry, (openErr, rs) => {
if (openErr) return reject(openErr);
const ws = fs.createWriteStream(nestedPath);
@@ -81,7 +98,7 @@ function extractZipInternal(
ws.on('error', reject);
ws.on('finish', () => {
zipfile.close();
extractZipInternal(nestedPath, target, false)
extractZipInternal(nestedPath, target, false, budget)
.then(() => {
fs.unlinkSync(nestedPath);
resolve();
@@ -92,13 +109,21 @@ function extractZipInternal(
});
} else {
zipfile.close();
extractZipInternal(source, target, false).then(resolve, reject);
extractZipInternal(source, target, false, budget).then(
resolve,
reject,
);
}
});
zipfile.once('error', reject);
return;
}
if (zipfile.entryCount > MAX_ENTRIES) {
zipfile.close();
return reject(new Error('Import archive has too many entries'));
}
// Normal extraction
zipfile.readEntry();
zipfile.on('entry', (entry) => {
@@ -144,6 +169,15 @@ function extractZipInternal(
return;
}
budget.used += entry.uncompressedSize;
if (budget.used > budget.max) {
return reject(
new Error(
'Import archive exceeds the allowed extracted size limit',
),
);
}
// Handle files
try {
fs.mkdirSync(path.dirname(fullPath), { recursive: true });