2021-06-17 05:16:08 +00:00
|
|
|
import streamSaver from "streamsaver";
|
|
|
|
import Exporter from "./Exporter";
|
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
import { formatFullDateNoDay } from "../../DateUtils";
|
|
|
|
import { _t } from "../../languageHandler";
|
|
|
|
import * as ponyfill from "web-streams-polyfill/ponyfill"
|
|
|
|
import { haveTileForEvent } from "../../components/views/rooms/EventTile";
|
|
|
|
import { exportTypes } from "./exportUtils";
|
|
|
|
import { exportOptions } from "./exportUtils";
|
|
|
|
import { textForEvent } from "../../TextForEvent";
|
2021-06-23 06:28:50 +00:00
|
|
|
import zip from "./StreamToZip";
|
|
|
|
|
2021-06-17 05:16:08 +00:00
|
|
|
|
|
|
|
export default class PlainTextExporter extends Exporter {
|
|
|
|
protected totalSize: number;
|
|
|
|
protected mediaOmitText: string;
|
2021-06-23 06:28:50 +00:00
|
|
|
private readonly fileDir: string;
|
2021-06-17 05:16:08 +00:00
|
|
|
|
|
|
|
constructor(room: Room, exportType: exportTypes, exportOptions: exportOptions) {
|
|
|
|
super(room, exportType, exportOptions);
|
|
|
|
this.totalSize = 0;
|
2021-06-23 06:28:50 +00:00
|
|
|
this.fileDir = `matrix-export-${formatFullDateNoDay(new Date())}`;
|
2021-06-17 05:16:08 +00:00
|
|
|
this.mediaOmitText = !this.exportOptions.attachmentsIncluded
|
|
|
|
? _t("Media omitted")
|
|
|
|
: _t("Media omitted - file size limit exceeded");
|
|
|
|
window.addEventListener("beforeunload", this.onBeforeUnload)
|
|
|
|
}
|
|
|
|
|
|
|
|
protected onBeforeUnload = (e: BeforeUnloadEvent) => {
|
|
|
|
e.preventDefault();
|
|
|
|
return e.returnValue = "Are you sure you want to exit during this export?";
|
|
|
|
}
|
|
|
|
|
2021-06-22 05:19:14 +00:00
|
|
|
protected textForReplyEvent = (ev : MatrixEvent) => {
|
|
|
|
const REPLY_REGEX = /> <(.*?)>(.*?)\n\n(.*)/;
|
|
|
|
const REPLY_SOURCE_MAX_LENGTH = 32;
|
|
|
|
const content = ev.getContent();
|
|
|
|
|
|
|
|
const match = REPLY_REGEX.exec(content.body);
|
|
|
|
|
2021-06-22 06:42:37 +00:00
|
|
|
if (!match) return content.body;
|
|
|
|
|
2021-06-22 05:19:14 +00:00
|
|
|
let rplSource: string;
|
|
|
|
const rplName = match[1];
|
|
|
|
const rplText = match[3];
|
2021-06-22 06:42:37 +00:00
|
|
|
|
|
|
|
rplSource = match[2].substring(1, REPLY_SOURCE_MAX_LENGTH);
|
2021-06-22 05:19:14 +00:00
|
|
|
// Get the first non-blank line from the source.
|
|
|
|
const lines = rplSource.split('\n').filter((line) => !/^\s*$/.test(line))
|
|
|
|
if (lines.length > 0) {
|
|
|
|
// Cut to a maximum length.
|
|
|
|
rplSource = lines[0].substring(0, REPLY_SOURCE_MAX_LENGTH);
|
|
|
|
// Ellipsis if needed.
|
|
|
|
if (lines[0].length > REPLY_SOURCE_MAX_LENGTH) {
|
|
|
|
rplSource = rplSource + "...";
|
|
|
|
}
|
|
|
|
// Wrap in formatting
|
|
|
|
rplSource = ` "${rplSource}"`;
|
|
|
|
} else {
|
|
|
|
// Don't show a source because we couldn't format one.
|
|
|
|
rplSource = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return `<${rplName}${rplSource}> ${rplText}`;
|
|
|
|
}
|
|
|
|
|
2021-06-23 06:28:50 +00:00
|
|
|
protected _textForEvent = async (mxEv: MatrixEvent) => {
|
2021-06-22 05:19:14 +00:00
|
|
|
const senderDisplayName = mxEv.sender && mxEv.sender.name ? mxEv.sender.name : mxEv.getSender();
|
2021-06-23 06:28:50 +00:00
|
|
|
if (this.exportOptions.attachmentsIncluded && this.isAttachment(mxEv)) {
|
|
|
|
const blob = await this.getMediaBlob(mxEv);
|
|
|
|
this.totalSize += blob.size;
|
|
|
|
const filePath = this.getFilePath(mxEv);
|
|
|
|
this.addFile(filePath, blob);
|
|
|
|
if (this.totalSize > this.exportOptions.maxSize - 1024 * 1024) {
|
|
|
|
this.exportOptions.attachmentsIncluded = false;
|
|
|
|
}
|
|
|
|
}
|
2021-06-22 05:19:14 +00:00
|
|
|
if (this.isReply(mxEv)) return senderDisplayName + ": " + this.textForReplyEvent(mxEv);
|
|
|
|
else return textForEvent(mxEv);
|
|
|
|
}
|
|
|
|
|
2021-06-17 05:16:08 +00:00
|
|
|
protected async createOutput(events: MatrixEvent[]) {
|
|
|
|
let content = "";
|
|
|
|
for (const event of events) {
|
|
|
|
if (!haveTileForEvent(event)) continue;
|
2021-06-23 06:28:50 +00:00
|
|
|
const textForEvent = await this._textForEvent(event);
|
2021-06-22 07:20:15 +00:00
|
|
|
content += textForEvent && `${new Date(event.getTs()).toLocaleString()} - ${textForEvent}\n`;
|
2021-06-17 05:16:08 +00:00
|
|
|
}
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2021-06-23 06:28:50 +00:00
|
|
|
protected getFileName = () => {
|
|
|
|
if (this.exportOptions.attachmentsIncluded) {
|
|
|
|
return `${this.room.name}.txt`;
|
|
|
|
} else return `${this.fileDir}.txt`
|
|
|
|
}
|
|
|
|
|
2021-06-17 05:16:08 +00:00
|
|
|
public async export() {
|
|
|
|
console.info("Starting export process...");
|
|
|
|
console.info("Fetching events...");
|
|
|
|
const fetchStart = performance.now();
|
|
|
|
const res = await this.getRequiredEvents();
|
|
|
|
const fetchEnd = performance.now();
|
|
|
|
|
|
|
|
console.log(`Fetched ${res.length} events in ${(fetchEnd - fetchStart)/1000} s`);
|
|
|
|
console.info("Creating Output...");
|
|
|
|
|
|
|
|
const text = await this.createOutput(res);
|
|
|
|
|
2021-06-23 06:28:50 +00:00
|
|
|
console.info("Writing to the file system...");
|
2021-06-17 05:16:08 +00:00
|
|
|
streamSaver.WritableStream = ponyfill.WritableStream
|
2021-06-23 06:28:50 +00:00
|
|
|
|
|
|
|
const files = this.files;
|
|
|
|
if (files.length) {
|
|
|
|
this.addFile(this.getFileName(), new Blob([text]));
|
|
|
|
const fileStream = streamSaver.createWriteStream(`${this.fileDir}.zip`);
|
|
|
|
const readableZipStream = zip({
|
|
|
|
start(ctrl) {
|
|
|
|
for (const file of files) ctrl.enqueue(file);
|
|
|
|
ctrl.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const writer = fileStream.getWriter()
|
|
|
|
const reader = readableZipStream.getReader()
|
|
|
|
await this.pumpToFileStream(reader, writer);
|
|
|
|
} else {
|
|
|
|
const fileStream = streamSaver.createWriteStream(`${this.fileDir}.txt`);
|
|
|
|
const writer = fileStream.getWriter()
|
|
|
|
const data = new TextEncoder().encode(text);
|
|
|
|
await writer.write(data);
|
|
|
|
await writer.close();
|
|
|
|
}
|
|
|
|
|
2021-06-17 05:16:08 +00:00
|
|
|
const exportEnd = performance.now();
|
|
|
|
console.info(`Export Successful! Exported ${res.length} events in ${(exportEnd - fetchStart)/1000} seconds`);
|
|
|
|
window.removeEventListener("beforeunload", this.onBeforeUnload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|