mirror of
https://github.com/outline/outline.git
synced 2026-11-21 05:24:03 +03:00
205 lines
6.0 KiB
TypeScript
205 lines
6.0 KiB
TypeScript
import type { ZipFile } from "yazl";
|
|
import { omit } from "es-toolkit/compat";
|
|
import { NavigationNodeType, type NavigationNode } from "@shared/types";
|
|
import env from "@server/env";
|
|
import type { Collection, FileOperation } from "@server/models";
|
|
import { Attachment, Document } from "@server/models";
|
|
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) {
|
|
this.addAttachmentToArchive(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) {
|
|
if (node.type === NavigationNodeType.Link) {
|
|
continue;
|
|
}
|
|
|
|
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.");
|
|
}
|
|
}
|