mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-07-09 00:45:05 +02:00
BinaryManager support for libraries and downloading from github release assets
This commit is contained in:
parent
ee53086444
commit
329e9c9eb2
4 changed files with 574 additions and 639 deletions
|
@ -2,25 +2,267 @@ const child_process = require('child_process')
|
|||
const { promisify } = require('util')
|
||||
const exec = promisify(child_process.exec)
|
||||
const path = require('path')
|
||||
const axios = require('axios')
|
||||
const which = require('../libs/which')
|
||||
const fs = require('../libs/fsExtra')
|
||||
const ffbinaries = require('../libs/ffbinaries')
|
||||
const Logger = require('../Logger')
|
||||
const fileUtils = require('../utils/fileUtils')
|
||||
const StreamZip = require('../libs/nodeStreamZip')
|
||||
|
||||
class GithubAssetDownloader {
|
||||
constructor(owner, repo) {
|
||||
this.owner = owner
|
||||
this.repo = repo
|
||||
this.assetCache = {}
|
||||
}
|
||||
|
||||
async getAssetUrl(releaseTag, assetName) {
|
||||
// Check if the assets information is already cached for the release tag
|
||||
if (this.assetCache[releaseTag]) {
|
||||
Logger.debug(`[GithubAssetDownloader] Repo ${this.repo} release ${releaseTag}: assets found in cache.`)
|
||||
} else {
|
||||
// Get the release information
|
||||
const releaseUrl = `https://api.github.com/repos/${this.owner}/${this.repo}/releases/tags/${releaseTag}`
|
||||
const releaseResponse = await axios.get(releaseUrl, {
|
||||
headers: {
|
||||
Accept: 'application/vnd.github.v3+json',
|
||||
'User-Agent': 'axios'
|
||||
}
|
||||
})
|
||||
|
||||
// Cache the assets information for the release tag
|
||||
this.assetCache[releaseTag] = releaseResponse.data.assets
|
||||
Logger.debug(`[GithubAssetDownloader] Repo ${this.repo} release ${releaseTag}: assets fetched from API.`)
|
||||
}
|
||||
|
||||
// Find the asset URL
|
||||
const assets = this.assetCache[releaseTag]
|
||||
const asset = assets.find((asset) => asset.name === assetName)
|
||||
if (!asset) {
|
||||
throw new Error(`[GithubAssetDownloader] Repo ${this.repo} release ${releaseTag}: asset ${assetName} not found`)
|
||||
}
|
||||
|
||||
return asset.browser_download_url
|
||||
}
|
||||
|
||||
async downloadAsset(assetUrl, destDir) {
|
||||
const zipPath = path.join(destDir, 'temp.zip')
|
||||
const writer = fs.createWriteStream(zipPath)
|
||||
|
||||
const assetResponse = await axios({
|
||||
url: assetUrl,
|
||||
method: 'GET',
|
||||
responseType: 'stream'
|
||||
})
|
||||
|
||||
assetResponse.data.pipe(writer)
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
writer.on('finish', () => {
|
||||
Logger.debug(`[GithubAssetDownloader] Downloaded asset ${assetUrl} to ${zipPath}`)
|
||||
resolve()
|
||||
})
|
||||
writer.on('error', (err) => {
|
||||
Logger.error(`[GithubAssetDownloader] Error downloading asset ${assetUrl}: ${err.message}`)
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
|
||||
return zipPath
|
||||
}
|
||||
|
||||
async extractFiles(zipPath, filesToExtract, destDir) {
|
||||
const zip = new StreamZip.async({ file: zipPath })
|
||||
|
||||
for (const file of filesToExtract) {
|
||||
const outputPath = path.join(destDir, file.outputFileName)
|
||||
await zip.extract(file.pathInsideZip, outputPath)
|
||||
Logger.debug(`[GithubAssetDownloader] Extracted file ${file.pathInsideZip} to ${outputPath}`)
|
||||
}
|
||||
|
||||
await zip.close()
|
||||
}
|
||||
|
||||
async downloadAndExtractFiles(releaseTag, assetName, filesToExtract, destDir) {
|
||||
let zipPath
|
||||
try {
|
||||
await fs.ensureDir(destDir)
|
||||
const assetUrl = await this.getAssetUrl(releaseTag, assetName)
|
||||
zipPath = await this.downloadAsset(assetUrl, destDir)
|
||||
await this.extractFiles(zipPath, filesToExtract, destDir)
|
||||
} catch (error) {
|
||||
Logger.error(`[GithubAssetDownloader] Error downloading or extracting files: ${error.message}`)
|
||||
throw error
|
||||
} finally {
|
||||
if (zipPath) await fs.remove(zipPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class FFBinariesDownloader extends GithubAssetDownloader {
|
||||
constructor() {
|
||||
super('ffbinaries', 'ffbinaries-prebuilt')
|
||||
}
|
||||
|
||||
getPlatformSuffix() {
|
||||
const platform = process.platform
|
||||
const arch = process.arch
|
||||
|
||||
switch (platform) {
|
||||
case 'win32':
|
||||
return 'win-64'
|
||||
case 'darwin':
|
||||
return 'macos-64'
|
||||
case 'linux':
|
||||
switch (arch) {
|
||||
case 'x64':
|
||||
return 'linux-64'
|
||||
case 'x32':
|
||||
case 'ia32':
|
||||
return 'linux-32'
|
||||
case 'arm64':
|
||||
return 'linux-arm-64'
|
||||
case 'arm':
|
||||
return 'linux-armhf-32'
|
||||
default:
|
||||
throw new Error(`Unsupported architecture: ${arch}`)
|
||||
}
|
||||
default:
|
||||
throw new Error(`Unsupported platform: ${platform}`)
|
||||
}
|
||||
}
|
||||
|
||||
async downloadBinary(binaryName, releaseTag, destDir) {
|
||||
const platformSuffix = this.getPlatformSuffix()
|
||||
const assetName = `${binaryName}-${releaseTag}-${platformSuffix}.zip`
|
||||
const fileName = process.platform === 'win32' ? `${binaryName}.exe` : binaryName
|
||||
const filesToExtract = [{ pathInsideZip: fileName, outputFileName: fileName }]
|
||||
releaseTag = `v${releaseTag}`
|
||||
|
||||
await this.downloadAndExtractFiles(releaseTag, assetName, filesToExtract, destDir)
|
||||
}
|
||||
}
|
||||
|
||||
class SQLeanDownloader extends GithubAssetDownloader {
|
||||
constructor() {
|
||||
super('nalgeon', 'sqlean')
|
||||
}
|
||||
|
||||
getPlatformSuffix() {
|
||||
const platform = process.platform
|
||||
const arch = process.arch
|
||||
|
||||
switch (platform) {
|
||||
case 'win32':
|
||||
return arch === 'x64' ? 'win-x64' : 'win-x86'
|
||||
case 'darwin':
|
||||
return arch === 'arm64' ? 'macos-arm64' : 'macos-x86'
|
||||
case 'linux':
|
||||
return arch === 'arm64' ? 'linux-arm64' : 'linux-x86'
|
||||
default:
|
||||
throw new Error(`Unsupported platform or architecture: ${platform}, ${arch}`)
|
||||
}
|
||||
}
|
||||
|
||||
getLibraryName(binaryName) {
|
||||
const platform = process.platform
|
||||
|
||||
switch (platform) {
|
||||
case 'win32':
|
||||
return `${binaryName}.dll`
|
||||
case 'darwin':
|
||||
return `${binaryName}.dylib`
|
||||
case 'linux':
|
||||
return `${binaryName}.so`
|
||||
default:
|
||||
throw new Error(`Unsupported platform: ${platform}`)
|
||||
}
|
||||
}
|
||||
|
||||
async downloadBinary(binaryName, releaseTag, destDir) {
|
||||
const platformSuffix = this.getPlatformSuffix()
|
||||
const assetName = `sqlean-${platformSuffix}.zip`
|
||||
const fileName = this.getLibraryName(binaryName)
|
||||
const filesToExtract = [{ pathInsideZip: fileName, outputFileName: fileName }]
|
||||
|
||||
await this.downloadAndExtractFiles(releaseTag, assetName, filesToExtract, destDir)
|
||||
}
|
||||
}
|
||||
|
||||
class Binary {
|
||||
constructor(name, type, envVariable, validVersions, source) {
|
||||
this.name = name
|
||||
this.type = type
|
||||
this.envVariable = envVariable
|
||||
this.validVersions = validVersions
|
||||
this.source = source
|
||||
this.fileName = this.getFileName()
|
||||
this.exec = exec
|
||||
}
|
||||
|
||||
async find(mainInstallDir, altInstallDir) {
|
||||
// 1. check path specified in environment variable
|
||||
const defaultPath = process.env[this.envVariable]
|
||||
if (await this.isGood(defaultPath)) return defaultPath
|
||||
// 2. find the first instance of the binary in the PATH environment variable
|
||||
if (this.type === 'executable') {
|
||||
const whichPath = which.sync(this.fileName, { nothrow: true })
|
||||
if (await this.isGood(whichPath)) return whichPath
|
||||
}
|
||||
// 3. check main install path (binary root dir)
|
||||
const mainInstallPath = path.join(mainInstallDir, this.fileName)
|
||||
if (await this.isGood(mainInstallPath)) return mainInstallPath
|
||||
// 4. check alt install path (/config)
|
||||
const altInstallPath = path.join(altInstallDir, this.fileName)
|
||||
if (await this.isGood(altInstallPath)) return altInstallPath
|
||||
return null
|
||||
}
|
||||
|
||||
getFileName() {
|
||||
if (this.type === 'executable') {
|
||||
return this.name + (process.platform == 'win32' ? '.exe' : '')
|
||||
} else if (this.type === 'library') {
|
||||
return this.name + (process.platform == 'win32' ? '.dll' : '.so')
|
||||
} else {
|
||||
return this.name
|
||||
}
|
||||
}
|
||||
|
||||
async isGood(binaryPath) {
|
||||
if (!binaryPath || !(await fs.pathExists(binaryPath))) return false
|
||||
if (!this.validVersions.length) return true
|
||||
if (this.type === 'library') return true
|
||||
try {
|
||||
const { stdout } = await this.exec('"' + binaryPath + '"' + ' -version')
|
||||
const version = stdout.match(/version\s([\d\.]+)/)?.[1]
|
||||
if (!version) return false
|
||||
return this.validVersions.some((validVersion) => version.startsWith(validVersion))
|
||||
} catch (err) {
|
||||
Logger.error(`[Binary] Failed to check version of ${binaryPath}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
async download(destination) {
|
||||
await this.source.downloadBinary(this.name, this.validVersions[0], destination)
|
||||
}
|
||||
}
|
||||
|
||||
const ffbinaries = new FFBinariesDownloader()
|
||||
const sqlean = new SQLeanDownloader()
|
||||
|
||||
class BinaryManager {
|
||||
|
||||
defaultRequiredBinaries = [
|
||||
{ name: 'ffmpeg', envVariable: 'FFMPEG_PATH', validVersions: ['5.1'] },
|
||||
{ name: 'ffprobe', envVariable: 'FFPROBE_PATH', validVersions: ['5.1'] }
|
||||
new Binary('ffmpeg', 'executable', 'FFMPEG_PATH', ['5.1'], ffbinaries), // ffmpeg executable
|
||||
new Binary('ffprobe', 'executable', 'FFPROBE_PATH', ['5.1'], ffbinaries), // ffprobe executable
|
||||
new Binary('unicode', 'library', 'SQLEAN_UNICODE_PATH', ['0.24.2'], sqlean) // sqlean unicode extension
|
||||
]
|
||||
|
||||
constructor(requiredBinaries = this.defaultRequiredBinaries) {
|
||||
this.requiredBinaries = requiredBinaries
|
||||
this.mainInstallPath = process.pkg ? path.dirname(process.execPath) : global.appRoot
|
||||
this.altInstallPath = global.ConfigPath
|
||||
this.mainInstallDir = process.pkg ? path.dirname(process.execPath) : global.appRoot
|
||||
this.altInstallDir = global.ConfigPath
|
||||
this.initialized = false
|
||||
this.exec = exec
|
||||
}
|
||||
|
||||
async init() {
|
||||
|
@ -44,36 +286,30 @@ class BinaryManager {
|
|||
this.initialized = true
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove old/invalid binaries in main or alt install path
|
||||
*
|
||||
* @param {string[]} binaryNames
|
||||
*/
|
||||
async removeOldBinaries(binaryNames) {
|
||||
for (const binaryName of binaryNames) {
|
||||
const executable = this.getExecutableFileName(binaryName)
|
||||
const mainInstallPath = path.join(this.mainInstallPath, executable)
|
||||
if (await fs.pathExists(mainInstallPath)) {
|
||||
Logger.debug(`[BinaryManager] Removing old binary: ${mainInstallPath}`)
|
||||
await fs.remove(mainInstallPath)
|
||||
}
|
||||
const altInstallPath = path.join(this.altInstallPath, executable)
|
||||
if (await fs.pathExists(altInstallPath)) {
|
||||
Logger.debug(`[BinaryManager] Removing old binary: ${altInstallPath}`)
|
||||
await fs.remove(altInstallPath)
|
||||
}
|
||||
async removeBinary(destination, binary) {
|
||||
const binaryPath = path.join(destination, binary.fileName)
|
||||
if (await fs.pathExists(binaryPath)) {
|
||||
Logger.debug(`[BinaryManager] Removing binary: ${binaryPath}`)
|
||||
await fs.remove(binaryPath)
|
||||
}
|
||||
}
|
||||
|
||||
async removeOldBinaries(binaries) {
|
||||
for (const binary of binaries) {
|
||||
await this.removeBinary(this.mainInstallDir, binary)
|
||||
await this.removeBinary(this.altInstallDir, binary)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find required binaries and return array of binary names that are missing
|
||||
*
|
||||
*
|
||||
* @returns {Promise<string[]>}
|
||||
*/
|
||||
async findRequiredBinaries() {
|
||||
const missingBinaries = []
|
||||
for (const binary of this.requiredBinaries) {
|
||||
const binaryPath = await this.findBinary(binary.name, binary.envVariable, binary.validVersions)
|
||||
const binaryPath = await binary.find(this.mainInstallDir, this.altInstallDir)
|
||||
if (binaryPath) {
|
||||
Logger.info(`[BinaryManager] Found valid binary ${binary.name} at ${binaryPath}`)
|
||||
if (process.env[binary.envVariable] !== binaryPath) {
|
||||
|
@ -82,79 +318,22 @@ class BinaryManager {
|
|||
}
|
||||
} else {
|
||||
Logger.info(`[BinaryManager] ${binary.name} not found or version too old`)
|
||||
missingBinaries.push(binary.name)
|
||||
missingBinaries.push(binary)
|
||||
}
|
||||
}
|
||||
return missingBinaries
|
||||
}
|
||||
|
||||
/**
|
||||
* Find absolute path for binary
|
||||
*
|
||||
* @param {string} name
|
||||
* @param {string} envVariable
|
||||
* @param {string[]} [validVersions]
|
||||
* @returns {Promise<string>} Path to binary
|
||||
*/
|
||||
async findBinary(name, envVariable, validVersions = []) {
|
||||
const executable = this.getExecutableFileName(name)
|
||||
// 1. check path specified in environment variable
|
||||
const defaultPath = process.env[envVariable]
|
||||
if (await this.isBinaryGood(defaultPath, validVersions)) return defaultPath
|
||||
// 2. find the first instance of the binary in the PATH environment variable
|
||||
const whichPath = which.sync(executable, { nothrow: true })
|
||||
if (await this.isBinaryGood(whichPath, validVersions)) return whichPath
|
||||
// 3. check main install path (binary root dir)
|
||||
const mainInstallPath = path.join(this.mainInstallPath, executable)
|
||||
if (await this.isBinaryGood(mainInstallPath, validVersions)) return mainInstallPath
|
||||
// 4. check alt install path (/config)
|
||||
const altInstallPath = path.join(this.altInstallPath, executable)
|
||||
if (await this.isBinaryGood(altInstallPath, validVersions)) return altInstallPath
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Check binary path exists and optionally check version is valid
|
||||
*
|
||||
* @param {string} binaryPath
|
||||
* @param {string[]} [validVersions]
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
async isBinaryGood(binaryPath, validVersions = []) {
|
||||
if (!binaryPath || !await fs.pathExists(binaryPath)) return false
|
||||
if (!validVersions.length) return true
|
||||
try {
|
||||
const { stdout } = await this.exec('"' + binaryPath + '"' + ' -version')
|
||||
const version = stdout.match(/version\s([\d\.]+)/)?.[1]
|
||||
if (!version) return false
|
||||
return validVersions.some(validVersion => version.startsWith(validVersion))
|
||||
} catch (err) {
|
||||
Logger.error(`[BinaryManager] Failed to check version of ${binaryPath}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string[]} binaries
|
||||
*/
|
||||
async install(binaries) {
|
||||
if (!binaries.length) return
|
||||
Logger.info(`[BinaryManager] Installing binaries: ${binaries.join(', ')}`)
|
||||
let destination = await fileUtils.isWritable(this.mainInstallPath) ? this.mainInstallPath : this.altInstallPath
|
||||
await ffbinaries.downloadBinaries(binaries, { destination, version: '5.1', force: true })
|
||||
Logger.info(`[BinaryManager] Installing binaries: ${binaries.map((binary) => binary.name).join(', ')}`)
|
||||
let destination = (await fileUtils.isWritable(this.mainInstallDir)) ? this.mainInstallDir : this.altInstallDir
|
||||
for (const binary of binaries) {
|
||||
await binary.download(destination)
|
||||
}
|
||||
Logger.info(`[BinaryManager] Binaries installed to ${destination}`)
|
||||
}
|
||||
|
||||
/**
|
||||
* Append .exe to binary name for Windows
|
||||
*
|
||||
* @param {string} name
|
||||
* @returns {string}
|
||||
*/
|
||||
getExecutableFileName(name) {
|
||||
return name + (process.platform == 'win32' ? '.exe' : '')
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BinaryManager
|
||||
module.exports = BinaryManager
|
||||
module.exports.Binary = Binary // for testing
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue