audiobookshelf/server/managers/BinaryManager.js

109 lines
4.2 KiB
JavaScript
Raw Normal View History

const child_process = require('child_process')
const { promisify } = require('util')
const exec = promisify(child_process.exec)
2023-12-05 20:19:17 +01:00
const path = require('path')
const which = require('../libs/which')
const fs = require('../libs/fsExtra')
2023-12-06 00:35:15 +01:00
const ffbinaries = require('../libs/ffbinaries')
2023-12-05 20:19:17 +01:00
const Logger = require('../Logger')
const fileUtils = require('../utils/fileUtils')
2023-12-05 20:19:17 +01:00
2023-12-06 00:35:15 +01:00
class BinaryManager {
2023-12-05 20:19:17 +01:00
2023-12-06 00:35:15 +01:00
defaultRequiredBinaries = [
{ name: 'ffmpeg', envVariable: 'FFMPEG_PATH' },
{ name: 'ffprobe', envVariable: 'FFPROBE_PATH' }
2023-12-05 20:19:17 +01:00
]
goodVersions = [ '5.1', '6' ]
2023-12-05 20:19:17 +01:00
constructor(requiredBinaries = this.defaultRequiredBinaries) {
this.requiredBinaries = requiredBinaries
this.mainInstallPath = process.pkg ? path.dirname(process.execPath) : global.appRoot
this.altInstallPath = global.ConfigPath
this.exec = exec
2023-12-05 20:19:17 +01:00
}
async init() {
if (this.initialized) return
const missingBinaries = await this.findRequiredBinaries()
if (missingBinaries.length == 0) return
await this.removeOldBinaries(missingBinaries)
2023-12-05 20:19:17 +01:00
await this.install(missingBinaries)
const missingBinariesAfterInstall = await this.findRequiredBinaries()
if (missingBinariesAfterInstall.length != 0) {
Logger.error(`[BinaryManager] Failed to find or install required binaries: ${missingBinariesAfterInstall.join(', ')}`)
process.exit(1)
}
this.initialized = true
}
async removeOldBinaries(binaryNames) {
for (const binaryName of binaryNames) {
const executable = this.getExecutableFileName(binaryName)
const mainInstallPath = path.join(this.mainInstallPath, executable)
const altInstallPath = path.join(this.altInstallPath, executable)
Logger.debug(`[BinaryManager] Removing old binaries: ${mainInstallPath}, ${altInstallPath}`)
await fs.remove(mainInstallPath)
await fs.remove(altInstallPath)
}
}
2023-12-05 20:19:17 +01:00
async findRequiredBinaries() {
const missingBinaries = []
for (const binary of this.requiredBinaries) {
const binaryPath = await this.findBinary(binary.name, binary.envVariable)
if (binaryPath) {
Logger.info(`[BinaryManager] Found good ${binary.name} at ${binaryPath}`)
if (process.env[binary.envVariable] !== binaryPath) {
Logger.info(`[BinaryManager] Updating process.env.${binary.envVariable}`)
process.env[binary.envVariable] = binaryPath
}
2023-12-05 20:19:17 +01:00
} else {
Logger.info(`[BinaryManager] ${binary.name} not found or version too old`)
2023-12-05 20:19:17 +01:00
missingBinaries.push(binary.name)
}
}
return missingBinaries
}
async findBinary(name, envVariable) {
const executable = this.getExecutableFileName(name)
2023-12-05 20:19:17 +01:00
const defaultPath = process.env[envVariable]
if (await this.isBinaryGood(defaultPath)) return defaultPath
2023-12-05 20:19:17 +01:00
const whichPath = which.sync(executable, { nothrow: true })
if (await this.isBinaryGood(whichPath)) return whichPath
2023-12-05 20:19:17 +01:00
const mainInstallPath = path.join(this.mainInstallPath, executable)
if (await this.isBinaryGood(mainInstallPath)) return mainInstallPath
2023-12-05 20:19:17 +01:00
const altInstallPath = path.join(this.altInstallPath, executable)
if (await this.isBinaryGood(altInstallPath)) return altInstallPath
2023-12-05 20:19:17 +01:00
return null
}
async isBinaryGood(binaryPath) {
if (!binaryPath || !await fs.pathExists(binaryPath)) return false
try {
const { stdout } = await this.exec('"' + binaryPath + '"' + ' -version')
const version = stdout.match(/version\s([\d\.]+)/)?.[1]
if (!version) return false
return this.goodVersions.some(goodVersion => version.startsWith(goodVersion))
} catch (err) {
Logger.error(`[BinaryManager] Failed to check version of ${binaryPath}`)
return false
}
}
2023-12-05 20:19:17 +01:00
async install(binaries) {
if (binaries.length == 0) 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: '6.1', force: true })
2023-12-05 20:19:17 +01:00
Logger.info(`[BinaryManager] Binaries installed to ${destination}`)
}
getExecutableFileName(name) {
return name + (process.platform == 'win32' ? '.exe' : '')
}
2023-12-05 20:19:17 +01:00
}
module.exports = BinaryManager