sort file

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
pull/21833/head
Michael Telatynski 2020-05-24 16:00:57 +01:00
parent 0705883c8f
commit 4a08c8ceda
1 changed files with 50 additions and 50 deletions

View File

@ -360,38 +360,6 @@ export default class ContentMessages {
private inprogress: IUpload[] = []; private inprogress: IUpload[] = [];
private mediaConfig: IMediaConfig = null; private mediaConfig: IMediaConfig = null;
static sharedInstance() {
if (window.mx_ContentMessages === undefined) {
window.mx_ContentMessages = new ContentMessages();
}
return window.mx_ContentMessages;
}
_isFileSizeAcceptable(file: File) {
if (this.mediaConfig !== null &&
this.mediaConfig["m.upload.size"] !== undefined &&
file.size > this.mediaConfig["m.upload.size"]) {
return false;
}
return true;
}
_ensureMediaConfigFetched() {
if (this.mediaConfig !== null) return;
console.log("[Media Config] Fetching");
return MatrixClientPeg.get().getMediaConfig().then((config) => {
console.log("[Media Config] Fetched config:", config);
return config;
}).catch(() => {
// Media repo can't or won't report limits, so provide an empty object (no limits).
console.log("[Media Config] Could not fetch config, so not limiting uploads.");
return {};
}).then((config) => {
this.mediaConfig = config;
});
}
sendStickerContentToRoom(url: string, roomId: string, info: string, text: string, matrixClient: MatrixClient) { sendStickerContentToRoom(url: string, roomId: string, info: string, text: string, matrixClient: MatrixClient) {
return MatrixClientPeg.get().sendStickerMessage(roomId, url, info, text).catch((e) => { return MatrixClientPeg.get().sendStickerMessage(roomId, url, info, text).catch((e) => {
console.warn(`Failed to send content with URL ${url} to room ${roomId}`, e); console.warn(`Failed to send content with URL ${url} to room ${roomId}`, e);
@ -431,13 +399,13 @@ export default class ContentMessages {
if (!shouldUpload) return; if (!shouldUpload) return;
} }
await this._ensureMediaConfigFetched(); await this.ensureMediaConfigFetched();
const tooBigFiles = []; const tooBigFiles = [];
const okFiles = []; const okFiles = [];
for (let i = 0; i < files.length; ++i) { for (let i = 0; i < files.length; ++i) {
if (this._isFileSizeAcceptable(files[i])) { if (this.isFileSizeAcceptable(files[i])) {
okFiles.push(files[i]); okFiles.push(files[i]);
} else { } else {
tooBigFiles.push(files[i]); tooBigFiles.push(files[i]);
@ -474,11 +442,30 @@ export default class ContentMessages {
uploadAll = true; uploadAll = true;
} }
} }
promBefore = this._sendContentToRoom(file, roomId, matrixClient, promBefore); promBefore = this.sendContentToRoom(file, roomId, matrixClient, promBefore);
} }
} }
_sendContentToRoom(file: File, roomId: string, matrixClient: MatrixClient, promBefore: Promise<any>) { getCurrentUploads() {
return this.inprogress.filter(u => !u.canceled);
}
cancelUpload(promise: Promise<any>) {
let upload: IUpload;
for (let i = 0; i < this.inprogress.length; ++i) {
if (this.inprogress[i].promise === promise) {
upload = this.inprogress[i];
break;
}
}
if (upload) {
upload.canceled = true;
MatrixClientPeg.get().cancelUpload(upload.promise);
dis.dispatch({action: 'upload_canceled', upload});
}
}
private sendContentToRoom(file: File, roomId: string, matrixClient: MatrixClient, promBefore: Promise<any>) {
const content: IContent = { const content: IContent = {
body: file.name || 'Attachment', body: file.name || 'Attachment',
info: { info: {
@ -602,22 +589,35 @@ export default class ContentMessages {
}); });
} }
getCurrentUploads() { private isFileSizeAcceptable(file: File) {
return this.inprogress.filter(u => !u.canceled); if (this.mediaConfig !== null &&
this.mediaConfig["m.upload.size"] !== undefined &&
file.size > this.mediaConfig["m.upload.size"]) {
return false;
}
return true;
} }
cancelUpload(promise: Promise<any>) { private ensureMediaConfigFetched() {
let upload; if (this.mediaConfig !== null) return;
for (let i = 0; i < this.inprogress.length; ++i) {
if (this.inprogress[i].promise === promise) { console.log("[Media Config] Fetching");
upload = this.inprogress[i]; return MatrixClientPeg.get().getMediaConfig().then((config) => {
break; console.log("[Media Config] Fetched config:", config);
} return config;
} }).catch(() => {
if (upload) { // Media repo can't or won't report limits, so provide an empty object (no limits).
upload.canceled = true; console.log("[Media Config] Could not fetch config, so not limiting uploads.");
MatrixClientPeg.get().cancelUpload(upload.promise); return {};
dis.dispatch({action: 'upload_canceled', upload}); }).then((config) => {
this.mediaConfig = config;
});
}
static sharedInstance() {
if (window.mx_ContentMessages === undefined) {
window.mx_ContentMessages = new ContentMessages();
} }
return window.mx_ContentMessages;
} }
} }