Fix types and precompute blob sizes to avoid overflows

This commit is contained in:
Jaiwanth 2021-07-19 13:17:19 +05:30
parent ed93bf4c77
commit f07402d234
5 changed files with 31 additions and 21 deletions

View file

@ -179,7 +179,7 @@ export default class MImageBody extends React.Component<IProps, IState> {
};
protected getContentUrl(): string {
const content = this.props.mxEvent.getContent();
const content: IMediaEventContent= this.props.mxEvent.getContent();
if (this.props.forExport) return content.url || content.file.url;
const media = mediaFromContent(content);
if (media.isEncrypted) {

View file

@ -306,13 +306,17 @@ export default class HTMLExporter extends Exporter {
if (this.exportOptions.attachmentsIncluded) {
try {
const blob = await this.getMediaBlob(mxEv);
this.totalSize += blob.size;
const filePath = this.getFilePath(mxEv);
eventTile = await this.getEventTile(mxEv, joined, filePath);
if (this.totalSize > this.exportOptions.maxSize - 1024 * 512) {
this.exportOptions.attachmentsIncluded = false;
if (this.totalSize + blob.size > this.exportOptions.maxSize) {
eventTile = await this.getEventTile(this.createModifiedEvent(this.mediaOmitText, mxEv), joined);
} else {
this.totalSize += blob.size;
const filePath = this.getFilePath(mxEv);
eventTile = await this.getEventTile(mxEv, joined, filePath);
if (this.totalSize == this.exportOptions.maxSize) {
this.exportOptions.attachmentsIncluded = false;
}
this.addFile(filePath, blob);
}
this.addFile(filePath, blob);
} catch (e) {
console.log("Error while fetching file" + e);
eventTile = await this.getEventTile(
@ -339,7 +343,7 @@ export default class HTMLExporter extends Exporter {
content += this._wantsDateSeparator(event, prevEvent) ? this.getDateSeparator(event) : "";
const shouldBeJoined = !this._wantsDateSeparator(event, prevEvent)
&& shouldFormContinuation(prevEvent, event);
&& shouldFormContinuation(prevEvent, event, false);
const body = await this.createMessageBody(event, shouldBeJoined);
this.totalSize += Buffer.byteLength(body);
content += body;

View file

@ -45,11 +45,13 @@ export default class JSONExporter extends Exporter {
if (this.exportOptions.attachmentsIncluded && this.isAttachment(mxEv)) {
try {
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;
if (this.totalSize + blob.size < this.exportOptions.maxSize) {
this.totalSize += blob.size;
const filePath = this.getFilePath(mxEv);
if (this.totalSize == this.exportOptions.maxSize) {
this.exportOptions.attachmentsIncluded = false;
}
this.addFile(filePath, blob);
}
} catch (err) {
console.log("Error fetching file: " + err);

View file

@ -66,12 +66,16 @@ export default class PlainTextExporter extends Exporter {
if (this.exportOptions.attachmentsIncluded) {
try {
const blob = await this.getMediaBlob(mxEv);
this.totalSize += blob.size;
const filePath = this.getFilePath(mxEv);
mediaText = " (" + _t("File Attached") + ")";
this.addFile(filePath, blob);
if (this.totalSize > this.exportOptions.maxSize - 1024 * 1024) {
this.exportOptions.attachmentsIncluded = false;
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;
}
}
} catch (error) {
mediaText = " (" + _t("Error fetching file") + ")";

View file

@ -223,8 +223,8 @@ export default function streamToZIP(underlyingSource: UnderlyingSource) {
function closeZip() {
let length = 0;
let index = 0;
let indexFilename;
let file;
let indexFilename: number;
let file: any;
for (indexFilename = 0; indexFilename < filenames.length; indexFilename++) {
file = files[filenames[indexFilename]];