mirror of
https://github.com/element-hq/element-web.git
synced 2024-12-15 03:01:29 +03:00
Move generic functions into base class
This commit is contained in:
parent
ab653d9952
commit
30c7017fad
2 changed files with 58 additions and 56 deletions
|
@ -3,6 +3,9 @@ import { Room } from "matrix-js-sdk/src/models/room";
|
|||
import { MatrixClientPeg } from "../../MatrixClientPeg";
|
||||
import { exportTypes } from "./exportUtils";
|
||||
import { exportOptions } from "./exportUtils";
|
||||
import { decryptFile } from "../DecryptFile";
|
||||
import { mediaFromContent } from "../../customisations/Media";
|
||||
import { formatFullDateNoDay } from "../../DateUtils";
|
||||
|
||||
export default abstract class Exporter {
|
||||
protected constructor(
|
||||
|
@ -89,5 +92,59 @@ export default abstract class Exporter {
|
|||
return events;
|
||||
}
|
||||
|
||||
protected async getMediaBlob(event: MatrixEvent) {
|
||||
let blob: Blob;
|
||||
try {
|
||||
const isEncrypted = event.isEncrypted();
|
||||
const content = event.getContent();
|
||||
const shouldDecrypt = isEncrypted && !content.hasOwnProperty("org.matrix.msc1767.file")
|
||||
&& event.getType() !== "m.sticker";
|
||||
if (shouldDecrypt) {
|
||||
blob = await decryptFile(content.file);
|
||||
} else {
|
||||
const media = mediaFromContent(event.getContent());
|
||||
const image = await fetch(media.srcHttp);
|
||||
blob = await image.blob();
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("Error decrypting media");
|
||||
}
|
||||
return blob;
|
||||
}
|
||||
|
||||
protected splitFileName(file: string) {
|
||||
const lastDot = file.lastIndexOf('.');
|
||||
if (lastDot === -1) return [file, ""];
|
||||
const fileName = file.slice(0, lastDot);
|
||||
const ext = file.slice(lastDot + 1);
|
||||
return [fileName, '.' + ext];
|
||||
}
|
||||
|
||||
protected getFilePath(event: MatrixEvent) {
|
||||
const mediaType = event.getContent().msgtype;
|
||||
let fileDirectory: string;
|
||||
switch (mediaType) {
|
||||
case "m.image":
|
||||
fileDirectory = "images";
|
||||
break;
|
||||
case "m.video":
|
||||
fileDirectory = "videos";
|
||||
break;
|
||||
case "m.audio":
|
||||
fileDirectory = "audio";
|
||||
break;
|
||||
default:
|
||||
fileDirectory = event.getType() === "m.sticker" ? "stickers" : "files";
|
||||
}
|
||||
const fileDate = formatFullDateNoDay(new Date(event.getTs()));
|
||||
const [fileName, fileExt] = this.splitFileName(event.getContent().body);
|
||||
return fileDirectory + "/" + fileName + '-' + fileDate + fileExt;
|
||||
}
|
||||
|
||||
protected isAttachment(mxEv: MatrixEvent) {
|
||||
const attachmentTypes = ["m.sticker", "m.image", "m.file", "m.video", "m.audio"];
|
||||
return mxEv.getType() === attachmentTypes[0] || attachmentTypes.includes(mxEv.getContent().msgtype);
|
||||
}
|
||||
|
||||
abstract export(): Promise<Blob>;
|
||||
}
|
||||
|
|
|
@ -2,8 +2,7 @@ import React from "react"
|
|||
import streamSaver from "streamsaver";
|
||||
import JSZip from "jszip";
|
||||
import Exporter from "./Exporter";
|
||||
import { decryptFile } from "../DecryptFile";
|
||||
import { mediaFromContent, mediaFromMxc } from "../../customisations/Media";
|
||||
import { mediaFromMxc } from "../../customisations/Media";
|
||||
import { Room } from "matrix-js-sdk/src/models/room";
|
||||
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
||||
import { renderToStaticMarkup } from 'react-dom/server'
|
||||
|
@ -216,26 +215,6 @@ export default class HTMLExporter extends Exporter {
|
|||
}
|
||||
}
|
||||
|
||||
protected async getMediaBlob(event: MatrixEvent) {
|
||||
let blob: Blob;
|
||||
try {
|
||||
const isEncrypted = event.isEncrypted();
|
||||
const content = event.getContent();
|
||||
const shouldDecrypt = isEncrypted && !content.hasOwnProperty("org.matrix.msc1767.file")
|
||||
&& event.getType() !== "m.sticker";
|
||||
if (shouldDecrypt) {
|
||||
blob = await decryptFile(content.file);
|
||||
} else {
|
||||
const media = mediaFromContent(event.getContent());
|
||||
const image = await fetch(media.srcHttp);
|
||||
blob = await image.blob();
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("Error decrypting media");
|
||||
}
|
||||
return blob;
|
||||
}
|
||||
|
||||
protected getDateSeparator(event: MatrixEvent) {
|
||||
const ts = event.getTs();
|
||||
const dateSeparator = <li key={ts}><DateSeparator forExport={true} key={ts} ts={ts} /></li>;
|
||||
|
@ -247,35 +226,6 @@ export default class HTMLExporter extends Exporter {
|
|||
return wantsDateSeparator(prevEvent.getDate(), event.getDate());
|
||||
}
|
||||
|
||||
protected splitFileName(file: string) {
|
||||
const lastDot = file.lastIndexOf('.');
|
||||
if (lastDot === -1) return [file, ""];
|
||||
const fileName = file.slice(0, lastDot);
|
||||
const ext = file.slice(lastDot + 1);
|
||||
return [fileName, '.' + ext];
|
||||
}
|
||||
|
||||
protected getFilePath(event: MatrixEvent) {
|
||||
const mediaType = event.getContent().msgtype;
|
||||
let fileDirectory: string;
|
||||
switch (mediaType) {
|
||||
case "m.image":
|
||||
fileDirectory = "images";
|
||||
break;
|
||||
case "m.video":
|
||||
fileDirectory = "videos";
|
||||
break;
|
||||
case "m.audio":
|
||||
fileDirectory = "audio";
|
||||
break;
|
||||
default:
|
||||
fileDirectory = event.getType() === "m.sticker" ? "stickers" : "files";
|
||||
}
|
||||
const fileDate = formatFullDateNoDay(new Date(event.getTs()));
|
||||
const [fileName, fileExt] = this.splitFileName(event.getContent().body);
|
||||
return fileDirectory + "/" + fileName + '-' + fileDate + fileExt;
|
||||
}
|
||||
|
||||
protected async getEventTile(mxEv: MatrixEvent, continuation: boolean, filePath?: string) {
|
||||
const hasAvatar = this.hasAvatar(mxEv);
|
||||
if (hasAvatar) await this.saveAvatarIfNeeded(mxEv);
|
||||
|
@ -314,11 +264,6 @@ export default class HTMLExporter extends Exporter {
|
|||
return eventTileMarkup;
|
||||
}
|
||||
|
||||
protected isAttachment(mxEv: MatrixEvent) {
|
||||
const attachmentTypes = ["m.sticker", "m.image", "m.file", "m.video", "m.audio"];
|
||||
return mxEv.getType() === attachmentTypes[0] || attachmentTypes.includes(mxEv.getContent().msgtype);
|
||||
}
|
||||
|
||||
protected async createMessageBody(mxEv: MatrixEvent, joined = false) {
|
||||
let eventTile: string;
|
||||
|
||||
|
|
Loading…
Reference in a new issue