@nexrender/core 1.60.10 → 1.60.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/tasks/download.js +58 -39
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexrender/core",
|
|
3
|
-
"version": "1.60.
|
|
3
|
+
"version": "1.60.12",
|
|
4
4
|
"main": "src/index.js",
|
|
5
5
|
"author": "Inlife",
|
|
6
6
|
"homepage": "https://www.nexrender.com",
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"publishConfig": {
|
|
44
44
|
"access": "public"
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "f9a3dd372b155160769bba0100eaf6a76f120935"
|
|
47
47
|
}
|
package/src/tasks/download.js
CHANGED
|
@@ -16,11 +16,19 @@ const download = (job, settings, asset) => {
|
|
|
16
16
|
|
|
17
17
|
settings.logger.log(`[${job.uid}] > Downloading asset ${asset.src}...`);
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const protocol = uri.protocol.replace(/:$/, '');
|
|
19
|
+
let uri;
|
|
20
|
+
let protocol;
|
|
22
21
|
let destName = '';
|
|
23
22
|
|
|
23
|
+
try {
|
|
24
|
+
// eslint-disable-next-line
|
|
25
|
+
uri = global.URL ? new URL(asset.src) : url.parse(asset.src)
|
|
26
|
+
protocol = uri.protocol.replace(/:$/, '');
|
|
27
|
+
} catch (error) {
|
|
28
|
+
settings.logger.log(`[download] error parsing asset ${asset.src}: ${error}`);
|
|
29
|
+
return Promise.reject(error);
|
|
30
|
+
}
|
|
31
|
+
|
|
24
32
|
/* if asset doesnt have a file name, make up a random one */
|
|
25
33
|
if (protocol === 'data' && !asset.layerName) {
|
|
26
34
|
destName = Math.random().toString(36).substring(2);
|
|
@@ -90,43 +98,49 @@ const download = (job, settings, asset) => {
|
|
|
90
98
|
|
|
91
99
|
/* TODO: maybe move to external package ?? */
|
|
92
100
|
const src = asset.src
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
return withTimeout(
|
|
104
|
+
fetch(src, {
|
|
105
|
+
...asset.params,
|
|
106
|
+
timeout: NEXRENDER_DOWNLOAD_TIMEOUT
|
|
107
|
+
})
|
|
108
|
+
.then(res => res.ok ? res : Promise.reject(new Error(`Unable to download file ${src}`)))
|
|
109
|
+
.then(res => {
|
|
110
|
+
// Set a file extension based on content-type header if not already set
|
|
111
|
+
if (!asset.extension) {
|
|
112
|
+
const contentType = res.headers.get('content-type')
|
|
113
|
+
const fileExt = mime.extension(contentType) || undefined
|
|
114
|
+
|
|
115
|
+
asset.extension = fileExt
|
|
116
|
+
const destHasExtension = path.extname(asset.dest) ? true : false
|
|
117
|
+
// don't do this if asset.dest already has extension else it gives you example.jpg.jpg
|
|
118
|
+
// like file in case of assets and aep/aepx file
|
|
119
|
+
if (asset.extension && !destHasExtension) {
|
|
120
|
+
asset.dest += `.${fileExt}`
|
|
121
|
+
}
|
|
111
122
|
}
|
|
112
|
-
}
|
|
113
123
|
|
|
114
|
-
|
|
124
|
+
const stream = fs.createWriteStream(asset.dest)
|
|
115
125
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
126
|
+
return withTimeout(new Promise((resolve, reject) => {
|
|
127
|
+
const errorHandler = (error) => {
|
|
128
|
+
reject(new Error('Unable to download file ' + asset.src + ' due to ' + error))
|
|
129
|
+
};
|
|
120
130
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
131
|
+
res.body
|
|
132
|
+
.on('error', errorHandler)
|
|
133
|
+
.pipe(stream)
|
|
124
134
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
135
|
+
stream
|
|
136
|
+
.on('error', errorHandler)
|
|
137
|
+
.on('finish', resolve)
|
|
138
|
+
}), NEXRENDER_DOWNLOAD_TIMEOUT, 'Download timed out for asset ' + asset.src)
|
|
128
139
|
}), NEXRENDER_DOWNLOAD_TIMEOUT, 'Download timed out for asset ' + asset.src)
|
|
129
|
-
|
|
140
|
+
} catch (error) {
|
|
141
|
+
settings.logger.log(`[download] error downloading asset ${asset.src}: ${error}`);
|
|
142
|
+
return Promise.reject(error);
|
|
143
|
+
}
|
|
130
144
|
|
|
131
145
|
case 'file':
|
|
132
146
|
const filepath = uri2path(expandEnvironmentVariables(asset.src))
|
|
@@ -175,10 +189,15 @@ const download = (job, settings, asset) => {
|
|
|
175
189
|
module.exports = function(job, settings) {
|
|
176
190
|
settings.logger.log(`[${job.uid}] downloading assets...`)
|
|
177
191
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
192
|
+
try {
|
|
193
|
+
const promises = [].concat(
|
|
194
|
+
download(job, settings, job.template),
|
|
195
|
+
job.assets.map(asset => download(job, settings, asset))
|
|
196
|
+
)
|
|
182
197
|
|
|
183
|
-
|
|
198
|
+
return Promise.all(promises).then(() => job);
|
|
199
|
+
} catch (error) {
|
|
200
|
+
settings.logger.log(`[download] error downloading assets: ${error}`);
|
|
201
|
+
return Promise.reject(error);
|
|
202
|
+
}
|
|
184
203
|
}
|