feat: Add option to not include attachments in exported data (#5463)

This commit is contained in:
Tom Moor
2023-06-20 21:17:39 -04:00
committed by GitHub
parent 0e5a576439
commit eb62b961a4
13 changed files with 106 additions and 23 deletions

View File

@@ -25,12 +25,14 @@ export default abstract class ExportDocumentTreeTask extends ExportTask {
pathInZip,
documentId,
format = FileOperationFormat.MarkdownZip,
includeAttachments,
pathMap,
}: {
zip: JSZip;
pathInZip: string;
documentId: string;
format: FileOperationFormat;
includeAttachments: boolean;
pathMap: Map<string, string>;
}) {
Logger.debug("task", `Adding document to archive`, { documentId });
@@ -44,7 +46,9 @@ export default abstract class ExportDocumentTreeTask extends ExportTask {
? await DocumentHelper.toHTML(document, { centered: true })
: await DocumentHelper.toMarkdown(document);
const attachmentIds = parseAttachmentIds(document.text);
const attachmentIds = includeAttachments
? parseAttachmentIds(document.text)
: [];
const attachments = attachmentIds.length
? await Attachment.findAll({
where: {
@@ -117,13 +121,15 @@ export default abstract class ExportDocumentTreeTask extends ExportTask {
* @param zip The JSZip instance to add files to
* @param collections The collections to export
* @param format The format to export in
* @param includeAttachments Whether to include attachments in the export
*
* @returns The path to the zip file in tmp.
*/
protected async addCollectionsToArchive(
zip: JSZip,
collections: Collection[],
format: FileOperationFormat
format: FileOperationFormat,
includeAttachments = true
) {
const pathMap = this.createPathMap(collections, format);
Logger.debug(
@@ -139,6 +145,7 @@ export default abstract class ExportDocumentTreeTask extends ExportTask {
zip,
pathInZip,
documentId,
includeAttachments,
format,
pathMap,
});

View File

@@ -1,16 +1,17 @@
import JSZip from "jszip";
import { FileOperationFormat } from "@shared/types";
import { Collection } from "@server/models";
import { Collection, FileOperation } from "@server/models";
import ExportDocumentTreeTask from "./ExportDocumentTreeTask";
export default class ExportHTMLZipTask extends ExportDocumentTreeTask {
public async export(collections: Collection[]) {
public async export(collections: Collection[], fileOperation: FileOperation) {
const zip = new JSZip();
return await this.addCollectionsToArchive(
zip,
collections,
FileOperationFormat.HTMLZip
FileOperationFormat.HTMLZip,
fileOperation.includeAttachments
);
}
}

View File

@@ -25,7 +25,11 @@ export default class ExportJSONTask extends ExportTask {
// serial to avoid overloading, slow and steady wins the race
for (const collection of collections) {
await this.addCollectionToArchive(zip, collection);
await this.addCollectionToArchive(
zip,
collection,
fileOperation.includeAttachments
);
}
await this.addMetadataToArchive(zip, fileOperation);
@@ -52,7 +56,11 @@ export default class ExportJSONTask extends ExportTask {
);
}
private async addCollectionToArchive(zip: JSZip, collection: Collection) {
private async addCollectionToArchive(
zip: JSZip,
collection: Collection,
includeAttachments: boolean
) {
const output: CollectionJSONExport = {
collection: {
...omit(presentCollection(collection), ["url"]),
@@ -75,12 +83,14 @@ export default class ExportJSONTask extends ExportTask {
continue;
}
const attachments = await Attachment.findAll({
where: {
teamId: document.teamId,
id: parseAttachmentIds(document.text),
},
});
const attachments = includeAttachments
? await Attachment.findAll({
where: {
teamId: document.teamId,
id: parseAttachmentIds(document.text),
},
})
: [];
await Promise.all(
attachments.map(async (attachment) => {

View File

@@ -1,16 +1,17 @@
import JSZip from "jszip";
import { FileOperationFormat } from "@shared/types";
import { Collection } from "@server/models";
import { Collection, FileOperation } from "@server/models";
import ExportDocumentTreeTask from "./ExportDocumentTreeTask";
export default class ExportMarkdownZipTask extends ExportDocumentTreeTask {
public async export(collections: Collection[]) {
public async export(collections: Collection[], fileOperation: FileOperation) {
const zip = new JSZip();
return await this.addCollectionsToArchive(
zip,
collections,
FileOperationFormat.MarkdownZip
FileOperationFormat.MarkdownZip,
fileOperation.includeAttachments
);
}
}

View File

@@ -41,7 +41,9 @@ export default abstract class ExportTask extends BaseTask<Props> {
});
try {
Logger.info("task", `ExportTask processing data for ${fileOperationId}`);
Logger.info("task", `ExportTask processing data for ${fileOperationId}`, {
includeAttachments: fileOperation.includeAttachments,
});
await this.updateFileOperation(fileOperation, {
state: FileOperationState.Creating,