-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Asset.js
197 lines (197 loc) · 6.84 KB
/
Asset.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { Platform } from 'expo-core';
import * as FileSystem from 'expo-file-system';
import Constants from 'expo-constants';
import { getAssetByID } from 'react-native/Libraries/Image/AssetRegistry';
import resolveAssetSource, { setCustomSourceTransformer, } from 'react-native/Libraries/Image/resolveAssetSource';
import * as AssetSources from './AssetSources';
import * as AssetUris from './AssetUris';
import * as EmbeddedAssets from './EmbeddedAssets';
import * as ImageAssets from './ImageAssets';
const MANAGED_ENV = !!Constants.manifest;
export class Asset {
constructor({ name, type, hash = null, uri, width, height }) {
this.hash = null;
this.localUri = null;
this.width = null;
this.height = null;
this.downloading = false;
this.downloaded = false;
this._downloadCallbacks = [];
this.name = name;
this.type = type;
this.hash = hash;
this.uri = uri;
if (typeof width === 'number') {
this.width = width;
}
if (typeof height === 'number') {
this.height = height;
}
// This only applies to assets that are bundled in Expo standalone apps
if (MANAGED_ENV && hash) {
this.localUri = EmbeddedAssets.getEmbeddedAssetUri(hash, type);
if (this.localUri) {
this.downloaded = true;
}
}
}
static loadAsync(moduleId) {
const moduleIds = Array.isArray(moduleId) ? moduleId : [moduleId];
return Promise.all(moduleIds.map(moduleId => Asset.fromModule(moduleId).downloadAsync()));
}
static fromModule(virtualAssetModule) {
if (typeof virtualAssetModule === 'string') {
return Asset.fromURI(virtualAssetModule);
}
const meta = getAssetByID(virtualAssetModule);
if (!meta) {
throw new Error(`Module "${virtualAssetModule}" is missing from the asset registry`);
}
// Outside of the managed env we need the moduleId to initialize the asset
// because resolveAssetSource depends on it
if (!MANAGED_ENV) {
const { uri } = resolveAssetSource(virtualAssetModule);
const asset = new Asset({
name: meta.name,
type: meta.type,
hash: meta.hash,
uri,
width: meta.width,
height: meta.height,
});
Asset.byHash[meta.hash] = asset;
return asset;
}
return Asset.fromMetadata(meta);
}
static fromMetadata(meta) {
// The hash of the whole asset, not to be confused with the hash of a specific file returned
// from `selectAssetSource`
const metaHash = meta.hash;
if (Asset.byHash[metaHash]) {
return Asset.byHash[metaHash];
}
else if (!MANAGED_ENV && !Asset.byHash[metaHash]) {
throw new Error('Assets must be initialized with Asset.fromModule');
}
const { uri, hash } = AssetSources.selectAssetSource(meta);
const asset = new Asset({
name: meta.name,
type: meta.type,
hash,
uri,
width: meta.width,
height: meta.height,
});
Asset.byHash[metaHash] = asset;
return asset;
}
static fromURI(uri) {
if (Asset.byUri[uri]) {
return Asset.byUri[uri];
}
// Possibly a Base64-encoded URI
let type = '';
if (uri.indexOf(';base64') > -1) {
type = uri.split(';')[0].split('/')[1];
}
else {
const extension = AssetUris.getFileExtension(uri);
type = extension.startsWith('.') ? extension.substring(1) : extension;
}
const asset = new Asset({
name: '',
type,
uri,
});
Asset.byUri[uri] = asset;
return asset;
}
async _downloadAsyncWeb() {
if (ImageAssets.isImageType(this.type)) {
const { width, height, name } = await ImageAssets.getImageInfoAsync(this.uri);
this.width = width;
this.height = height;
this.name = name;
}
else {
this.name = AssetUris.getFilename(this.uri);
}
this.localUri = this.uri;
}
async _downloadAsyncManagedEnv() {
const localUri = `${FileSystem.cacheDirectory}ExponentAsset-${this.hash}.${this.type}`;
let { exists, md5 } = await FileSystem.getInfoAsync(localUri, {
md5: true,
});
if (!exists || md5 !== this.hash) {
({ md5 } = await FileSystem.downloadAsync(this.uri, localUri, {
md5: true,
}));
if (md5 !== this.hash) {
throw new Error(`Downloaded file for asset '${this.name}.${this.type}' ` +
`Located at ${this.uri} ` +
`failed MD5 integrity check`);
}
}
this.localUri = localUri;
}
async _downloadAsyncUnmanagedEnv() {
// Bail out if it was bundled with the app
if (this.uri.startsWith('file://')) {
this.localUri = this.uri;
return;
}
const localUri = `${FileSystem.cacheDirectory}ExponentAsset-${this.hash}.${this.type}`;
// We don't check the FileSystem for an existing version of the asset and we
// also don't perform an integrity check!
await FileSystem.downloadAsync(this.uri, localUri);
this.localUri = localUri;
}
async downloadAsync() {
if (this.downloaded) {
return;
}
if (this.downloading) {
await new Promise((resolve, reject) => {
this._downloadCallbacks.push({ resolve, reject });
});
return;
}
this.downloading = true;
try {
if (Platform.OS === 'web') {
await this._downloadAsyncWeb();
}
else if (MANAGED_ENV) {
await this._downloadAsyncManagedEnv();
}
else {
await this._downloadAsyncUnmanagedEnv();
}
this.downloaded = true;
this._downloadCallbacks.forEach(({ resolve }) => resolve());
}
catch (e) {
this._downloadCallbacks.forEach(({ reject }) => reject(e));
throw e;
}
finally {
this.downloading = false;
this._downloadCallbacks = [];
}
}
}
Asset.byHash = {};
Asset.byUri = {};
// Override React Native's asset resolution for `Image` components
setCustomSourceTransformer(resolver => {
try {
const asset = Asset.fromMetadata(resolver.asset);
return resolver.fromSource(asset.downloaded ? asset.localUri : asset.uri);
}
catch (e) {
return resolver.defaultAsset();
}
});
//# sourceMappingURL=Asset.js.map