Files
outline/server/queues/tasks/ExportJSONTask.ts
T
Tom Moor 2c84b2100f fix: Stream attachments in document exports and stop on disconnect (#14697)
* fix: Stream attachments in single document exports and stop on disconnect

Single document and revision exports read every attachment into memory
before building the zip, and kept running after the client had gone.
Repeated exports of a document with large attachments ran web dynos out
of memory.

- Stream attachments into the zip with a shared AttachmentHelper.addToArchive
- Abort export work when the client connection closes
- Read only images that will be inlined, with a 1MB total inline limit

* fix: Destroy streams opened after abort, treat missing local files as absent
2026-11-24 19:51:15 -05:00

202 lines
6.0 KiB
TypeScript

import type { ZipFile } from "yazl";
import { omit } from "es-toolkit/compat";
import type { NavigationNode } from "@shared/types";
import env from "@server/env";
import type { Collection, FileOperation } from "@server/models";
import { Attachment, Document } from "@server/models";
import AttachmentHelper from "@server/models/helpers/AttachmentHelper";
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper";
import { presentAttachment, presentCollection } from "@server/presenters";
import { sequelizeReadOnly } from "@server/storage/database";
import type { CollectionJSONExport, JSONExportMetadata } from "@server/types";
import ZipHelper from "@server/utils/ZipHelper";
import { serializeFilename } from "@server/utils/fs";
import packageJson from "../../../package.json";
import ExportTask from "./ExportTask";
export default class ExportJSONTask extends ExportTask {
public async exportCollections(
collections: Collection[],
fileOperation: FileOperation
) {
const usedFilenames = new Set<string>();
return ZipHelper.toTmpFile(async (zip) => {
// serial to avoid overloading, slow and steady wins the race
for (const collection of collections) {
let filename = serializeFilename(collection.name);
let i = 0;
while (usedFilenames.has(filename)) {
filename = `${serializeFilename(collection.name)} (${++i})`;
}
usedFilenames.add(filename);
await this.addCollectionToArchive(
zip,
collection,
fileOperation.options?.includeAttachments ?? true,
filename
);
}
await this.addMetadataToArchive(zip, fileOperation);
});
}
private async addMetadataToArchive(
zip: ZipFile,
fileOperation: FileOperation
) {
const user = await fileOperation.$get("user");
const metadata: JSONExportMetadata = {
exportVersion: 1,
version: packageJson.version,
createdAt: new Date().toISOString(),
createdById: fileOperation.userId,
createdByEmail: user?.email ?? null,
};
zip.addBuffer(
Buffer.from(
env.isDevelopment
? JSON.stringify(metadata, null, 2)
: JSON.stringify(metadata)
),
`metadata.json`
);
}
private async addCollectionToArchive(
zip: ZipFile,
collection: Collection,
includeAttachments: boolean,
filename: string
) {
const output: CollectionJSONExport = {
collection: {
...(omit(await presentCollection(undefined, collection), [
"url",
"description",
]) as CollectionJSONExport["collection"]),
documentStructure: collection.documentStructure,
},
documents: {},
attachments: {},
};
const addAttachments = (attachments: Attachment[]) => {
for (const attachment of attachments) {
AttachmentHelper.addToArchive(zip, attachment, attachment.key);
output.attachments[attachment.id] = {
...omit(presentAttachment(attachment), "url"),
key: attachment.key,
};
}
};
const addDocumentTree = async (nodes: NavigationNode[]) => {
for (const node of nodes) {
const result = await sequelizeReadOnly.transaction(
async (transaction) => {
const document = await Document.findByPk(node.id, {
includeState: true,
transaction,
});
if (!document) {
return;
}
const attachmentIds = includeAttachments
? ProsemirrorHelper.parseAttachmentIds(
DocumentHelper.toProsemirror(document)
)
: [];
const attachments = attachmentIds.length
? await Attachment.findAll({
where: {
teamId: document.teamId,
id: attachmentIds,
},
transaction,
})
: [];
return { attachments, document };
}
);
if (!result) {
continue;
}
const { attachments, document } = result;
addAttachments(attachments);
output.documents[document.id] = {
id: document.id,
urlId: document.urlId,
title: document.title,
icon: document.icon,
color: document.color,
data: await DocumentHelper.toJSON(document),
createdById: document.createdById,
createdByName: document.createdBy.name,
createdByEmail: document.createdBy.email,
createdAt: document.createdAt.toISOString(),
updatedAt: document.updatedAt.toISOString(),
publishedAt: document.publishedAt
? document.publishedAt.toISOString()
: null,
fullWidth: document.fullWidth,
parentDocumentId: document.parentDocumentId,
};
if (node.children?.length > 0) {
await addDocumentTree(node.children);
}
}
};
const collectionAttachmentIds = includeAttachments
? ProsemirrorHelper.parseAttachmentIds(
DocumentHelper.toProsemirror(collection)
)
: [];
const collectionAttachments = collectionAttachmentIds.length
? await sequelizeReadOnly.transaction((transaction) =>
Attachment.findAll({
where: {
teamId: collection.teamId,
id: collectionAttachmentIds,
},
transaction,
})
)
: [];
addAttachments(collectionAttachments);
if (collection.documentStructure) {
await addDocumentTree(collection.documentStructure);
}
zip.addBuffer(
Buffer.from(
env.isDevelopment
? JSON.stringify(output, null, 2)
: JSON.stringify(output)
),
`${filename}.json`
);
}
public async exportDocument(): Promise<string> {
throw new Error("JSON export unsupported for individual document.");
}
}