element-web/src/utils/exportUtils/PlainTextExport.ts

137 lines
5.3 KiB
TypeScript
Raw Normal View History

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 { haveTileForEvent } from "../../components/views/rooms/EventTile";
import { exportTypes } from "./exportUtils";
import { exportOptions } from "./exportUtils";
import { textForEvent } from "../../TextForEvent";
2021-07-02 11:22:33 +00:00
import { MutableRefObject } from "react";
export default class PlainTextExporter extends Exporter {
protected totalSize: number;
protected mediaOmitText: string;
2021-07-02 04:53:25 +00:00
constructor(
room: Room,
exportType: exportTypes,
exportOptions: exportOptions,
2021-07-02 11:22:33 +00:00
exportProgressRef: MutableRefObject<HTMLParagraphElement>,
2021-07-02 04:53:25 +00:00
) {
2021-07-02 11:22:33 +00:00
super(room, exportType, exportOptions, exportProgressRef);
this.totalSize = 0;
this.mediaOmitText = !this.exportOptions.attachmentsIncluded
? _t("Media omitted")
: _t("Media omitted - file size limit exceeded");
}
2021-06-30 08:38:22 +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);
if (!match) return content.body;
let rplSource: string;
const rplName = match[1];
const rplText = match[3];
rplSource = match[2].substring(1, REPLY_SOURCE_MAX_LENGTH);
// Get the first non-blank line from the source.
2021-06-30 08:38:22 +00:00
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-30 08:38:22 +00:00
};
protected _textForEvent = async (mxEv: MatrixEvent) => {
const senderDisplayName = mxEv.sender && mxEv.sender.name ? mxEv.sender.name : mxEv.getSender();
2021-06-25 09:19:01 +00:00
let mediaText = "";
if (this.isAttachment(mxEv)) {
if (this.exportOptions.attachmentsIncluded) {
try {
const blob = await this.getMediaBlob(mxEv);
if (this.totalSize + blob.size > this.exportOptions.maxSize) {
mediaText = ` (${this.mediaOmitText})`;
} else {
this.totalSize += blob.size;
const filePath = this.getFilePath(mxEv);
mediaText = " (" + _t("File Attached") + ")";
this.addFile(filePath, blob);
if (this.totalSize == this.exportOptions.maxSize) {
this.exportOptions.attachmentsIncluded = false;
}
2021-06-25 09:19:01 +00:00
}
} catch (error) {
mediaText = " (" + _t("Error fetching file") + ")";
2021-06-25 09:28:59 +00:00
console.log("Error fetching file " + error);
2021-06-25 09:19:01 +00:00
}
} else mediaText = ` (${this.mediaOmitText})`;
}
2021-06-25 09:19:01 +00:00
if (this.isReply(mxEv)) return senderDisplayName + ": " + this.textForReplyEvent(mxEv) + mediaText;
2021-06-25 09:31:14 +00:00
else return textForEvent(mxEv) + mediaText;
2021-06-30 08:38:22 +00:00
};
protected async createOutput(events: MatrixEvent[]) {
let content = "";
2021-07-02 04:53:25 +00:00
for (let i = 0; i < events.length; i++) {
const event = events[i];
2021-07-02 11:22:33 +00:00
this.updateProgress(`Processing event ${i + 1} out of ${events.length}`, false, true);
2021-06-27 15:25:54 +00:00
if (this.cancelled) return this.cleanUp();
if (!haveTileForEvent(event)) continue;
const textForEvent = await this._textForEvent(event);
content += textForEvent && `${new Date(event.getTs()).toLocaleString()} - ${textForEvent}\n`;
}
return content;
}
public async export() {
2021-07-02 04:53:25 +00:00
this.updateProgress("Starting export process...");
this.updateProgress("Fetching events...");
2021-06-25 10:19:39 +00:00
const fetchStart = performance.now();
const res = await this.getRequiredEvents();
const fetchEnd = performance.now();
console.log(`Fetched ${res.length} events in ${(fetchEnd - fetchStart)/1000}s`);
2021-07-02 04:53:25 +00:00
this.updateProgress("Creating output...");
const text = await this.createOutput(res);
if (this.files.length) {
this.addFile("export.txt", new Blob([text]));
await this.downloadZIP();
} else {
const fileName = `matrix-export-${formatFullDateNoDay(new Date())}.txt`;
await this.downloadPlainText(fileName, text);
}
const exportEnd = performance.now();
2021-06-25 10:19:39 +00:00
2021-06-27 15:25:54 +00:00
if (this.cancelled) {
console.info("Export cancelled successfully");
} else {
2021-06-30 08:38:22 +00:00
console.info("Export successful!");
2021-06-27 15:25:54 +00:00
console.log(`Exported ${res.length} events in ${(exportEnd - fetchStart)/1000} seconds`);
}
2021-06-25 10:19:39 +00:00
2021-06-27 15:25:54 +00:00
this.cleanUp();
}
}