diff --git a/src/lib/Attachment.ts b/src/lib/Attachment.ts index ae15eb9..6b3f0ae 100644 --- a/src/lib/Attachment.ts +++ b/src/lib/Attachment.ts @@ -1,7 +1,7 @@ import db from "@inrixia/db"; import { nPad } from "@inrixia/helpers/math"; import { ValueOfA } from "@inrixia/helpers/ts"; -import { settings } from "./helpers/index.js"; +import { settings, args } from "./helpers/index.js"; import sanitize from "sanitize-filename"; import { dirname, basename, extname } from "path"; @@ -31,7 +31,7 @@ enum Extensions { } export class Attachment implements AttachmentAttributes { - private static readonly AttachmentsDB: Record = db>(`./db/attachments.json`); + private static readonly AttachmentsDB: Record = db>(args.attachmentsPath); public static readonly Extensions = Extensions; public readonly attachmentId: string; diff --git a/src/lib/Video.ts b/src/lib/Video.ts index af755fe..ac8a287 100644 --- a/src/lib/Video.ts +++ b/src/lib/Video.ts @@ -363,7 +363,7 @@ export class Video extends Attachment { await new Promise((resolve, reject) => execFile( - "./db/ffmpeg", + args.ffmpegPath, [ "-i", this.partialPath, diff --git a/src/lib/defaults.ts b/src/lib/defaults.ts index bf37956..84ee5fe 100644 --- a/src/lib/defaults.ts +++ b/src/lib/defaults.ts @@ -1,4 +1,5 @@ import { Resolutions, Channels, Settings, Args } from "./types.js"; +import { getBinaryFilename, detectPlatform } from "ffbinaries"; export const defaultResolutions: Resolutions = ["360", "720", "1080", "2160"]; export const defaultSubChannels: Record = { @@ -34,7 +35,26 @@ export const defaultArgs: Args = { plexUsername: "", plexPassword: "", sanityCheck: false, + workPath: "./db", + ffmpegPath: "", + settingsPath: "", + cookiesPath: "", + attachmentsPath: "", }; +export function fixArgs(args: Args) { + if (args.ffmpegPath === "") { + args.ffmpegPath = `${args.workPath}/${getBinaryFilename("ffmpeg", detectPlatform() )}`; + } + if (args.settingsPath === "") { + args.settingsPath = `${args.workPath}/settings.json`; + } + if (args.cookiesPath === "") { + args.cookiesPath = `${args.workPath}/cookies.json`; + } + if (args.attachmentsPath === "") { + args.attachmentsPath = `${args.workPath}/attachments.json`; + } +} export const defaultSettings: Settings = { __SettingsWiki: "https://github.com/Inrixia/Floatplane-Downloader/blob/master/wiki/settings.md", diff --git a/src/lib/helpers/fetchFFMPEG.ts b/src/lib/helpers/fetchFFMPEG.ts index b3499ac..9d1440b 100644 --- a/src/lib/helpers/fetchFFMPEG.ts +++ b/src/lib/helpers/fetchFFMPEG.ts @@ -1,16 +1,18 @@ import { downloadBinaries, detectPlatform, getBinaryFilename } from "ffbinaries"; +import { args } from "./index.js"; import fs from "fs"; +import { dirname, basename } from "path"; export const fetchFFMPEG = (): Promise => new Promise((resolve, reject) => { - const platform = detectPlatform(); - const path = "./db/"; - if (fs.existsSync(`${path}${getBinaryFilename("ffmpeg", platform)}`) === false) { + let platform = detectPlatform(); + let path = args.ffmpegPath; + if (fs.existsSync(path) === false) { process.stdout.write("> Ffmpeg binary missing! Downloading... "); downloadBinaries( - "ffmpeg", + basename(path), { - destination: path, + destination: dirname(path), platform, }, (err) => { diff --git a/src/lib/helpers/index.ts b/src/lib/helpers/index.ts index dfcc135..3522d29 100644 --- a/src/lib/helpers/index.ts +++ b/src/lib/helpers/index.ts @@ -1,5 +1,5 @@ import { getEnv, rebuildTypes, recursiveUpdate } from "@inrixia/helpers/object"; -import { defaultArgs, defaultSettings } from "../defaults.js"; +import { defaultArgs, defaultSettings, fixArgs } from "../defaults.js"; import { Histogram } from "prom-client"; import db from "@inrixia/db"; @@ -22,9 +22,31 @@ import type { PartialArgs, Settings } from "../types.js"; import { FileCookieStore } from "tough-cookie-file-store"; import { CookieJar } from "tough-cookie"; -export const cookieJar = new CookieJar(new FileCookieStore("./db/cookies.json")); import { Floatplane } from "floatplane"; + +const argv = ARGV(process.argv.slice(2))({}); +const env = getEnv(); + +export const args = defaultArgs; +recursiveUpdate(args, env, { setUndefined: false, setDefined: true }); +recursiveUpdate(args, argv, { setUndefined: false, setDefined: true }); +fixArgs(args); + +export const settings = defaultSettings; +let newSettings = db(args.settingsPath, { template: defaultSettings, pretty: true, forceCreate: true, updateOnExternalChanges: true }); +recursiveUpdate(settings, newSettings, { setUndefined: true, setDefined: true }); + +recursiveUpdate(settings, argv, { setUndefined: false, setDefined: true }); + +if (env.__FPDSettings !== undefined) { + if (typeof env.__FPDSettings !== "string") throw new Error("The __FPDSettings environment variable cannot be parsed!"); + recursiveUpdate(settings, parse(env.__FPDSettings.replaceAll('\\"', '"')), { setUndefined: false, setDefined: true }); +} + +recursiveUpdate(settings, env, { setUndefined: false, setDefined: true }); + +export const cookieJar = new CookieJar(new FileCookieStore(args.cookiesPath)); export const fApi = new Floatplane( cookieJar, `Floatplane-Downloader/${DownloaderVersion} (Inrix, +https://github.com/Inrixia/Floatplane-Downloader), CFNetwork`, @@ -58,27 +80,7 @@ fApi.extend({ }, }); -const argv = ARGV(process.argv.slice(2))({}); -const env = getEnv(); -export const args = defaultArgs; -recursiveUpdate(args, env, { setUndefined: false, setDefined: true }); -recursiveUpdate(args, argv, { setUndefined: false, setDefined: true }); - -export const settings = defaultSettings; -recursiveUpdate(settings, - db("./db/settings.json", { template: defaultSettings, pretty: true, forceCreate: true, updateOnExternalChanges: true }), - { setUndefined: true, setDefined: true }); - -recursiveUpdate(settings, argv, { setUndefined: false, setDefined: true }); - -if (env.__FPDSettings !== undefined) { - if (typeof env.__FPDSettings !== "string") throw new Error("The __FPDSettings environment variable cannot be parsed!"); - recursiveUpdate(settings, parse(env.__FPDSettings.replaceAll('\\"', '"')), { setUndefined: false, setDefined: true }); -} - -recursiveUpdate(settings, env, { setUndefined: false, setDefined: true }); -console.log(settings); @@ -94,3 +96,5 @@ if (args.headless === true) { return originalStdoutWrite(...params); }) as typeof process.stdout.write; } + + diff --git a/src/lib/types.ts b/src/lib/types.ts index 488c4b8..0c3059d 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -33,6 +33,11 @@ export type Args = { plexUsername: string; plexPassword: string; sanityCheck: boolean; + workPath: string; + settingsPath: string; + ffmpegPath: string; + cookiesPath: string; + attachmentsPath: string; }; export type PartialArgs = Partial;