Skip to content

Commit

Permalink
Remove hardcoded paths
Browse files Browse the repository at this point in the history
All paths can now be set in the arguments, and are all derived from the
workPath (by default: ./db). This *should* require no change from the
outside users as well, a transparent change.
  • Loading branch information
Noodlez1232 committed Oct 29, 2024
1 parent f67d4a3 commit 20deec1
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/lib/Attachment.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -31,7 +31,7 @@ enum Extensions {
}

export class Attachment implements AttachmentAttributes {
private static readonly AttachmentsDB: Record<string, AttachmentInfo> = db<Record<string, AttachmentInfo>>(`./db/attachments.json`);
private static readonly AttachmentsDB: Record<string, AttachmentInfo> = db<Record<string, AttachmentInfo>>(args.attachmentsPath);
public static readonly Extensions = Extensions;

public readonly attachmentId: string;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ export class Video extends Attachment {

await new Promise((resolve, reject) =>
execFile(
"./db/ffmpeg",
args.ffmpegPath,
[
"-i",
this.partialPath,
Expand Down
20 changes: 20 additions & 0 deletions src/lib/defaults.ts
Original file line number Diff line number Diff line change
@@ -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<string, Channels> = {
Expand Down Expand Up @@ -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() )}`;

Check failure on line 46 in src/lib/defaults.ts

View workflow job for this annotation

GitHub Actions / eslint

[eslint] reported by reviewdog 🐶 Delete `·` Raw Output: {"ruleId":"prettier/prettier","severity":2,"message":"Delete `·`","line":46,"column":85,"nodeType":null,"messageId":"delete","endLine":46,"endColumn":86,"fix":{"range":[1241,1242],"text":""}}

Check failure on line 46 in src/lib/defaults.ts

View workflow job for this annotation

GitHub Actions / eslint

[eslint] reported by reviewdog 🐶 Delete `·` Raw Output: {"ruleId":"prettier/prettier","severity":2,"message":"Delete `·`","line":46,"column":85,"nodeType":null,"messageId":"delete","endLine":46,"endColumn":86,"fix":{"range":[1241,1242],"text":""}}
}
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",
Expand Down
12 changes: 7 additions & 5 deletions src/lib/helpers/fetchFFMPEG.ts
Original file line number Diff line number Diff line change
@@ -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<void> =>
new Promise((resolve, reject) => {
const platform = detectPlatform();
const path = "./db/";
if (fs.existsSync(`${path}${getBinaryFilename("ffmpeg", platform)}`) === false) {
let platform = detectPlatform();

Check failure on line 8 in src/lib/helpers/fetchFFMPEG.ts

View workflow job for this annotation

GitHub Actions / eslint

[eslint] reported by reviewdog 🐶 'platform' is never reassigned. Use 'const' instead. Raw Output: {"ruleId":"prefer-const","severity":2,"message":"'platform' is never reassigned. Use 'const' instead.","line":8,"column":7,"nodeType":"Identifier","messageId":"useConst","endLine":8,"endColumn":15,"fix":{"range":[267,299],"text":"const platform = detectPlatform();"}}

Check failure on line 8 in src/lib/helpers/fetchFFMPEG.ts

View workflow job for this annotation

GitHub Actions / eslint

[eslint] reported by reviewdog 🐶 'platform' is never reassigned. Use 'const' instead. Raw Output: {"ruleId":"prefer-const","severity":2,"message":"'platform' is never reassigned. Use 'const' instead.","line":8,"column":7,"nodeType":"Identifier","messageId":"useConst","endLine":8,"endColumn":15,"fix":{"range":[267,299],"text":"const platform = detectPlatform();"}}
let path = args.ffmpegPath;

Check failure on line 9 in src/lib/helpers/fetchFFMPEG.ts

View workflow job for this annotation

GitHub Actions / eslint

[eslint] reported by reviewdog 🐶 'path' is never reassigned. Use 'const' instead. Raw Output: {"ruleId":"prefer-const","severity":2,"message":"'path' is never reassigned. Use 'const' instead.","line":9,"column":7,"nodeType":"Identifier","messageId":"useConst","endLine":9,"endColumn":11,"fix":{"range":[302,329],"text":"const path = args.ffmpegPath;"}}

Check failure on line 9 in src/lib/helpers/fetchFFMPEG.ts

View workflow job for this annotation

GitHub Actions / eslint

[eslint] reported by reviewdog 🐶 'path' is never reassigned. Use 'const' instead. Raw Output: {"ruleId":"prefer-const","severity":2,"message":"'path' is never reassigned. Use 'const' instead.","line":9,"column":7,"nodeType":"Identifier","messageId":"useConst","endLine":9,"endColumn":11,"fix":{"range":[302,329],"text":"const 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) => {
Expand Down
48 changes: 26 additions & 22 deletions src/lib/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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))<PartialArgs>({});
const env = getEnv();

export const args = defaultArgs;
recursiveUpdate(args, env, { setUndefined: false, setDefined: true });

Check failure on line 32 in src/lib/helpers/index.ts

View workflow job for this annotation

GitHub Actions / eslint

[eslint] reported by reviewdog 🐶 Replace `↹` with `·` Raw Output: {"ruleId":"prettier/prettier","severity":2,"message":"Replace `↹` with `·`","line":32,"column":27,"nodeType":null,"messageId":"replace","endLine":32,"endColumn":28,"fix":{"range":[916,917],"text":" "}}

Check failure on line 32 in src/lib/helpers/index.ts

View workflow job for this annotation

GitHub Actions / eslint

[eslint] reported by reviewdog 🐶 Replace `↹` with `·` Raw Output: {"ruleId":"prettier/prettier","severity":2,"message":"Replace `↹` with `·`","line":32,"column":27,"nodeType":null,"messageId":"replace","endLine":32,"endColumn":28,"fix":{"range":[916,917],"text":" "}}
recursiveUpdate(args, argv, { setUndefined: false, setDefined: true });
fixArgs(args);

export const settings = defaultSettings;
let newSettings = db<Settings>(args.settingsPath, { template: defaultSettings, pretty: true, forceCreate: true, updateOnExternalChanges: true });

Check failure on line 37 in src/lib/helpers/index.ts

View workflow job for this annotation

GitHub Actions / eslint

[eslint] reported by reviewdog 🐶 'newSettings' is never reassigned. Use 'const' instead. Raw Output: {"ruleId":"prefer-const","severity":2,"message":"'newSettings' is never reassigned. Use 'const' instead.","line":37,"column":5,"nodeType":"Identifier","messageId":"useConst","endLine":37,"endColumn":16,"fix":{"range":[1090,1235],"text":"const newSettings = db<Settings>(args.settingsPath, { template: defaultSettings, pretty: true, forceCreate: true, updateOnExternalChanges: true });"}}

Check failure on line 37 in src/lib/helpers/index.ts

View workflow job for this annotation

GitHub Actions / eslint

[eslint] reported by reviewdog 🐶 'newSettings' is never reassigned. Use 'const' instead. Raw Output: {"ruleId":"prefer-const","severity":2,"message":"'newSettings' is never reassigned. Use 'const' instead.","line":37,"column":5,"nodeType":"Identifier","messageId":"useConst","endLine":37,"endColumn":16,"fix":{"range":[1090,1235],"text":"const newSettings = db<Settings>(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`,
Expand Down Expand Up @@ -58,27 +80,7 @@ fApi.extend({
},
});

const argv = ARGV(process.argv.slice(2))<PartialArgs>({});
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<Settings>("./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);



Expand All @@ -94,3 +96,5 @@ if (args.headless === true) {
return originalStdoutWrite(...params);
}) as typeof process.stdout.write;
}

Check failure on line 99 in src/lib/helpers/index.ts

View workflow job for this annotation

GitHub Actions / eslint

[eslint] reported by reviewdog 🐶 Delete `⏎⏎` Raw Output: {"ruleId":"prettier/prettier","severity":2,"message":"Delete `⏎⏎`","line":99,"column":1,"nodeType":null,"messageId":"delete","endLine":101,"endColumn":1,"fix":{"range":[3639,3641],"text":""}}

Check failure on line 99 in src/lib/helpers/index.ts

View workflow job for this annotation

GitHub Actions / eslint

[eslint] reported by reviewdog 🐶 Delete `⏎⏎` Raw Output: {"ruleId":"prettier/prettier","severity":2,"message":"Delete `⏎⏎`","line":99,"column":1,"nodeType":null,"messageId":"delete","endLine":101,"endColumn":1,"fix":{"range":[3639,3641],"text":""}}

5 changes: 5 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Args & Settings>;
Expand Down

0 comments on commit 20deec1

Please sign in to comment.