-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVideoCacheProvider.js
272 lines (239 loc) · 7.8 KB
/
VideoCacheProvider.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
'use strict';
const _ = require('lodash');
const RNFetchBlob = require('react-native-fetch-blob').default;
const {
fs
} = RNFetchBlob;
const baseCacheDir = fs.dirs.CacheDir + '/videosCacheDir';
const SHA1 = require("crypto-js/sha1");
const URL = require('url-parse');
const defaultHeaders = {};
const defaultVideoTypes = ['avi','asf', 'amv', 'flv', 'gif', 'mp4', 'mov', 'mpg', 'mkv', 'm4v', 'mpeg', 'm2v', 'mpv', 'mng', 'ogv', 'ogg', 'wmv', '3gp', 'webm' ];
const defaultResolveHeaders = _.constant(defaultHeaders);
const defaultOptions = {
useQueryParamsInCacheKey: false
};
const activeDownloads = {};
function serializeObjectKeys(obj) {
return _(obj)
.toPairs()
.sortBy(a => a[0])
.map(a => a[1])
.value();
}
function getQueryForCacheKey(url, useQueryParamsInCacheKey) {
if (_.isArray(useQueryParamsInCacheKey)) {
return serializeObjectKeys(_.pick(url.query, useQueryParamsInCacheKey));
}
if (useQueryParamsInCacheKey) {
return serializeObjectKeys(url.query);
}
return '';
}
function generateCacheKey(url, options) {
const parsedUrl = new URL(url, null, true);
const pathParts = parsedUrl.pathname.split('/');
// last path part is the file name
const fileName = pathParts.pop();
const filePath = pathParts.join('/');
const parts = fileName.split('.');
const fileType = parts.length > 1 ? _.toLower(parts.pop()) : '';
const type = defaultVideoTypes.includes(fileType) ? fileType : 'mp4';
const cacheable = filePath + fileName + type + getQueryForCacheKey(parsedUrl, options.useQueryParamsInCacheKey);
return SHA1(cacheable) + '.' + type;
}
function getCachePath(url, options) {
if (options.cacheGroup) {
return options.cacheGroup;
}
const {
host
} = new URL(url);
return host.replace(/[^a-z0-9]/gi, '').toLowerCase();
}
function getCachedVideoFilePath(url, options) {
const cachePath = getCachePath(url, options);
const cacheKey = generateCacheKey(url, options);
return `${baseCacheDir}/${cachePath}/${cacheKey}`;
}
function deleteFile(filePath) {
return fs.stat(filePath)
.then(res => res && res.type === 'file')
.then(exists => exists && fs.unlink(filePath))
.catch((err) => {
// swallow error to always resolve
});
}
function getDirPath(filePath) {
return _.initial(filePath.split('/')).join('/');
}
function ensurePath(dirPath) {
return fs.isDir(dirPath)
.then(exists =>
!exists && fs.mkdir(dirPath)
)
.catch(err => {
// swallow folder already exists errors
if (err.message.includes('folder already exists')) {
return;
}
throw err;
});
}
function downloadVideo(fromUrl, toFile, headers = {}) {
// use toFile as the key as is was created using the cacheKey
if (!_.has(activeDownloads, toFile)) {
// create an active download for this file
activeDownloads[toFile] = new Promise((resolve, reject) => {
RNFetchBlob
.config({path: toFile})
.fetch('GET', fromUrl, headers)
.then(res => {
if (Math.floor(res.respInfo.status / 100) !== 2) {
throw new Error('Failed to successfully download image');
}
resolve(toFile);
})
.catch(err => {
return deleteFile(toFile)
.then(() => reject(err));
})
.finally(() => {
// cleanup
delete activeDownloads[toFile];
});
});
}
return activeDownloads[toFile];
}
function createPrefetcer(list) {
const urls = _.clone(list);
return {
next() {
return urls.shift();
}
};
}
function runPrefetchTask(prefetcher, options) {
const url = prefetcher.next();
if (!url) {
return Promise.resolve();
}
// if url is cacheable - cache it
if (isCacheable(url)) {
// check cache
return getCachedVideoPath(url, options)
// if not found download
.catch(() => cacheVideo(url, options))
// allow prefetch task to fail without terminating other prefetch tasks
.catch(_.noop)
// then run next task
.then(() => runPrefetchTask(prefetcher, options));
}
// else get next
return runPrefetchTask(prefetcher, options);
}
function collectFilesInfo(basePath) {
return fs.stat(basePath)
.then((info) => {
if (info.type === 'file') {
return [info];
}
return fs.ls(basePath)
.then(files => {
const promises = _.map(files, file => {
return collectFilesInfo(`${basePath}/${file}`);
});
return Promise.all(promises);
});
})
.catch(err => {
return [];
});
}
function isCacheable(url) {
return _.isString(url) && (_.startsWith(url, 'http://') || _.startsWith(url, 'https://'));
}
function getCachedVideoPath(url, options = defaultOptions) {
const filePath = getCachedVideoFilePath(url, options);
return fs.stat(filePath)
.then(res => {
if (res.type !== 'file') {
// reject the promise if res is not a file
throw new Error('Failed to get image from cache');
}
if (!res.size) {
// something went wrong with the download, file size is 0, remove it
return deleteFile(filePath)
.then(() => {
throw new Error('Failed to get image from cache');
});
}
return filePath;
})
.catch(err => {
throw err;
})
}
function cacheVideo(url, options = defaultOptions, resolveHeaders = defaultResolveHeaders) {
const filePath = getCachedVideoFilePath(url, options);
const dirPath = getDirPath(filePath);
return ensurePath(dirPath)
.then(() => resolveHeaders())
.then(headers => downloadVideo(url, filePath, headers));
}
function deleteCachedVideo(url, options = defaultOptions) {
const filePath = getCachedVideoFilePath(url, options);
return deleteFile(filePath);
}
function cacheMultipleVideos(urls, options = defaultOptions) {
const prefetcher = createPrefetcer(urls);
const numberOfWorkers = urls.length;
const promises = _.times(numberOfWorkers, () =>
runPrefetchTask(prefetcher, options)
);
return Promise.all(promises);
}
function deleteMultipleCachedVideos(urls, options = defaultOptions) {
return _.reduce(urls, (p, url) =>
p.then(() => deleteCachedVideo(url, options)),
Promise.resolve()
);
}
function seedCache(local, url, options = defaultOptions) {
const filePath = getCachedVideoFilePath(url, options);
const dirPath = getDirPath(filePath);
return ensurePath(dirPath)
.then(() => fs.cp(local, filePath))
}
function clearCache() {
return fs.unlink(baseCacheDir)
.catch(() => {
// swallow exceptions if path doesn't exist
})
.then(() => ensurePath(baseCacheDir));
}
function getCacheInfo() {
return ensurePath(baseCacheDir)
.then(() => collectFilesInfo(baseCacheDir))
.then(cache => {
const files = _.flattenDeep(cache);
const size = _.sumBy(files, 'size');
return {
files,
size
};
});
}
module.exports = {
isCacheable,
getCachedVideoFilePath,
getCachedVideoPath,
cacheVideo,
deleteCachedVideo,
cacheMultipleVideos,
deleteMultipleCachedVideos,
clearCache,
seedCache,
getCacheInfo
};