From bd75849e73076aff42b8af38f866df5f2907cefa Mon Sep 17 00:00:00 2001 From: Jaiwanth Date: Tue, 15 Jun 2021 16:41:31 +0530 Subject: [PATCH] Enable option to set maximum file size --- src/components/views/rooms/RoomHeader.js | 6 +++++- src/i18n/strings/en_EN.json | 2 ++ src/utils/exportUtils/HtmlExport.tsx | 14 ++++++++++++-- src/utils/exportUtils/exportUtils.ts | 1 + 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/components/views/rooms/RoomHeader.js b/src/components/views/rooms/RoomHeader.js index 3372785390..f0f9b4d646 100644 --- a/src/components/views/rooms/RoomHeader.js +++ b/src/components/views/rooms/RoomHeader.js @@ -86,7 +86,11 @@ export default class RoomHeader extends React.Component { this.props.room, exportFormats.HTML, exportTypes.START_DATE, - { startDate: parseInt(new Date("2021.05.20").getTime().toFixed(0)), attachmentsIncluded: false }, + { + startDate: parseInt(new Date("2021.05.20").getTime().toFixed(0)), + attachmentsIncluded: true, + maxSize: 3 * 1024 * 1024, // 3 MB + }, ); } diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 8640735557..8748c5ba80 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -727,6 +727,8 @@ "Invite to %(spaceName)s": "Invite to %(spaceName)s", "Share your public space": "Share your public space", "Unknown App": "Unknown App", + "Media omitted": "Media omitted", + "Media omitted - file size limit exceeded": "Media omitted - file size limit exceeded", "%(creatorName)s created this room.": "%(creatorName)s created this room.", "This is the start of export of . Exported by at %(exportDate)s.": "This is the start of export of . Exported by at %(exportDate)s.", "Topic: %(topic)s": "Topic: %(topic)s", diff --git a/src/utils/exportUtils/HtmlExport.tsx b/src/utils/exportUtils/HtmlExport.tsx index eed1a818e4..c1b2f4ea70 100644 --- a/src/utils/exportUtils/HtmlExport.tsx +++ b/src/utils/exportUtils/HtmlExport.tsx @@ -31,6 +31,8 @@ export default class HTMLExporter extends Exporter { protected avatars: Map; protected permalinkCreator: RoomPermalinkCreator; protected matrixClient: MatrixClient; + protected totalSize: number; + protected mediaOmitText: string; constructor(room: Room, exportType: exportTypes, exportOptions: exportOptions) { super(room, exportType, exportOptions); @@ -38,6 +40,10 @@ export default class HTMLExporter extends Exporter { this.avatars = new Map(); this.matrixClient = MatrixClientPeg.get(); this.permalinkCreator = new RoomPermalinkCreator(this.room); + this.totalSize = 0; + this.mediaOmitText = !this.exportOptions.attachmentsIncluded + ? _t("Media omitted") + : _t("Media omitted - file size limit exceeded"); window.addEventListener("beforeunload", this.onBeforeUnload) } @@ -270,15 +276,19 @@ export default class HTMLExporter extends Exporter { if (this.isAttachment(mxEv)) { if (this.exportOptions.attachmentsIncluded) { const blob = await this.getMediaBlob(mxEv); + this.totalSize += blob.size; const filePath = this.getFilePath(mxEv); eventTile = await this.getEventTile(mxEv, joined, filePath); + if (this.totalSize > this.exportOptions.maxSize - 1024 * 1024) { + this.exportOptions.attachmentsIncluded = false; + } this.zip.file(filePath, blob); } else { const modifiedContent = { msgtype: "m.text", - body: "**Media omitted**", + body: `**${this.mediaOmitText}**`, format: "org.matrix.custom.html", - formatted_body: "Media omitted", + formatted_body: `${this.mediaOmitText}`, } if (mxEv.isEncrypted()) { mxEv._clearEvent.content = modifiedContent; diff --git a/src/utils/exportUtils/exportUtils.ts b/src/utils/exportUtils/exportUtils.ts index 407cef3077..77fb23509e 100644 --- a/src/utils/exportUtils/exportUtils.ts +++ b/src/utils/exportUtils/exportUtils.ts @@ -18,6 +18,7 @@ export interface exportOptions { startDate?: number; numberOfMessages?: number; attachmentsIncluded: boolean; + maxSize: number; } const exportConversationalHistory = async (