|
| 1 | +import * as fs from "fs" |
| 2 | +import * as path from "path" |
| 3 | +import { ElectronPlatformName } from "./ElectronFramework" |
| 4 | + |
| 5 | +import { log } from "builder-util" |
| 6 | +import { getBin } from "../binDownload" |
| 7 | +import { PrepareApplicationStageDirectoryOptions } from "../Framework" |
| 8 | + |
| 9 | +// NOTE: Adapted from https://github.com/MarshallOfSound/electron-packager-plugin-non-proprietary-codecs-ffmpeg to resolve dependency vulnerabilities |
| 10 | +const downloadFFMPEG = async (electronVersion: string, platform: ElectronPlatformName, arch: string) => { |
| 11 | + const ffmpegFileName = `ffmpeg-v${electronVersion}-${platform}-${arch}.zip` |
| 12 | + const url = `https://github.com/electron/electron/releases/download/v${electronVersion}/${ffmpegFileName}` |
| 13 | + |
| 14 | + log.info({ file: ffmpegFileName }, "downloading non-proprietary FFMPEG") |
| 15 | + return getBin(ffmpegFileName, url) |
| 16 | +} |
| 17 | + |
| 18 | +const copyFFMPEG = (targetPath: string, platform: ElectronPlatformName) => (sourcePath: string) => { |
| 19 | + let fileName = "ffmpeg.dll" |
| 20 | + if (["darwin", "mas"].includes(platform)) { |
| 21 | + fileName = "libffmpeg.dylib" |
| 22 | + } else if (platform === "linux") { |
| 23 | + fileName = "libffmpeg.so" |
| 24 | + } |
| 25 | + |
| 26 | + const libPath = path.resolve(sourcePath, fileName) |
| 27 | + const libTargetPath = path.resolve(targetPath, fileName) |
| 28 | + log.info({ lib: libPath, target: libTargetPath }, "copying non-proprietary FFMPEG") |
| 29 | + |
| 30 | + // If the source doesn't exist we have a problem |
| 31 | + if (!fs.existsSync(libPath)) { |
| 32 | + throw new Error(`Failed to find FFMPEG library file at path: ${libPath}`) |
| 33 | + } |
| 34 | + |
| 35 | + // If we are copying to the source we can stop immediately |
| 36 | + if (libPath !== libTargetPath) { |
| 37 | + fs.copyFileSync(libPath, libTargetPath) |
| 38 | + } |
| 39 | + return libTargetPath |
| 40 | +} |
| 41 | + |
| 42 | +export default function injectFFMPEG(options: PrepareApplicationStageDirectoryOptions, electrionVersion: string) { |
| 43 | + let libPath = options.appOutDir |
| 44 | + if (options.platformName === "darwin") { |
| 45 | + libPath = path.resolve(options.appOutDir, "Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Libraries") |
| 46 | + } |
| 47 | + |
| 48 | + return downloadFFMPEG(electrionVersion, options.platformName, options.arch).then(copyFFMPEG(libPath, options.platformName)) |
| 49 | +} |
0 commit comments