better error handling for file and picture upload
This commit is contained in:
parent
47147f2d46
commit
f6dbb23f79
2 changed files with 42 additions and 32 deletions
|
@ -165,13 +165,19 @@ export class RoomViewModel extends ViewModel {
|
|||
return false;
|
||||
}
|
||||
|
||||
async _sendFile() {
|
||||
let file;
|
||||
async _pickAndSendFile() {
|
||||
try {
|
||||
file = await this.platform.openFile();
|
||||
} catch (err) {
|
||||
const file = await this.platform.openFile();
|
||||
if (!file) {
|
||||
return;
|
||||
}
|
||||
return this._sendFile(file);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
async _sendFile(file) {
|
||||
const content = {
|
||||
body: file.name,
|
||||
msgtype: "m.file"
|
||||
|
@ -179,20 +185,21 @@ export class RoomViewModel extends ViewModel {
|
|||
await this._room.sendEvent("m.room.message", content, {
|
||||
"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()) {
|
||||
alert("Please allow canvas image data access, so we can scale your images down.");
|
||||
return;
|
||||
}
|
||||
let file;
|
||||
try {
|
||||
file = await this.platform.openFile("image/*");
|
||||
} catch (err) {
|
||||
const file = await this.platform.openFile("image/*");
|
||||
if (!file) {
|
||||
return;
|
||||
}
|
||||
if (!file.blob.mimeType.startsWith("image/")) {
|
||||
return this._sendFile(file);
|
||||
}
|
||||
const image = await this.platform.loadImage(file.blob);
|
||||
const content = {
|
||||
body: file.name,
|
||||
|
@ -200,7 +207,7 @@ export class RoomViewModel extends ViewModel {
|
|||
info: imageToInfo(image)
|
||||
};
|
||||
const attachments = {
|
||||
"url": this._room.createAttachment(file.blob, file.name),
|
||||
"url": this._room.createAttachment(image.blob, file.name),
|
||||
};
|
||||
if (image.maxDimension > 600) {
|
||||
const thumbnail = await image.scale(400);
|
||||
|
@ -209,6 +216,9 @@ export class RoomViewModel extends ViewModel {
|
|||
this._room.createAttachment(thumbnail.blob, file.name);
|
||||
}
|
||||
await this._room.sendEvent("m.room.message", content, attachments);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -238,11 +248,11 @@ class ComposerViewModel extends ViewModel {
|
|||
}
|
||||
|
||||
sendPicture() {
|
||||
this._roomVM._sendPicture();
|
||||
this._roomVM._pickAndSendPicture();
|
||||
}
|
||||
|
||||
sendFile() {
|
||||
this._roomVM._sendFile();
|
||||
this._roomVM._pickAndSendFile();
|
||||
}
|
||||
|
||||
get canSend() {
|
||||
|
|
|
@ -159,7 +159,7 @@ export class Platform {
|
|||
if (file) {
|
||||
resolve({name: file.name, blob: BlobHandle.fromBlob(file)});
|
||||
} else {
|
||||
reject(new Error("No file selected"));
|
||||
resolve();
|
||||
}
|
||||
}
|
||||
input.addEventListener("change", checkFile, true);
|
||||
|
|
Reference in a new issue