From 7286aa28e189ba4de903b404a017ab0c5ee3d2cb Mon Sep 17 00:00:00 2001 From: Jaiwanth Date: Wed, 26 May 2021 10:28:20 +0530 Subject: [PATCH] Handle audio and video files --- src/HtmlUtils.tsx | 2 +- src/utils/exportUtils/HtmlExport.ts | 49 ++++++++++++++++++++++++---- src/utils/exportUtils/StreamToZip.ts | 17 ++++++++++ 3 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 src/utils/exportUtils/StreamToZip.ts diff --git a/src/HtmlUtils.tsx b/src/HtmlUtils.tsx index ef5ac383e3..1d5defe45d 100644 --- a/src/HtmlUtils.tsx +++ b/src/HtmlUtils.tsx @@ -233,7 +233,7 @@ const transformTags: IExtendedSanitizeOptions["transformTags"] = { // custom to }, }; -const sanitizeHtmlParams: IExtendedSanitizeOptions = { +export const sanitizeHtmlParams: IExtendedSanitizeOptions = { allowedTags: [ 'font', // custom to matrix for IRC-style font coloring 'del', // for markdown diff --git a/src/utils/exportUtils/HtmlExport.ts b/src/utils/exportUtils/HtmlExport.ts index c21f227c35..8e6070cff6 100644 --- a/src/utils/exportUtils/HtmlExport.ts +++ b/src/utils/exportUtils/HtmlExport.ts @@ -8,6 +8,8 @@ import { MatrixEvent } from "matrix-js-sdk/src/models/event"; import { getUserNameColorClass } from "../FormattingUtils"; import { Exporter } from "./Exporter"; import * as ponyfill from "web-streams-polyfill/ponyfill" +import { sanitizeHtmlParams } from "../../HtmlUtils"; +import sanitizeHtml from "sanitize-html"; const css = ` body { @@ -356,7 +358,7 @@ export default class HTMLExporter extends Exporter { } } - protected async getImageData(event: MatrixEvent) { + protected async getMediaBlob(event: MatrixEvent) { let blob: Blob; try { const isEncrypted = event.isEncrypted(); @@ -369,7 +371,7 @@ export default class HTMLExporter extends Exporter { blob = await image.blob(); } } catch (err) { - console.log("Error decrypting image"); + console.log("Error decrypting media"); } return blob; } @@ -405,21 +407,54 @@ export default class HTMLExporter extends Exporter { let messageBody = ""; switch (event.getContent().msgtype) { case "m.text": - messageBody = `
${event.getContent().body}
`; + messageBody = ` +
+ ${sanitizeHtml(event.getContent().body, sanitizeHtmlParams)} +
`; break; case "m.image": { + const blob = await this.getMediaBlob(event); + const fileName = `${event.getId()}.${blob.type.replace("image/", "")}`; messageBody = ` `; - const blob = await this.getImageData(event); - this.zip.file(`images/${event.getId()}.png`, blob); - } + this.zip.file(`images/${fileName}`, blob); break; + } + case "m.video": { + const blob = await this.getMediaBlob(event); + const fileName = `${event.getId()}.${blob.type.replace("video/", "")}`; + messageBody = ` +
+
`; + this.zip.file(`videos/${fileName}`, blob); + break; + } + case "m.audio": { + const blob = await this.getMediaBlob(event); + const fileName = `${event.getId()}.${blob.type.replace("audio/", "")}`; + messageBody = ` +
+
`; + this.zip.file(`audio/${fileName}`, blob); + break; + } default: break; } diff --git a/src/utils/exportUtils/StreamToZip.ts b/src/utils/exportUtils/StreamToZip.ts new file mode 100644 index 0000000000..769fa91dba --- /dev/null +++ b/src/utils/exportUtils/StreamToZip.ts @@ -0,0 +1,17 @@ +/*Not to be reviewed now*/ +class fileCheckSum { + protected CRC32: number; + public table: any[]; + constructor() { + this.CRC32 = -1 + } + + protected append(data: any[]) { + let crc = this.CRC32 | 0; + const table = this.table; + for (let offset = 0, len = data.length | 0; offset < len; offset++) { + crc = (crc >>> 8) ^ table[(crc ^ data[offset]) & 0xFF] + } + this.CRC32 = crc + } +}