better error handling for file and picture upload

This commit is contained in:
Bruno Windels 2020-11-18 20:07:31 +01:00
parent 47147f2d46
commit f6dbb23f79
2 changed files with 42 additions and 32 deletions

View file

@ -165,13 +165,19 @@ export class RoomViewModel extends ViewModel {
return false; return false;
} }
async _sendFile() { async _pickAndSendFile() {
let file;
try { try {
file = await this.platform.openFile(); const file = await this.platform.openFile();
} catch (err) { if (!file) {
return; return;
} }
return this._sendFile(file);
} catch (err) {
console.error(err);
}
}
async _sendFile(file) {
const content = { const content = {
body: file.name, body: file.name,
msgtype: "m.file" msgtype: "m.file"
@ -179,20 +185,21 @@ export class RoomViewModel extends ViewModel {
await this._room.sendEvent("m.room.message", content, { await this._room.sendEvent("m.room.message", content, {
"url": this._room.createAttachment(file.blob, file.name) "url": this._room.createAttachment(file.blob, file.name)
}); });
// TODO: dispose file.blob (in the attachment, after upload)
} }
async _sendPicture() { async _pickAndSendPicture() {
try {
if (!this.platform.hasReadPixelPermission()) { if (!this.platform.hasReadPixelPermission()) {
alert("Please allow canvas image data access, so we can scale your images down."); alert("Please allow canvas image data access, so we can scale your images down.");
return; return;
} }
let file; const file = await this.platform.openFile("image/*");
try { if (!file) {
file = await this.platform.openFile("image/*");
} catch (err) {
return; return;
} }
if (!file.blob.mimeType.startsWith("image/")) {
return this._sendFile(file);
}
const image = await this.platform.loadImage(file.blob); const image = await this.platform.loadImage(file.blob);
const content = { const content = {
body: file.name, body: file.name,
@ -200,7 +207,7 @@ export class RoomViewModel extends ViewModel {
info: imageToInfo(image) info: imageToInfo(image)
}; };
const attachments = { const attachments = {
"url": this._room.createAttachment(file.blob, file.name), "url": this._room.createAttachment(image.blob, file.name),
}; };
if (image.maxDimension > 600) { if (image.maxDimension > 600) {
const thumbnail = await image.scale(400); const thumbnail = await image.scale(400);
@ -209,6 +216,9 @@ export class RoomViewModel extends ViewModel {
this._room.createAttachment(thumbnail.blob, file.name); this._room.createAttachment(thumbnail.blob, file.name);
} }
await this._room.sendEvent("m.room.message", content, attachments); await this._room.sendEvent("m.room.message", content, attachments);
} catch (err) {
console.error(err);
}
} }
@ -238,11 +248,11 @@ class ComposerViewModel extends ViewModel {
} }
sendPicture() { sendPicture() {
this._roomVM._sendPicture(); this._roomVM._pickAndSendPicture();
} }
sendFile() { sendFile() {
this._roomVM._sendFile(); this._roomVM._pickAndSendFile();
} }
get canSend() { get canSend() {

View file

@ -159,7 +159,7 @@ export class Platform {
if (file) { if (file) {
resolve({name: file.name, blob: BlobHandle.fromBlob(file)}); resolve({name: file.name, blob: BlobHandle.fromBlob(file)});
} else { } else {
reject(new Error("No file selected")); resolve();
} }
} }
input.addEventListener("change", checkFile, true); input.addEventListener("change", checkFile, true);