-
-
Notifications
You must be signed in to change notification settings - Fork 325
/
index.js
103 lines (84 loc) · 3.57 KB
/
index.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
const fs = require('fs');
const path = require('path');
async function findValidateCache(asset, settings, cacheDirectory, ttl){
if (asset.src.startsWith('file://')) {
settings.logger.log(`> Skipping cache for ${asset.src}; local file protocol is being used`);
return;
}
const fileName = path.basename(asset.src);
const maybeCachedFileLocation = path.join(cacheDirectory, fileName);
if (!fs.existsSync(maybeCachedFileLocation)) {
settings.logger.log(`> Cached file not found at ${maybeCachedFileLocation}`);
return;
}
if (ttl) {
const birthtime = fs.statSync(maybeCachedFileLocation).birthtimeMs;
if (Date.now() - birthtime > ttl) {
settings.logger.log(`> Cached file expired at ${maybeCachedFileLocation}`);
settings.logger.log(`> Deleting cache at ${maybeCachedFileLocation}`);
fs.unlinkSync(maybeCachedFileLocation);
return;
}
}
settings.logger.log(`> Cached file found at ${maybeCachedFileLocation}`);
settings.logger.log(`> Old source: ${asset.src}`);
asset.src = `file://${maybeCachedFileLocation}`;
settings.logger.log(`> New source: ${asset.src}`);
}
const predownload = async (job, settings, { cacheDirectory, ttl, cacheAssets }) => {
// Job template
await findValidateCache(job.template, settings, cacheDirectory, ttl);
if(cacheAssets){
// Job assets
for(const asset of job.assets){
// Only asset types that can be downloaded files
if(['image', 'audio', 'video', 'script', 'static'].includes(asset.type)){
await findValidateCache(asset, settings, cacheDirectory, ttl);
}
}
}
}
async function saveCache(asset, settings, workpath, cacheDirectory){
if (asset.src.startsWith('file://')) {
settings.logger.log(`> Skipping cache for ${asset.src}; local file protocol is being used`);
return;
}
if (!fs.existsSync(cacheDirectory)) {
settings.logger.log(`> Creating cache directory at ${cacheDirectory}`);
fs.mkdirSync(cacheDirectory);
}
const fileName = path.basename(asset.src);
const from = asset.dest ? asset.dest: path.join(workpath, fileName);
const to = path.join(cacheDirectory, fileName);
settings.logger.log(`> Copying from ${from} to ${to}`);
fs.copyFileSync(from, to);
}
const postdownload = async (job, settings, { cacheDirectory, cacheAssets }) => {
// Job template
await saveCache(job.template, settings, job.workpath, cacheDirectory);
if(cacheAssets){
// Job assets
for(const asset of job.assets){
// Only asset types that can be downloaded files
if(['image', 'audio', 'video', 'script', 'static'].includes(asset.type)){
await saveCache(asset, settings, job.workpath, cacheDirectory);
}
}
}
}
module.exports = (job, settings, { cacheDirectory, ttl, cacheAssets }, type) => {
if (!cacheDirectory) {
throw new Error(`cacheDirectory not provided.`);
}
cacheDirectory = cacheDirectory.replace('~', require('os').homedir());
if (fs.existsSync(cacheDirectory) && !fs.lstatSync(cacheDirectory).isDirectory()) {
throw new Error(`Cache path of ${cacheDirectory} exists but is not a directory, stopping`);
}
if (type === 'predownload') {
return predownload(job, settings, { cacheDirectory, ttl, cacheAssets }, type);
}
if (type === 'postdownload') {
return postdownload(job, settings, { cacheDirectory, cacheAssets }, type);
}
return Promise.resolve();
}