mirror of https://github.com/vector-im/riot-web
Wrap media fetch in try catch
parent
5fff64f128
commit
dd40f81edf
|
@ -57,6 +57,7 @@ export default class HTMLExporter extends Exporter {
|
||||||
if (avatarUrl) {
|
if (avatarUrl) {
|
||||||
const image = await fetch(avatarUrl);
|
const image = await fetch(avatarUrl);
|
||||||
blob = await image.blob();
|
blob = await image.blob();
|
||||||
|
this.totalSize += blob.size;
|
||||||
this.addFile(avatarPath, blob);
|
this.addFile(avatarPath, blob);
|
||||||
}
|
}
|
||||||
const avatar = (
|
const avatar = (
|
||||||
|
@ -270,32 +271,44 @@ export default class HTMLExporter extends Exporter {
|
||||||
return eventTileMarkup;
|
return eventTileMarkup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected createModifiedEvent = (text: string, mxEv: MatrixEvent) => {
|
||||||
|
const modifiedContent = {
|
||||||
|
msgtype: "m.text",
|
||||||
|
body: `*${text}*`,
|
||||||
|
format: "org.matrix.custom.html",
|
||||||
|
formatted_body: `<em>${text}</em>`,
|
||||||
|
}
|
||||||
|
const modifiedEvent = new MatrixEvent();
|
||||||
|
modifiedEvent.event = mxEv.event;
|
||||||
|
modifiedEvent.sender = mxEv.sender;
|
||||||
|
modifiedEvent.event.type = "m.room.message";
|
||||||
|
modifiedEvent.event.content = modifiedContent;
|
||||||
|
return modifiedEvent;
|
||||||
|
}
|
||||||
|
|
||||||
protected async createMessageBody(mxEv: MatrixEvent, joined = false) {
|
protected async createMessageBody(mxEv: MatrixEvent, joined = false) {
|
||||||
let eventTile: string;
|
let eventTile: string;
|
||||||
|
|
||||||
if (this.isAttachment(mxEv)) {
|
if (this.isAttachment(mxEv)) {
|
||||||
if (this.exportOptions.attachmentsIncluded) {
|
if (this.exportOptions.attachmentsIncluded) {
|
||||||
const blob = await this.getMediaBlob(mxEv);
|
try {
|
||||||
this.totalSize += blob.size;
|
const blob = await this.getMediaBlob(mxEv);
|
||||||
const filePath = this.getFilePath(mxEv);
|
this.totalSize += blob.size;
|
||||||
eventTile = await this.getEventTile(mxEv, joined, filePath);
|
const filePath = this.getFilePath(mxEv);
|
||||||
if (this.totalSize > this.exportOptions.maxSize - 1024 * 1024) {
|
eventTile = await this.getEventTile(mxEv, joined, filePath);
|
||||||
this.exportOptions.attachmentsIncluded = false;
|
if (this.totalSize > this.exportOptions.maxSize - 1024 * 512) {
|
||||||
|
this.exportOptions.attachmentsIncluded = false;
|
||||||
|
}
|
||||||
|
this.addFile(filePath, blob);
|
||||||
|
} catch (e) {
|
||||||
|
console.log("Error while fetching file");
|
||||||
|
eventTile = await this.getEventTile(
|
||||||
|
this.createModifiedEvent(_t("Error fetching file"), mxEv),
|
||||||
|
joined,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
this.addFile(filePath, blob);
|
|
||||||
} else {
|
} else {
|
||||||
const modifiedContent = {
|
eventTile = await this.getEventTile(this.createModifiedEvent(this.mediaOmitText, mxEv), joined);
|
||||||
msgtype: "m.text",
|
|
||||||
body: `*${this.mediaOmitText}*`,
|
|
||||||
format: "org.matrix.custom.html",
|
|
||||||
formatted_body: `<em>${this.mediaOmitText}</em>`,
|
|
||||||
}
|
|
||||||
const modifiedEvent = new MatrixEvent();
|
|
||||||
modifiedEvent.event = mxEv.event;
|
|
||||||
modifiedEvent.sender = mxEv.sender;
|
|
||||||
modifiedEvent.event.type = "m.room.message";
|
|
||||||
modifiedEvent.event.content = modifiedContent;
|
|
||||||
eventTile = await this.getEventTile(modifiedEvent, joined);
|
|
||||||
}
|
}
|
||||||
} else eventTile = await this.getEventTile(mxEv, joined);
|
} else eventTile = await this.getEventTile(mxEv, joined);
|
||||||
|
|
||||||
|
@ -305,14 +318,16 @@ export default class HTMLExporter extends Exporter {
|
||||||
protected async createHTML(events: MatrixEvent[]) {
|
protected async createHTML(events: MatrixEvent[]) {
|
||||||
let content = "";
|
let content = "";
|
||||||
let prevEvent = null;
|
let prevEvent = null;
|
||||||
for (const event of events) {
|
for (let i = 0; i < events.length; i++) {
|
||||||
|
const event = events[i];
|
||||||
|
console.log("Processing event " + i + " out of " + events.length);
|
||||||
if (!haveTileForEvent(event)) continue;
|
if (!haveTileForEvent(event)) continue;
|
||||||
|
|
||||||
content += this._wantsDateSeparator(event, prevEvent) ? this.getDateSeparator(event) : "";
|
content += this._wantsDateSeparator(event, prevEvent) ? this.getDateSeparator(event) : "";
|
||||||
const shouldBeJoined = !this._wantsDateSeparator(event, prevEvent)
|
const shouldBeJoined = !this._wantsDateSeparator(event, prevEvent)
|
||||||
&& shouldFormContinuation(prevEvent, event);
|
&& shouldFormContinuation(prevEvent, event);
|
||||||
const body = await this.createMessageBody(event, shouldBeJoined);
|
const body = await this.createMessageBody(event, shouldBeJoined);
|
||||||
|
this.totalSize += Buffer.byteLength(body);
|
||||||
content += body;
|
content += body;
|
||||||
prevEvent = event;
|
prevEvent = event;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue