mirror of
https://github.com/outline/outline.git
synced 2026-11-21 05:24:03 +03:00
* shares.info, collections.info, documents.info * shares.list, shares.create, shares.update * shares.sitemap * parity with existing document shared screen * collection share popover * parent share and table * collection scene * collection link in sidebar * sidebar and breadcrumb collection link click * collection link click in editor * meta * more meta + 404 page * map internal link, remove showLastUpdated option * fix shares.list pagination * show last updated * shareLoader tests * lint * sidebar context for collection link * badge in shares table * fix existing tests * tsc * update failing test snapshot * env * signed url for collection attachments * include collection content in SSR for screen readers * search * drafts can be shared * review * tsc, remove old shared-doc scene * tweaks * DRY * refactor loader * Remove share/collection urls * fix: Collection overview should not be editable when viewing shared link and logged in * Tweak public breadcrumb * fix: Deleted documents should never be exposed through share * empty sharedTree array where includeChildDocuments is false * revert includeChildDocs guard for logical correctness + SSR bug fix * fix: check document is part of share --------- Co-authored-by: Tom Moor <tom@getoutline.com>
161 lines
4.8 KiB
TypeScript
161 lines
4.8 KiB
TypeScript
import JSZip from "jszip";
|
|
import omit from "lodash/omit";
|
|
import { NavigationNode } from "@shared/types";
|
|
import env from "@server/env";
|
|
import Logger from "@server/logging/Logger";
|
|
import {
|
|
Attachment,
|
|
Collection,
|
|
Document,
|
|
FileOperation,
|
|
} from "@server/models";
|
|
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
|
|
import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper";
|
|
import { presentAttachment, presentCollection } from "@server/presenters";
|
|
import { 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 export(collections: Collection[], fileOperation: FileOperation) {
|
|
const zip = new JSZip();
|
|
|
|
// serial to avoid overloading, slow and steady wins the race
|
|
for (const collection of collections) {
|
|
await this.addCollectionToArchive(
|
|
zip,
|
|
collection,
|
|
fileOperation.options?.includeAttachments ?? true
|
|
);
|
|
}
|
|
|
|
await this.addMetadataToArchive(zip, fileOperation);
|
|
|
|
return ZipHelper.toTmpFile(zip);
|
|
}
|
|
|
|
private async addMetadataToArchive(zip: JSZip, 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.file(
|
|
`metadata.json`,
|
|
env.isDevelopment
|
|
? JSON.stringify(metadata, null, 2)
|
|
: JSON.stringify(metadata)
|
|
);
|
|
}
|
|
|
|
private async addCollectionToArchive(
|
|
zip: JSZip,
|
|
collection: Collection,
|
|
includeAttachments: boolean
|
|
) {
|
|
const output: CollectionJSONExport = {
|
|
collection: {
|
|
...(omit(await presentCollection(undefined, collection), [
|
|
"url",
|
|
"description",
|
|
]) as CollectionJSONExport["collection"]),
|
|
documentStructure: collection.documentStructure,
|
|
},
|
|
documents: {},
|
|
attachments: {},
|
|
};
|
|
|
|
async function addDocumentTree(nodes: NavigationNode[]) {
|
|
for (const node of nodes) {
|
|
const document = await Document.findByPk(node.id, {
|
|
includeState: true,
|
|
});
|
|
|
|
if (!document) {
|
|
continue;
|
|
}
|
|
|
|
const attachments = includeAttachments
|
|
? await Attachment.findAll({
|
|
where: {
|
|
teamId: document.teamId,
|
|
id: ProsemirrorHelper.parseAttachmentIds(
|
|
DocumentHelper.toProsemirror(document)
|
|
),
|
|
},
|
|
})
|
|
: [];
|
|
|
|
await Promise.all(
|
|
attachments.map(async (attachment) => {
|
|
zip.file(
|
|
attachment.key,
|
|
new Promise<Buffer>((resolve) => {
|
|
attachment.buffer.then(resolve).catch((err) => {
|
|
Logger.warn(`Failed to read attachment from storage`, {
|
|
attachmentId: attachment.id,
|
|
teamId: attachment.teamId,
|
|
error: err.message,
|
|
});
|
|
resolve(Buffer.from(""));
|
|
});
|
|
}),
|
|
{
|
|
date: attachment.updatedAt,
|
|
createFolders: true,
|
|
}
|
|
);
|
|
|
|
output.attachments[attachment.id] = {
|
|
...omit(presentAttachment(attachment), "url"),
|
|
key: attachment.key,
|
|
};
|
|
})
|
|
);
|
|
|
|
output.documents[document.id] = {
|
|
id: document.id,
|
|
urlId: document.urlId,
|
|
title: document.title,
|
|
icon: document.icon,
|
|
color: document.color,
|
|
data: DocumentHelper.toProsemirror(document).toJSON(),
|
|
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,
|
|
template: document.template,
|
|
parentDocumentId: document.parentDocumentId,
|
|
};
|
|
|
|
if (node.children?.length > 0) {
|
|
await addDocumentTree(node.children);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (collection.documentStructure) {
|
|
await addDocumentTree(collection.documentStructure);
|
|
}
|
|
|
|
zip.file(
|
|
`${serializeFilename(collection.name)}.json`,
|
|
env.isDevelopment
|
|
? JSON.stringify(output, null, 2)
|
|
: JSON.stringify(output)
|
|
);
|
|
}
|
|
}
|