/
AssetRemoteData.js
155 lines (141 loc) · 5.51 KB
/
AssetRemoteData.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Wire
* Copyright (C) 2018 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/
'use strict';
window.z = window.z || {};
window.z.assets = z.assets || {};
z.assets.AssetRemoteData = class AssetRemoteData {
/**
* Use either z.assets.AssetRemoteData.v2 or z.assets.AssetRemoteData.v3 to initialize.
* @param {Uint8Array} otrKey - Encryption key
* @param {Uint8Array} sha256 - Checksum
*/
constructor(otrKey, sha256) {
this.otrKey = otrKey;
this.sha256 = sha256;
this.downloadProgress = ko.observable();
this.cancelDownload = undefined;
this.generateUrl = undefined;
this.identifier = undefined;
this.logger = new z.util.Logger('z.assets.AssetRemoteData', z.config.LOGGER.OPTIONS);
}
/**
* Static initializer for v3 assets.
*
* @param {string} assetKey - ID to retrieve asset with
* @param {Uint8Array} [otrKey] - Encryption key
* @param {Uint8Array} [sha256] - Checksum
* @param {string} [assetToken] - Token data
* @param {boolean} [forceCaching=false] - Cache asset in ServiceWorker
* @returns {z.assets.AssetRemoteData} V3 asset remote data
*/
static v3(assetKey, otrKey, sha256, assetToken, forceCaching = false) {
const remoteData = new z.assets.AssetRemoteData(otrKey, sha256);
remoteData.generateUrl = () => wire.app.service.asset.generateAssetUrlV3(assetKey, assetToken, forceCaching);
remoteData.identifier = `${assetKey}`;
return remoteData;
}
/**
* Static initializer for v2 assets.
*
* @param {string} conversationId - ID of conversation
* @param {string} assetId - ID to retrieve asset with
* @param {Uint8Array} otrKey - Encryption key
* @param {Uint8Array} sha256 - Checksum
* @param {boolean} [forceCaching=false] - Cache asset in ServiceWorker
* @returns {z.assets.AssetRemoteData} V2 asset remote data
*/
static v2(conversationId, assetId, otrKey, sha256, forceCaching = false) {
const remoteData = new z.assets.AssetRemoteData(otrKey, sha256);
remoteData.generateUrl = () => wire.app.service.asset.generateAssetUrlV2(assetId, conversationId, forceCaching);
remoteData.identifier = `${conversationId}${assetId}`;
return remoteData;
}
/**
* Static initializer for v1 assets.
*
* @deprecated
* @param {string} conversationId - ID of conversation
* @param {string} assetId - ID to retrieve asset with
* @param {boolean} [forceCaching=false] - Cache asset in ServiceWorker
* @returns {z.assets.AssetRemoteData} V1 asset remote data
*/
static v1(conversationId, assetId, forceCaching = false) {
const remoteData = new z.assets.AssetRemoteData();
remoteData.generateUrl = () => wire.app.service.asset.generateAssetUrl(assetId, conversationId, forceCaching);
remoteData.identifier = `${conversationId}${assetId}`;
return remoteData;
}
/**
* Get object url for asset remote data. URLs are cached in memory.
* @returns {Promise<string>} Object URL for asset
*/
getObjectUrl() {
const objectUrl = z.assets.AssetURLCache.getUrl(this.identifier);
return objectUrl
? Promise.resolve(objectUrl)
: this.load().then(blob => {
if (blob) {
const url = window.URL.createObjectURL(blob);
return z.assets.AssetURLCache.setUrl(this.identifier, url);
}
});
}
/**
* Loads and decrypts stored asset
* @returns {Promise<Blob>} Resolves with the decrypted asset data
*/
load() {
let type;
return this._loadBuffer()
.then(({buffer, mimeType}) => {
type = mimeType;
const isEncryptedAsset = this.otrKey && this.sha256;
return isEncryptedAsset
? z.assets.AssetCrypto.decryptAesAsset(buffer, this.otrKey.buffer, this.sha256.buffer)
: buffer;
})
.then(plaintext => new Blob([new Uint8Array(plaintext)], {mime_type: type}))
.catch(error => {
const errorMessage = (error && error.message) || '';
const isAssetNotFound = errorMessage.endsWith(z.error.BackendClientError.STATUS_CODE.NOT_FOUND);
const isServerError = errorMessage.endsWith(z.error.BackendClientError.STATUS_CODE.INTERNAL_SERVER_ERROR);
const isExpectedError = isAssetNotFound || isServerError;
if (!isExpectedError) {
throw error;
}
});
}
_loadBuffer() {
return this.generateUrl()
.then(generatedUrl => {
return z.util.loadUrlBuffer(generatedUrl, xhr => {
xhr.onprogress = event => this.downloadProgress(Math.round((event.loaded / event.total) * 100));
this.cancelDownload = () => xhr.abort.call(xhr);
});
})
.catch(error => {
const isValidationUtilError = error instanceof z.util.ValidationUtilError;
const message = isValidationUtilError
? `Failed to validate an asset URL (_load_buffer): ${error.message}`
: `Failed to load asset: ${error.message || error}`;
this.logger.error(message);
throw error;
});
}
};