2020-11-11 15:16:20 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {encryptAttachment} from "../e2ee/attachment.js";
|
2020-11-13 21:49:19 +05:30
|
|
|
import {createEnum} from "../../utils/enum.js";
|
|
|
|
import {ObservableValue} from "../../observable/ObservableValue.js";
|
|
|
|
import {AbortError} from "../../utils/error.js";
|
|
|
|
|
|
|
|
export const UploadStatus = createEnum("Waiting", "Encrypting", "Uploading", "Uploaded", "Error");
|
2020-11-11 15:16:20 +05:30
|
|
|
|
|
|
|
export class AttachmentUpload {
|
|
|
|
constructor({filename, blob, hsApi, platform, isEncrypted}) {
|
|
|
|
this._filename = filename;
|
|
|
|
this._unencryptedBlob = blob;
|
|
|
|
this._isEncrypted = isEncrypted;
|
|
|
|
this._platform = platform;
|
|
|
|
this._hsApi = hsApi;
|
|
|
|
this._mxcUrl = null;
|
|
|
|
this._transferredBlob = null;
|
|
|
|
this._encryptionInfo = null;
|
|
|
|
this._uploadRequest = null;
|
|
|
|
this._aborted = false;
|
2020-11-11 16:21:11 +05:30
|
|
|
this._error = null;
|
2020-11-13 21:49:19 +05:30
|
|
|
this._status = new ObservableValue(UploadStatus.Waiting);
|
|
|
|
}
|
|
|
|
|
|
|
|
get status() {
|
|
|
|
return this._status;
|
2020-11-11 15:16:20 +05:30
|
|
|
}
|
|
|
|
|
2020-11-13 21:49:19 +05:30
|
|
|
async upload() {
|
|
|
|
if (this._status.get() === UploadStatus.Waiting) {
|
|
|
|
this._upload();
|
|
|
|
}
|
|
|
|
await this._status.waitFor(s => s === UploadStatus.Error || s === UploadStatus.Uploaded).promise;
|
|
|
|
if (this._status.get() === UploadStatus.Error) {
|
|
|
|
throw this._error;
|
2020-11-11 15:16:20 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 21:49:19 +05:30
|
|
|
/** @package */
|
2020-11-11 15:16:20 +05:30
|
|
|
async _upload() {
|
2020-11-11 16:21:11 +05:30
|
|
|
try {
|
|
|
|
let transferredBlob = this._unencryptedBlob;
|
|
|
|
if (this._isEncrypted) {
|
2020-11-13 21:49:19 +05:30
|
|
|
this._status.set(UploadStatus.Encrypting);
|
2020-11-11 16:21:11 +05:30
|
|
|
const {info, blob} = await encryptAttachment(this._platform, this._unencryptedBlob);
|
|
|
|
transferredBlob = blob;
|
|
|
|
this._encryptionInfo = info;
|
|
|
|
}
|
|
|
|
if (this._aborted) {
|
2020-11-13 21:49:19 +05:30
|
|
|
throw new AbortError("upload aborted during encryption");
|
2020-11-11 16:21:11 +05:30
|
|
|
}
|
2020-11-13 21:49:19 +05:30
|
|
|
this._status.set(UploadStatus.Uploading);
|
2020-11-11 16:21:11 +05:30
|
|
|
this._uploadRequest = this._hsApi.uploadAttachment(transferredBlob, this._filename);
|
|
|
|
const {content_uri} = await this._uploadRequest.response();
|
|
|
|
this._mxcUrl = content_uri;
|
|
|
|
this._transferredBlob = transferredBlob;
|
2020-11-13 21:49:19 +05:30
|
|
|
this._status.set(UploadStatus.Uploaded);
|
2020-11-11 16:21:11 +05:30
|
|
|
} catch (err) {
|
|
|
|
this._error = err;
|
2020-11-13 21:49:19 +05:30
|
|
|
this._status.set(UploadStatus.Error);
|
2020-11-11 15:16:20 +05:30
|
|
|
}
|
2020-11-11 16:21:11 +05:30
|
|
|
}
|
|
|
|
|
2020-11-11 15:16:20 +05:30
|
|
|
/** @public */
|
|
|
|
abort() {
|
|
|
|
this._aborted = true;
|
|
|
|
this._uploadRequest?.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @public */
|
|
|
|
get localPreview() {
|
|
|
|
return this._unencryptedBlob;
|
|
|
|
}
|
|
|
|
|
2020-11-11 16:21:11 +05:30
|
|
|
get error() {
|
|
|
|
return this._error;
|
|
|
|
}
|
|
|
|
|
2020-11-11 15:16:20 +05:30
|
|
|
/** @package */
|
2020-11-13 21:49:19 +05:30
|
|
|
applyToContent(urlPath, content) {
|
2020-11-11 15:16:20 +05:30
|
|
|
if (!this._mxcUrl) {
|
|
|
|
throw new Error("upload has not finished");
|
|
|
|
}
|
2020-11-13 21:49:19 +05:30
|
|
|
let prefix = urlPath.substr(0, urlPath.lastIndexOf("url"));
|
|
|
|
setPath(`${prefix}info.size`, content, this._transferredBlob.size);
|
|
|
|
setPath(`${prefix}info.mimetype`, content, this._unencryptedBlob.mimeType);
|
2020-11-11 15:16:20 +05:30
|
|
|
if (this._isEncrypted) {
|
2020-11-13 21:49:19 +05:30
|
|
|
setPath(`${prefix}file`, content, Object.assign(this._encryptionInfo, {
|
2020-11-11 15:16:20 +05:30
|
|
|
mimetype: this._unencryptedBlob.mimeType,
|
|
|
|
url: this._mxcUrl
|
2020-11-13 21:49:19 +05:30
|
|
|
}));
|
2020-11-11 15:16:20 +05:30
|
|
|
} else {
|
2020-11-13 21:49:19 +05:30
|
|
|
setPath(`${prefix}url`, content, this._mxcUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setPath(path, content, value) {
|
|
|
|
const parts = path.split(".");
|
|
|
|
let obj = content;
|
|
|
|
for (let i = 0; i < (parts.length - 1); i += 1) {
|
|
|
|
const key = parts[i];
|
|
|
|
if (!obj[key]) {
|
|
|
|
obj[key] = {};
|
2020-11-11 15:16:20 +05:30
|
|
|
}
|
2020-11-13 21:49:19 +05:30
|
|
|
obj = obj[key];
|
2020-11-11 15:16:20 +05:30
|
|
|
}
|
2020-11-13 21:49:19 +05:30
|
|
|
const propKey = parts[parts.length - 1];
|
|
|
|
obj[propKey] = value;
|
2020-11-11 15:16:20 +05:30
|
|
|
}
|