Skip to content

Commit

Permalink
install script: download gzipped binary ⚡️
Browse files Browse the repository at this point in the history
closes #69
  • Loading branch information
derhuerst committed Mar 17, 2021
1 parent 73d0920 commit 47bda66
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ const {encode: encodeQuery} = require('querystring')
const {strictEqual} = require('assert')
const envPaths = require('env-paths')
const FileCache = require('@derhuerst/http-basic/lib/FileCache').default
const {extname} = require('path')
var ProgressBar = require("progress");
var request = require('@derhuerst/http-basic')
const {createGunzip} = require('zlib')
const {pipeline} = require('stream')
var ffmpegPath = require(".");
var pkg = require("./package");

Expand Down Expand Up @@ -71,6 +74,12 @@ cache.getCacheKey = (url) => {
return FileCache.prototype.getCacheKey(normalizeS3Url(url))
}

const isGzUrl = (url) => {
const path = new URL(url).pathname.split('/')
const filename = path[path.length - 1]
return filename && extname(filename) === '.gz'
}

const noop = () => {}
function downloadFile(url, destinationPath, progressCallback = noop) {
let fulfill, reject;
Expand Down Expand Up @@ -101,9 +110,19 @@ function downloadFile(url, destinationPath, progressCallback = noop) {
}

const file = fs.createWriteStream(destinationPath);
file.on("finish", () => fulfill());
file.on("error", error => reject(error));
response.body.pipe(file)
const streams = isGzUrl(url)
? [response.body, createGunzip(), file]
: [response.body, file]
pipeline(
...streams,
(err) => {
if (err) {
err.url = response.url
err.statusCode = response.statusCode
reject(err)
} else fulfill()
}
)

if (!response.fromCache && progressCallback) {
const cLength = response.headers["content-length"]
Expand Down Expand Up @@ -142,9 +161,11 @@ const releaseName = (
)
const arch = process.env.npm_config_arch || os.arch()
const platform = process.env.npm_config_platform || os.platform()
const downloadUrl = `https://github.com/eugeneware/ffmpeg-static/releases/download/${release}/${platform}-${arch}`
const readmeUrl = `${downloadUrl}.README`
const licenseUrl = `${downloadUrl}.LICENSE`

const baseUrl = `https://github.com/eugeneware/ffmpeg-static/releases/download/${release}`
const downloadUrl = `${baseUrl}/${platform}-${arch}.gz`
const readmeUrl = `${baseUrl}/${platform}-${arch}.README`
const licenseUrl = `${baseUrl}/${platform}-${arch}.LICENSE`

downloadFile(downloadUrl, ffmpegPath, onProgress)
.then(() => {
Expand Down

0 comments on commit 47bda66

Please sign in to comment.