2021-06-25 05:46:59 +00:00
|
|
|
import streamSaver from "streamsaver";
|
2021-06-22 05:21:16 +00:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
import { MatrixClientPeg } from "../../MatrixClientPeg";
|
|
|
|
import { exportOptions, exportTypes } from "./exportUtils";
|
|
|
|
import { decryptFile } from "../DecryptFile";
|
|
|
|
import { mediaFromContent } from "../../customisations/Media";
|
|
|
|
import { formatFullDateNoDay } from "../../DateUtils";
|
2021-06-24 12:49:12 +00:00
|
|
|
import { MatrixClient } from "matrix-js-sdk";
|
2021-06-29 14:13:21 +00:00
|
|
|
import streamToZIP from "./ZipStream";
|
2021-06-30 08:38:22 +00:00
|
|
|
import * as ponyfill from "web-streams-polyfill/ponyfill";
|
2021-06-25 05:46:59 +00:00
|
|
|
import "web-streams-polyfill/ponyfill"; // to support streams API for older browsers
|
2021-05-24 15:18:13 +00:00
|
|
|
|
2021-06-23 06:28:50 +00:00
|
|
|
type FileStream = {
|
2021-06-29 04:49:57 +00:00
|
|
|
name: string;
|
|
|
|
stream(): ReadableStream;
|
2021-06-23 06:28:50 +00:00
|
|
|
};
|
|
|
|
|
2021-06-03 08:09:14 +00:00
|
|
|
export default abstract class Exporter {
|
2021-06-23 06:28:50 +00:00
|
|
|
protected files: FileStream[];
|
2021-06-24 12:49:12 +00:00
|
|
|
protected client: MatrixClient;
|
2021-06-25 05:46:59 +00:00
|
|
|
protected writer: WritableStreamDefaultWriter<any>;
|
|
|
|
protected fileStream: WritableStream<any>;
|
2021-06-27 15:25:54 +00:00
|
|
|
protected cancelled: boolean;
|
2021-06-25 05:46:59 +00:00
|
|
|
|
2021-06-11 06:34:05 +00:00
|
|
|
protected constructor(
|
|
|
|
protected room: Room,
|
|
|
|
protected exportType: exportTypes,
|
|
|
|
protected exportOptions?: exportOptions,
|
2021-06-23 06:28:50 +00:00
|
|
|
) {
|
2021-06-27 15:25:54 +00:00
|
|
|
this.cancelled = false;
|
2021-06-23 06:28:50 +00:00
|
|
|
this.files = [];
|
2021-06-24 12:49:12 +00:00
|
|
|
this.client = MatrixClientPeg.get();
|
2021-06-25 05:46:59 +00:00
|
|
|
window.addEventListener("beforeunload", this.onBeforeUnload);
|
2021-06-27 15:25:54 +00:00
|
|
|
window.addEventListener("onunload", this.abortWriter);
|
2021-06-25 05:46:59 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 06:10:26 +00:00
|
|
|
protected onBeforeUnload(e: BeforeUnloadEvent): string {
|
2021-06-25 05:46:59 +00:00
|
|
|
e.preventDefault();
|
|
|
|
return e.returnValue = "Are you sure you want to exit during this export?";
|
2021-06-23 06:28:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 06:10:26 +00:00
|
|
|
protected addFile(filePath: string, blob: Blob): void {
|
2021-06-23 06:28:50 +00:00
|
|
|
const file = {
|
|
|
|
name: filePath,
|
|
|
|
stream: () => blob.stream(),
|
2021-06-30 08:38:22 +00:00
|
|
|
};
|
2021-06-23 06:28:50 +00:00
|
|
|
this.files.push(file);
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:10:26 +00:00
|
|
|
protected async downloadZIP(): Promise<any> {
|
2021-06-25 05:46:59 +00:00
|
|
|
const filename = `matrix-export-${formatFullDateNoDay(new Date())}.zip`;
|
2021-06-25 06:22:26 +00:00
|
|
|
|
|
|
|
// Support for older browsers
|
2021-06-30 08:38:22 +00:00
|
|
|
streamSaver.WritableStream = ponyfill.WritableStream;
|
2021-06-25 06:22:26 +00:00
|
|
|
|
|
|
|
// Create a writable stream to the directory
|
2021-06-25 05:46:59 +00:00
|
|
|
this.fileStream = streamSaver.createWriteStream(filename);
|
|
|
|
|
2021-06-27 15:25:54 +00:00
|
|
|
if (!this.cancelled) console.info("Generating a ZIP...");
|
|
|
|
else return this.cleanUp();
|
2021-06-25 06:22:26 +00:00
|
|
|
|
2021-06-25 05:46:59 +00:00
|
|
|
this.writer = this.fileStream.getWriter();
|
|
|
|
const files = this.files;
|
|
|
|
|
|
|
|
const readableZipStream = streamToZIP({
|
|
|
|
start(ctrl) {
|
|
|
|
for (const file of files) ctrl.enqueue(file);
|
|
|
|
ctrl.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-06-27 15:25:54 +00:00
|
|
|
if (this.cancelled) return this.cleanUp();
|
|
|
|
|
2021-06-30 08:38:22 +00:00
|
|
|
console.info("Writing to the file system...");
|
2021-06-25 05:46:59 +00:00
|
|
|
|
2021-06-30 08:38:22 +00:00
|
|
|
const reader = readableZipStream.getReader();
|
2021-06-25 05:46:59 +00:00
|
|
|
await this.pumpToFileStream(reader);
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:10:26 +00:00
|
|
|
protected cleanUp(): string {
|
2021-06-27 15:25:54 +00:00
|
|
|
console.log("Cleaning up...");
|
|
|
|
window.removeEventListener("beforeunload", this.onBeforeUnload);
|
|
|
|
window.removeEventListener("onunload", this.abortWriter);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:10:26 +00:00
|
|
|
public async cancelExport(): Promise<void> {
|
2021-06-27 15:25:54 +00:00
|
|
|
console.log("Cancelling export...");
|
|
|
|
this.cancelled = true;
|
|
|
|
await this.abortWriter();
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:46:59 +00:00
|
|
|
protected async downloadPlainText(fileName: string, text: string): Promise<any> {
|
|
|
|
this.fileStream = streamSaver.createWriteStream(fileName);
|
2021-06-30 08:38:22 +00:00
|
|
|
this.writer = this.fileStream.getWriter();
|
2021-06-25 05:46:59 +00:00
|
|
|
const data = new TextEncoder().encode(text);
|
2021-06-27 15:25:54 +00:00
|
|
|
if (this.cancelled) return this.cleanUp();
|
2021-06-25 05:46:59 +00:00
|
|
|
await this.writer.write(data);
|
|
|
|
await this.writer.close();
|
|
|
|
}
|
|
|
|
|
2021-06-27 15:25:54 +00:00
|
|
|
protected async abortWriter(): Promise<void> {
|
2021-06-25 06:22:26 +00:00
|
|
|
await this.fileStream?.abort();
|
|
|
|
await this.writer?.abort();
|
2021-06-25 05:46:59 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 06:10:26 +00:00
|
|
|
protected async pumpToFileStream(reader: ReadableStreamDefaultReader): Promise<void> {
|
2021-06-23 06:28:50 +00:00
|
|
|
const res = await reader.read();
|
2021-06-25 05:46:59 +00:00
|
|
|
if (res.done) await this.writer.close();
|
2021-06-23 06:28:50 +00:00
|
|
|
else {
|
2021-06-25 05:46:59 +00:00
|
|
|
await this.writer.write(res.value);
|
|
|
|
await this.pumpToFileStream(reader);
|
2021-06-23 06:28:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-03 07:51:56 +00:00
|
|
|
|
2021-06-29 06:10:26 +00:00
|
|
|
protected setEventMetadata(event: MatrixEvent): MatrixEvent {
|
2021-06-24 12:49:12 +00:00
|
|
|
const roomState = this.client.getRoom(this.room.roomId).currentState;
|
2021-06-07 06:04:03 +00:00
|
|
|
event.sender = roomState.getSentinelMember(
|
|
|
|
event.getSender(),
|
|
|
|
);
|
|
|
|
if (event.getType() === "m.room.member") {
|
|
|
|
event.target = roomState.getSentinelMember(
|
|
|
|
event.getStateKey(),
|
|
|
|
);
|
2021-06-04 09:38:17 +00:00
|
|
|
}
|
2021-06-07 06:04:03 +00:00
|
|
|
return event;
|
2021-06-04 09:38:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 06:10:26 +00:00
|
|
|
protected getLimit(): number {
|
2021-06-11 06:34:05 +00:00
|
|
|
let limit: number;
|
|
|
|
switch (this.exportType) {
|
|
|
|
case exportTypes.LAST_N_MESSAGES:
|
|
|
|
limit = this.exportOptions.numberOfMessages;
|
|
|
|
break;
|
|
|
|
case exportTypes.TIMELINE:
|
|
|
|
limit = 40;
|
|
|
|
break;
|
|
|
|
default:
|
2021-06-25 09:19:01 +00:00
|
|
|
limit = 10**8;
|
2021-06-11 06:34:05 +00:00
|
|
|
}
|
|
|
|
return limit;
|
|
|
|
}
|
|
|
|
|
2021-06-30 08:38:22 +00:00
|
|
|
protected async getRequiredEvents(): Promise<MatrixEvent[]> {
|
2021-06-24 12:49:12 +00:00
|
|
|
const eventMapper = this.client.getEventMapper();
|
2021-06-04 09:38:17 +00:00
|
|
|
|
|
|
|
let prevToken: string|null = null;
|
2021-06-11 06:34:05 +00:00
|
|
|
let limit = this.getLimit();
|
2021-06-04 09:38:17 +00:00
|
|
|
let events: MatrixEvent[] = [];
|
2021-06-07 06:04:03 +00:00
|
|
|
|
2021-06-04 09:38:17 +00:00
|
|
|
while (limit) {
|
2021-06-10 06:23:41 +00:00
|
|
|
const eventsPerCrawl = Math.min(limit, 1000);
|
2021-06-24 12:49:12 +00:00
|
|
|
const res: any = await this.client.createMessagesRequest(this.room.roomId, prevToken, eventsPerCrawl, "b");
|
2021-06-04 09:38:17 +00:00
|
|
|
|
2021-06-27 15:25:54 +00:00
|
|
|
if (this.cancelled) {
|
|
|
|
this.cleanUp();
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2021-06-04 09:38:17 +00:00
|
|
|
if (res.chunk.length === 0) break;
|
|
|
|
|
2021-06-10 06:23:41 +00:00
|
|
|
limit -= res.chunk.length;
|
2021-06-04 09:38:17 +00:00
|
|
|
|
|
|
|
const matrixEvents: MatrixEvent[] = res.chunk.map(eventMapper);
|
|
|
|
|
2021-06-11 06:34:05 +00:00
|
|
|
for (const mxEv of matrixEvents) {
|
|
|
|
if (this.exportOptions.startDate && mxEv.getTs() < this.exportOptions.startDate) {
|
|
|
|
// Once the last message received is older than the start date, we break out of both the loops
|
|
|
|
limit = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
events.push(mxEv);
|
|
|
|
}
|
2021-06-25 09:49:17 +00:00
|
|
|
console.log("Fetched " + events.length + " events so far.");
|
2021-06-04 09:38:17 +00:00
|
|
|
prevToken = res.end;
|
|
|
|
}
|
2021-06-08 13:07:36 +00:00
|
|
|
// Reverse the events so that we preserve the order
|
2021-06-07 06:04:03 +00:00
|
|
|
events = events.reverse();
|
2021-06-04 09:38:17 +00:00
|
|
|
|
|
|
|
const decryptionPromises = events
|
|
|
|
.filter(event => event.isEncrypted())
|
|
|
|
.map(event => {
|
2021-06-24 12:49:12 +00:00
|
|
|
return this.client.decryptEventIfNeeded(event, {
|
2021-06-07 06:04:03 +00:00
|
|
|
isRetry: true,
|
|
|
|
emit: false,
|
2021-06-04 09:38:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-06-08 13:07:14 +00:00
|
|
|
// Wait for all the events to get decrypted.
|
2021-06-04 09:38:17 +00:00
|
|
|
await Promise.all(decryptionPromises);
|
|
|
|
|
2021-06-07 09:17:27 +00:00
|
|
|
for (let i = 0; i < events.length; i++) this.setEventMetadata(events[i]);
|
2021-06-04 09:38:17 +00:00
|
|
|
|
2021-06-07 06:04:03 +00:00
|
|
|
return events;
|
2021-06-04 09:38:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 06:10:26 +00:00
|
|
|
protected async getMediaBlob(event: MatrixEvent): Promise<Blob> {
|
2021-06-14 12:36:40 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:10:26 +00:00
|
|
|
protected splitFileName(file: string): string[] {
|
2021-06-14 12:36:40 +00:00
|
|
|
const lastDot = file.lastIndexOf('.');
|
|
|
|
if (lastDot === -1) return [file, ""];
|
|
|
|
const fileName = file.slice(0, lastDot);
|
|
|
|
const ext = file.slice(lastDot + 1);
|
|
|
|
return [fileName, '.' + ext];
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:10:26 +00:00
|
|
|
protected getFilePath(event: MatrixEvent): string {
|
2021-06-14 12:36:40 +00:00
|
|
|
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()));
|
2021-06-25 09:49:17 +00:00
|
|
|
let [fileName, fileExt] = this.splitFileName(event.getContent().body);
|
|
|
|
if (event.getType() === "m.sticker") fileExt = ".png";
|
2021-06-14 12:36:40 +00:00
|
|
|
return fileDirectory + "/" + fileName + '-' + fileDate + fileExt;
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:10:26 +00:00
|
|
|
protected isReply(event: MatrixEvent): boolean {
|
2021-06-22 07:20:15 +00:00
|
|
|
const isEncrypted = event.isEncrypted();
|
|
|
|
// If encrypted, in_reply_to lies in event.event.content
|
|
|
|
const content = isEncrypted ? event.event.content : event.getContent();
|
|
|
|
const relatesTo = content["m.relates_to"];
|
2021-06-22 05:19:14 +00:00
|
|
|
return !!(relatesTo && relatesTo["m.in_reply_to"]);
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:10:26 +00:00
|
|
|
protected isAttachment(mxEv: MatrixEvent): boolean {
|
2021-06-14 12:36:40 +00:00
|
|
|
const attachmentTypes = ["m.sticker", "m.image", "m.file", "m.video", "m.audio"];
|
|
|
|
return mxEv.getType() === attachmentTypes[0] || attachmentTypes.includes(mxEv.getContent().msgtype);
|
|
|
|
}
|
|
|
|
|
2021-06-17 05:16:08 +00:00
|
|
|
abstract export(): Promise<any>;
|
2021-05-24 15:18:13 +00:00
|
|
|
}
|