Skip to content

Commit

Permalink
minor refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBrax committed Nov 8, 2023
1 parent ca27ed8 commit 52b7e51
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 108 deletions.
2 changes: 1 addition & 1 deletion server/src/Core/Helper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ExecReturn } from "@/Helpers/Execute";
import { execSimple } from "@/Helpers/Execute";
import { executable_name, is_windows } from "@/Helpers/System";
import fs from "node:fs";
import path from "node:path";
import type { ExecReturn } from "../Providers/Twitch";
import { BaseConfigDataFolder } from "./BaseConfig";
import { Config } from "./Config";
import { LOGLEVEL, log } from "./Log";
Expand Down
20 changes: 10 additions & 10 deletions server/src/Core/Providers/Base/BaseAutomator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import type { Exporter } from "@/Controllers/Exporter";
import { GetExporter } from "@/Controllers/Exporter";
import { progressOutput } from "@/Helpers/Console";
import { execSimple, startJob } from "@/Helpers/Execute";
import { formatBytes } from "@/Helpers/Format";
import { Sleep } from "@/Helpers/Sleep";
import { xClearInterval, xInterval } from "@/Helpers/Timeout";
import { isTwitchVOD, isTwitchVODChapter } from "@/Helpers/Types";
import type { RemuxReturn } from "@/Helpers/Video";
import { remuxFile } from "@/Helpers/Video";
import type { TwitchVODChapterJSON } from "@/Storage/JSON";
import type { VideoQuality } from "@common/Config";
import type { NotificationCategory } from "@common/Defs";
Expand All @@ -21,16 +31,6 @@ import fs from "node:fs";
import type { IncomingHttpHeaders } from "node:http";
import path from "node:path";
import sanitize from "sanitize-filename";
import type { Exporter } from "../../../Controllers/Exporter";
import { GetExporter } from "../../../Controllers/Exporter";
import { progressOutput } from "../../../Helpers/Console";
import { execSimple, startJob } from "../../../Helpers/Execute";
import { formatBytes } from "../../../Helpers/Format";
import { Sleep } from "../../../Helpers/Sleep";
import { xClearInterval, xInterval } from "../../../Helpers/Timeout";
import { isTwitchVOD, isTwitchVODChapter } from "../../../Helpers/Types";
import { remuxFile } from "../../../Helpers/Video";
import type { RemuxReturn } from "../../../Providers/Twitch";
import { BaseConfigCacheFolder, BaseConfigDataFolder } from "../../BaseConfig";
import { ClientBroker } from "../../ClientBroker";
import { Config } from "../../Config";
Expand Down
2 changes: 1 addition & 1 deletion server/src/Core/Providers/Base/BaseVOD.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { ExecReturn } from "@/Providers/Twitch";
import type { ApiBaseVod } from "@common/Api/Client";
import type { VODBookmark } from "@common/Bookmark";
import type { VideoQuality } from "@common/Config";
Expand All @@ -15,6 +14,7 @@ import { randomUUID } from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import { debugLog } from "../../../Helpers/Console";
import type { ExecReturn } from "../../../Helpers/Execute";
import { startJob } from "../../../Helpers/Execute";
import { formatBytes } from "../../../Helpers/Format";
import { xClearTimeout, xTimeout } from "../../../Helpers/Timeout";
Expand Down
8 changes: 8 additions & 0 deletions server/src/Helpers/Console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { Config } from "@/Core/Config";
import chalk from "chalk";
import { formatBytes } from "./Format";

