2024-01-25 16:51:06 +01:00
|
|
|
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')
|
2023-12-14 08:47:18 +01:00
|
|
|
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 = [
|
2024-02-28 23:16:26 +01:00
|
|
|
{ name: 'ffmpeg', envVariable: 'FFMPEG_PATH', validVersions: ['5.1'] },
|
|
|
|
{ name: 'ffprobe', envVariable: 'FFPROBE_PATH', validVersions: ['5.1'] }
|
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
|
2024-02-18 00:40:33 +01:00
|
|
|
this.initialized = false
|
2024-01-25 16:51:06 +01:00
|
|
|
this.exec = exec
|
2023-12-05 20:19:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
2024-03-14 22:29:01 +01:00
|
|
|
// Optional skip binaries check
|
|
|
|
if (process.env.SKIP_BINARIES_CHECK === '1') {
|
|
|
|
Logger.info('[BinaryManager] Skipping check for binaries')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-05 20:19:17 +01:00
|
|
|
if (this.initialized) return
|
2024-03-14 22:29:01 +01:00
|
|
|
|
2023-12-05 20:19:17 +01:00
|
|
|
const missingBinaries = await this.findRequiredBinaries()
|
|
|
|
if (missingBinaries.length == 0) return
|
2024-01-25 16:51:06 +01:00
|
|
|
await this.removeOldBinaries(missingBinaries)
|
2023-12-05 20:19:17 +01:00
|
|
|
await this.install(missingBinaries)
|
|
|
|
const missingBinariesAfterInstall = await this.findRequiredBinaries()
|
2024-02-18 00:40:33 +01:00
|
|
|
if (missingBinariesAfterInstall.length) {
|
2023-12-05 20:19:17 +01:00
|
|
|
Logger.error(`[BinaryManager] Failed to find or install required binaries: ${missingBinariesAfterInstall.join(', ')}`)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
this.initialized = true
|
|
|
|
}
|
|
|
|
|
2024-02-18 00:40:33 +01:00
|
|
|
/**
|
|
|
|
* Remove old/invalid binaries in main or alt install path
|
|
|
|
*
|
|
|
|
* @param {string[]} binaryNames
|
|
|
|
*/
|
2024-01-25 16:51:06 +01:00
|
|
|
async removeOldBinaries(binaryNames) {
|
|
|
|
for (const binaryName of binaryNames) {
|
|
|
|
const executable = this.getExecutableFileName(binaryName)
|
|
|
|
const mainInstallPath = path.join(this.mainInstallPath, executable)
|
2024-02-18 00:40:33 +01:00
|
|
|
if (await fs.pathExists(mainInstallPath)) {
|
|
|
|
Logger.debug(`[BinaryManager] Removing old binary: ${mainInstallPath}`)
|
|
|
|
await fs.remove(mainInstallPath)
|
|
|
|
}
|
2024-01-25 16:51:06 +01:00
|
|
|
const altInstallPath = path.join(this.altInstallPath, executable)
|
2024-02-18 00:40:33 +01:00
|
|
|
if (await fs.pathExists(altInstallPath)) {
|
|
|
|
Logger.debug(`[BinaryManager] Removing old binary: ${altInstallPath}`)
|
|
|
|
await fs.remove(altInstallPath)
|
|
|
|
}
|
2024-01-25 16:51:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-18 00:40:33 +01:00
|
|
|
/**
|
|
|
|
* Find required binaries and return array of binary names that are missing
|
|
|
|
*
|
|
|
|
* @returns {Promise<string[]>}
|
|
|
|
*/
|
2023-12-05 20:19:17 +01:00
|
|
|
async findRequiredBinaries() {
|
|
|
|
const missingBinaries = []
|
|
|
|
for (const binary of this.requiredBinaries) {
|
2024-02-18 00:40:33 +01:00
|
|
|
const binaryPath = await this.findBinary(binary.name, binary.envVariable, binary.validVersions)
|
2023-12-05 20:19:17 +01:00
|
|
|
if (binaryPath) {
|
2024-02-18 00:40:33 +01:00
|
|
|
Logger.info(`[BinaryManager] Found valid binary ${binary.name} at ${binaryPath}`)
|
2023-12-08 00:32:06 +01:00
|
|
|
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 {
|
2024-01-25 16:51:06 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-18 00:40:33 +01:00
|
|
|
/**
|
|
|
|
* 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 = []) {
|
2024-01-25 16:51:06 +01:00
|
|
|
const executable = this.getExecutableFileName(name)
|
2024-02-18 00:40:33 +01:00
|
|
|
// 1. check path specified in environment variable
|
2023-12-05 20:19:17 +01:00
|
|
|
const defaultPath = process.env[envVariable]
|
2024-02-18 00:40:33 +01:00
|
|
|
if (await this.isBinaryGood(defaultPath, validVersions)) return defaultPath
|
|
|
|
// 2. find the first instance of the binary in the PATH environment variable
|
2023-12-05 20:19:17 +01:00
|
|
|
const whichPath = which.sync(executable, { nothrow: true })
|
2024-02-18 00:40:33 +01:00
|
|
|
if (await this.isBinaryGood(whichPath, validVersions)) return whichPath
|
|
|
|
// 3. check main install path (binary root dir)
|
2023-12-05 20:19:17 +01:00
|
|
|
const mainInstallPath = path.join(this.mainInstallPath, executable)
|
2024-02-18 00:40:33 +01:00
|
|
|
if (await this.isBinaryGood(mainInstallPath, validVersions)) return mainInstallPath
|
|
|
|
// 4. check alt install path (/config)
|
2023-12-05 20:19:17 +01:00
|
|
|
const altInstallPath = path.join(this.altInstallPath, executable)
|
2024-02-18 00:40:33 +01:00
|
|
|
if (await this.isBinaryGood(altInstallPath, validVersions)) return altInstallPath
|
2023-12-05 20:19:17 +01:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2024-02-18 00:40:33 +01:00
|
|
|
/**
|
|
|
|
* Check binary path exists and optionally check version is valid
|
|
|
|
*
|
|
|
|
* @param {string} binaryPath
|
|
|
|
* @param {string[]} [validVersions]
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
async isBinaryGood(binaryPath, validVersions = []) {
|
2024-01-25 16:51:06 +01:00
|
|
|
if (!binaryPath || !await fs.pathExists(binaryPath)) return false
|
2024-02-18 00:40:33 +01:00
|
|
|
if (!validVersions.length) return true
|
2024-01-25 16:51:06 +01:00
|
|
|
try {
|
|
|
|
const { stdout } = await this.exec('"' + binaryPath + '"' + ' -version')
|
|
|
|
const version = stdout.match(/version\s([\d\.]+)/)?.[1]
|
|
|
|
if (!version) return false
|
2024-02-18 00:40:33 +01:00
|
|
|
return validVersions.some(validVersion => version.startsWith(validVersion))
|
2024-01-25 16:51:06 +01:00
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`[BinaryManager] Failed to check version of ${binaryPath}`)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-18 00:40:33 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string[]} binaries
|
|
|
|
*/
|
2023-12-05 20:19:17 +01:00
|
|
|
async install(binaries) {
|
2024-02-18 00:40:33 +01:00
|
|
|
if (!binaries.length) return
|
2023-12-05 20:19:17 +01:00
|
|
|
Logger.info(`[BinaryManager] Installing binaries: ${binaries.join(', ')}`)
|
2023-12-14 08:47:18 +01:00
|
|
|
let destination = await fileUtils.isWritable(this.mainInstallPath) ? this.mainInstallPath : this.altInstallPath
|
2024-02-28 23:16:26 +01:00
|
|
|
await ffbinaries.downloadBinaries(binaries, { destination, version: '5.1', force: true })
|
2023-12-05 20:19:17 +01:00
|
|
|
Logger.info(`[BinaryManager] Binaries installed to ${destination}`)
|
|
|
|
}
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-02-18 00:40:33 +01:00
|
|
|
/**
|
|
|
|
* Append .exe to binary name for Windows
|
|
|
|
*
|
|
|
|
* @param {string} name
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2024-01-25 16:51:06 +01:00
|
|
|
getExecutableFileName(name) {
|
|
|
|
return name + (process.platform == 'win32' ? '.exe' : '')
|
|
|
|
}
|
2023-12-05 20:19:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = BinaryManager
|