2020-11-11 10:46:20 +01:00
|
|
|
/*
|
|
|
|
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";
|
|
|
|
|
|
|
|
export class AttachmentUpload {
|
2020-11-18 13:02:38 +01:00
|
|
|
constructor({filename, blob, platform}) {
|
2020-11-11 10:46:20 +01:00
|
|
|
this._filename = filename;
|
2020-11-18 13:02:38 +01:00
|
|
|
// need to keep around for local preview while uploading
|
2020-11-11 10:46:20 +01:00
|
|
|
this._unencryptedBlob = blob;
|
2020-11-18 13:02:38 +01:00
|
|
|
this._transferredBlob = this._unencryptedBlob;
|
2020-11-11 10:46:20 +01:00
|
|
|
this._platform = platform;
|
|
|
|
this._mxcUrl = null;
|
|
|
|
this._encryptionInfo = null;
|
|
|
|
this._uploadRequest = null;
|
|
|
|
this._aborted = false;
|
2020-11-11 11:51:11 +01:00
|
|
|
this._error = null;
|
2020-11-18 13:02:38 +01:00
|
|
|
this._sentBytes = 0;
|
2020-11-11 10:46:20 +01:00
|
|
|
}
|
|
|
|
|
2020-11-18 13:02:38 +01:00
|
|
|
/** important to call after encrypt() if encryption is needed */
|
|
|
|
get size() {
|
|
|
|
return this._transferredBlob.size;
|
2020-11-16 10:45:46 +01:00
|
|
|
}
|
|
|
|
|
2020-11-18 13:02:38 +01:00
|
|
|
get sentBytes() {
|
|
|
|
return this._sentBytes;
|
2020-11-11 11:51:11 +01:00
|
|
|
}
|
|
|
|
|
2020-11-11 10:46:20 +01:00
|
|
|
/** @public */
|
|
|
|
abort() {
|
|
|
|
this._uploadRequest?.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @public */
|
|
|
|
get localPreview() {
|
|
|
|
return this._unencryptedBlob;
|
|
|
|
}
|
|
|
|
|
2020-11-18 13:02:38 +01:00
|
|
|
/** @package */
|
|
|
|
async encrypt() {
|
|
|
|
if (this._encryptionInfo) {
|
|
|
|
throw new Error("already encrypted");
|
|
|
|
}
|
|
|
|
const {info, blob} = await encryptAttachment(this._platform, this._transferredBlob);
|
|
|
|
this._transferredBlob = blob;
|
|
|
|
this._encryptionInfo = info;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @package */
|
2021-02-23 19:22:59 +01:00
|
|
|
async upload(hsApi, progressCallback, log) {
|
2020-11-18 13:02:38 +01:00
|
|
|
this._uploadRequest = hsApi.uploadAttachment(this._transferredBlob, this._filename, {
|
|
|
|
uploadProgress: sentBytes => {
|
|
|
|
this._sentBytes = sentBytes;
|
|
|
|
progressCallback();
|
2021-02-23 19:22:59 +01:00
|
|
|
},
|
|
|
|
log
|
2020-11-18 13:02:38 +01:00
|
|
|
});
|
|
|
|
const {content_uri} = await this._uploadRequest.response();
|
|
|
|
this._mxcUrl = content_uri;
|
2020-11-11 11:51:11 +01:00
|
|
|
}
|
|
|
|
|
2020-11-11 10:46:20 +01:00
|
|
|
/** @package */
|
2020-11-13 17:19:19 +01:00
|
|
|
applyToContent(urlPath, content) {
|
2020-11-11 10:46:20 +01:00
|
|
|
if (!this._mxcUrl) {
|
|
|
|
throw new Error("upload has not finished");
|
|
|
|
}
|
2020-11-13 17:19:19 +01:00
|
|
|
let prefix = urlPath.substr(0, urlPath.lastIndexOf("url"));
|
|
|
|
setPath(`${prefix}info.size`, content, this._transferredBlob.size);
|
2020-11-13 19:22:06 +01:00
|
|
|
setPath(`${prefix}info.mimetype`, content, this._unencryptedBlob.mimeType);
|
2020-11-18 13:02:38 +01:00
|
|
|
if (this._encryptionInfo) {
|
2020-11-13 17:19:19 +01:00
|
|
|
setPath(`${prefix}file`, content, Object.assign(this._encryptionInfo, {
|
2020-11-13 19:22:06 +01:00
|
|
|
mimetype: this._unencryptedBlob.mimeType,
|
2020-11-11 10:46:20 +01:00
|
|
|
url: this._mxcUrl
|
2020-11-13 17:19:19 +01:00
|
|
|
}));
|
2020-11-11 10:46:20 +01:00
|
|
|
} else {
|
2020-11-13 17:19:19 +01:00
|
|
|
setPath(`${prefix}url`, content, this._mxcUrl);
|
|
|
|
}
|
|
|
|
}
|
2020-11-18 20:08:42 +01:00
|
|
|
|
|
|
|
dispose() {
|
|
|
|
this._unencryptedBlob.dispose();
|
|
|
|
this._transferredBlob.dispose();
|
|
|
|
}
|
2020-11-13 17:19:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 10:46:20 +01:00
|
|
|
}
|
2020-11-13 17:19:19 +01:00
|
|
|
obj = obj[key];
|
2020-11-11 10:46:20 +01:00
|
|
|
}
|
2020-11-13 17:19:19 +01:00
|
|
|
const propKey = parts[parts.length - 1];
|
|
|
|
obj[propKey] = value;
|
2020-11-11 10:46:20 +01:00
|
|
|
}
|