/**
* Logs a progress message to the console, optionally including memory usage information.
* @param text - The message to log.
*/
export function progressOutput(text: string) {
if (Config.debug) {
const mem = process.memoryUsage();
Expand All @@ -17,6 +21,10 @@ export function progressOutput(text: string) {
}
}

/**
* Logs debug information to the console if debug mode is enabled.
* @param args - The arguments to log to the console.
*/
export function debugLog(...args: unknown[]) {
if (Config && Config.debug) {
console.debug(
Expand Down
26 changes: 21 additions & 5 deletions server/src/Helpers/Execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import chalk from "chalk";
import type { ChildProcessWithoutNullStreams } from "node:child_process";
import { spawn } from "node:child_process";
import type { Stream } from "node:stream";
import type { ExecReturn } from "../Providers/Twitch";

export interface ExecReturn {
stdout: string[];
stderr: string[];
code: number;
bin?: string;
args?: string[];
what?: string;
}

interface RunningProcess {
internal_pid: number;
Expand Down Expand Up @@ -124,10 +132,10 @@ export function isExecReturn(
/**
* Execute a command, make a job, and when it's done, return the output
*
* @param bin
* @param args
* @param jobName
* @param progressFunction
* @param bin - The binary to execute
* @param args - The arguments to pass to the binary
* @param jobName - The name of the job to create
* @param progressFunction - Return a number between 0 and 1 to set the progress of the job
* @returns
*/
export function execAdvanced(
Expand Down Expand Up @@ -256,6 +264,14 @@ export function execAdvanced(
});
}

/**
* Spawns a new process with the given binary and arguments, and returns a Job object to track its progress.
* @param jobName - The name of the job to be executed.
* @param bin - The binary to be executed.
* @param args - The arguments to be passed to the binary.
* @param env - An optional object containing environment variables to be set for the process.
* @returns A Job object representing the spawned process, or false if the process failed to spawn.
*/
export function startJob(
jobName: string,
bin: string,
Expand Down
16 changes: 16 additions & 0 deletions server/src/Helpers/Filesystem.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import fs from "node:fs";
import path from "node:path";

/**
* Calculates the size of a directory in bytes.
* @param dir - The path to the directory.
* @returns The size of the directory in bytes.
*/
export function directorySize(dir: string): number {
let size = 0;
for (const file of fs.readdirSync(dir)) {
Expand Down Expand Up @@ -49,6 +54,12 @@ export function validateAbsolutePath(dir: string): boolean {
return path.isAbsolute(dir) && !dir.match(/\0/);
}

/**
* Validates whether a given directory path is a relative path.
*
* @param dir - The directory path to validate.
* @returns A boolean indicating whether the directory path is relative or not.
*/
export function validateRelativePath(dir: string): boolean {
return (
!path.isAbsolute(dir) &&
Expand All @@ -67,6 +78,11 @@ export function validateRelativePath(dir: string): boolean {
);
}

/**
* Validates a filename to ensure it does not contain any invalid characters.
* @param filename - The filename to validate.
* @returns True if the filename is valid, false otherwise.
*/
export function validateFilename(filename: string): boolean {
return !/[\\/:*?"<>|\0]/.test(filename);
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/Helpers/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { BaseConfigCacheFolder } from "@/Core/BaseConfig";
import { Config } from "@/Core/Config";
import { Helper } from "@/Core/Helper";
import { LOGLEVEL, log } from "@/Core/Log";
import type { ExecReturn } from "@/Providers/Twitch";
import { createHash } from "crypto";
import fs from "node:fs";
import path from "node:path";
import type { ExecReturn } from "./Execute";
import { execSimple } from "./Execute";

export async function imageThumbnail(
Expand Down
9 changes: 4 additions & 5 deletions server/src/Helpers/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import { z } from "zod";
*/
export function parseJSON<T extends ZodRawShape>(
json: string,
classType: new () => T
classType: T
): T {
const obj = JSON.parse(json);
const instance = new classType();

// Validate the object against the class type's schema.
const schema = z.object(instance);
const schema = z.object(classType);
schema.parse(obj);

Object.assign(instance, obj);
return instance;
Object.assign(classType, obj);
return classType;
}
21 changes: 0 additions & 21 deletions server/src/Helpers/ReplaceAll.ts

This file was deleted.

26 changes: 13 additions & 13 deletions server/src/Helpers/Software.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { AppRoot, BaseConfigDataFolder } from "@/Core/BaseConfig";
import path from "node:path";
import fs from "node:fs";
import type { ExecReturn } from "../Providers/Twitch";
import { AppRoot } from "@/Core/BaseConfig";
import { Helper } from "@/Core/Helper";
import type { BinaryStatus } from "@common/Api/About";
import { compareVersions } from "compare-versions";
import fs from "node:fs";
import path from "node:path";
import type { ExecReturn } from "./Execute";
import { execSimple } from "./Execute";

interface BinaryDef {
Expand Down Expand Up @@ -48,15 +48,15 @@ export function DVRBinaries(): Record<string, BinaryDef> {

export function DVRPipPackages(): Record<string, BinaryDef> {
return {
tcd: {
binary: Helper.path_tcd(),
version_args: [
"--version",
"--settings-file",
path.join(BaseConfigDataFolder.config, "tcd_settings.json"),
],
version_regex: /^Twitch Chat Downloader\s+([0-9.]+)$/m,
},
// tcd: {
// binary: Helper.path_tcd(),
// version_args: [
// "--version",
// "--settings-file",
// path.join(BaseConfigDataFolder.config, "tcd_settings.json"),
// ],
// version_regex: /^Twitch Chat Downloader\s+([0-9.]+)$/m,
// },
streamlink: {
binary: Helper.path_streamlink(),
version_args: ["--version"],
Expand Down
8 changes: 7 additions & 1 deletion server/src/Helpers/Video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ import type { MediaInfo } from "@common/mediainfofield";
import { createHash } from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import type { RemuxReturn } from "../Providers/Twitch";
import { progressOutput } from "./Console";
import { execSimple, startJob } from "./Execute";
import { formatDuration } from "./Format";

export interface RemuxReturn {
stdout: string[];
stderr: string[];
code: number;
success: boolean;
}

/**
* Remux input to output
*
Expand Down
34 changes: 0 additions & 34 deletions server/src/Providers/Kick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,6 @@ import type {
} from "@common/KickAPI/Kick";
import axios, { isAxiosError } from "axios";

/*
const axiosInstance = axios.create({
baseURL: "https://kick.com/api/v1/",
"headers": {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; rv:114.0) Gecko/20100101 Firefox/114.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*;q=0.8",
"Accept-Language": "en-GB,en;q=0.8,sv-SE;q=0.5,sv;q=0.3",
"Content-Encoding": "gzip",
"Alt-Used": "kick.com",
"Upgrade-Insecure-Requests": "1",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none",
"Sec-Fetch-User": "?1",
"Referer": "https://kick.com/",
"DNT": "1",
"If-Modified-Since": "Thu, 01 Jan 1970 00:00:00 GMT",
},
});
export function getAxiosInstance() {
return axiosInstance;
}
export function hasAxiosInstance() {
return axiosInstance !== undefined;
}
export function setApiToken(token: string) {
axiosInstance.defaults.headers.common["Authorization"] = `Bearer ${token}`;
}
*/

const baseURL = "https://kick.com/api/v2/";

const cookies: Record<string, string> = {};
Expand Down
16 changes: 0 additions & 16 deletions server/src/Providers/Twitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,6 @@ import fs from "node:fs";
import path from "node:path";
import { WebSocket } from "ws";

export interface ExecReturn {
stdout: string[];
stderr: string[];
code: number;
bin?: string;
args?: string[];
what?: string;
}

export interface RemuxReturn {
stdout: string[];
stderr: string[];
code: number;
success: boolean;
}

interface TwitchAuthAppTokenFile {
access_token: string;
expires_at: number;
Expand Down

0 comments on commit 52b7e51

Please sign in to comment.