@harivilasp/mediautil 0.1.1 → 0.1.2
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/bin/mediautil.js +21 -4
- package/package.json +1 -1
package/bin/mediautil.js
CHANGED
|
@@ -5,7 +5,7 @@ const os = require("os");
|
|
|
5
5
|
const path = require("path");
|
|
6
6
|
const { spawnSync } = require("child_process");
|
|
7
7
|
|
|
8
|
-
const version = "0.1.
|
|
8
|
+
const version = "0.1.2";
|
|
9
9
|
const repo = process.env.MEDIAUTIL_REPO || "harivilasp/mediautil";
|
|
10
10
|
const binDir = path.join(__dirname, "..", "vendor");
|
|
11
11
|
const binPath = path.join(binDir, process.platform === "win32" ? "mediautil.exe" : "mediautil");
|
|
@@ -24,17 +24,34 @@ function assetUrl() {
|
|
|
24
24
|
return `https://github.com/${repo}/releases/download/v${version}/mediautil-${t}.${suffix}`;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
function download(url, destination) {
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
function download(url, destination, redirects = 0) {
|
|
28
|
+
if (redirects > 5) {
|
|
29
|
+
return Promise.reject(new Error(`download failed: too many redirects ${url}`));
|
|
30
|
+
}
|
|
31
|
+
|
|
30
32
|
return new Promise((resolve, reject) => {
|
|
31
33
|
https.get(url, (response) => {
|
|
34
|
+
if ([301, 302, 303, 307, 308].includes(response.statusCode)) {
|
|
35
|
+
const location = response.headers.location;
|
|
36
|
+
if (!location) {
|
|
37
|
+
reject(new Error(`download failed: ${response.statusCode} missing location ${url}`));
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
response.resume();
|
|
41
|
+
download(new URL(location, url).toString(), destination, redirects + 1).then(resolve, reject);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
32
45
|
if (response.statusCode !== 200) {
|
|
33
46
|
reject(new Error(`download failed: ${response.statusCode} ${url}`));
|
|
34
47
|
return;
|
|
35
48
|
}
|
|
49
|
+
|
|
50
|
+
fs.mkdirSync(path.dirname(destination), { recursive: true });
|
|
51
|
+
const file = fs.createWriteStream(destination);
|
|
36
52
|
response.pipe(file);
|
|
37
53
|
file.on("finish", () => file.close(resolve));
|
|
54
|
+
file.on("error", reject);
|
|
38
55
|
}).on("error", reject);
|
|
39
56
|
});
|
|
40
57
|
}
|