Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBrax committed Nov 28, 2023
1 parent f6628ce commit 194ca35
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 120 deletions.
44 changes: 35 additions & 9 deletions server/src/Exporters/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ export class BaseExporter {
public supportsDirectories = false;
public directoryMode = false;

setDirectoryMode(state: boolean) {
public setDirectoryMode(state: boolean) {
if (!this.supportsDirectories) return;
this.directoryMode = state;
}

loadVOD(vod: VODTypes, segment = 0): boolean {
/**
* Loads a VOD and sets the filename, extension, and vod properties.
* @param vod - The VOD object to load.
* @param segment - The index of the segment to load (default is 0).
* @returns True if the VOD is successfully loaded, false otherwise.
* @throws Error if the VOD has no filename, no segments, the segment file does not exist, or no segment filename.
*/
public loadVOD(vod: VODTypes, segment = 0): boolean {
if (!vod.filename) throw new Error("No filename");
if (!vod.segments || vod.segments.length == 0)
throw new Error("No segments");
Expand All @@ -42,23 +49,34 @@ export class BaseExporter {
}
}

loadFile(filename: string): boolean {
/**
* Loads a file with the specified filename.
* @param filename - The name of the file to load.
* @returns True if the file was successfully loaded, false otherwise.
* @throws Error if no filename is provided or if the file does not exist.
*/
public loadFile(filename: string): boolean {
if (!filename) throw new Error("No filename");
if (!fs.existsSync(filename)) throw new Error("File does not exist");
this.filename = filename;
this.extension = path.extname(this.filename).substring(1);
return true;
}

setTemplate(template_filename: string): void {
/**
* Sets the template filename for the exporter.
* @param template_filename - The filename of the template.
* @returns void
*/
public setTemplate(template_filename: string): void {
this.template_filename = template_filename;
}

setOutputFilename(filename: string): void {
public setOutputFilename(filename: string): void {
this.output_filename = filename;
}

setSource(source: "segment" | "downloaded" | "burned"): void {
public setSource(source: "segment" | "downloaded" | "burned"): void {
if (!this.vod) throw new Error("No vod loaded for setSource");
if (source == "segment") {
if (
Expand All @@ -80,7 +98,7 @@ export class BaseExporter {
}
}

getFormattedTitle() {
public getFormattedTitle() {
if (this.output_filename !== "") {
return this.output_filename; // override
}
Expand Down Expand Up @@ -141,11 +159,19 @@ export class BaseExporter {
return formatString(this.template_filename, replacements);
}

async export(): Promise<boolean | string> {
/**
* Exports the VOD.
* @returns A promise that resolves to a boolean or a string.
*/
public async export(): Promise<boolean | string> {
return await Promise.reject(new Error("Export not implemented"));
}

async verify(): Promise<boolean> {
/**
* Verifies the status of the exported file. This is called after the export is complete.
* @returns A promise that resolves to a boolean.
*/
public async verify(): Promise<boolean> {
return await Promise.reject(new Error("Verification not implemented"));
}
}
48 changes: 26 additions & 22 deletions server/src/Exporters/FTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import path from "node:path";
import sanitize from "sanitize-filename";
import { BaseExporter } from "./Base";

/**
* Basic FTP exporter to transfer the VOD to a remote FTP server.
* Uses curl to transfer the file.
*/
export class FTPExporter extends BaseExporter {
public type = "FTP";

Expand All @@ -15,23 +19,23 @@ export class FTPExporter extends BaseExporter {

// public supportsDirectories = true;

setDirectory(directory: string): void {
public setDirectory(directory: string): void {
this.directory = directory;
}

setHost(host: string): void {
public setHost(host: string): void {
this.host = host;
}

setUsername(username: string): void {
public setUsername(username: string): void {
this.username = username;
}

setPassword(password: string): void {
public setPassword(password: string): void {
this.password = password;
}

export(): Promise<boolean | string> {
public export(): Promise<boolean | string> {
return new Promise<boolean | string>((resolve, reject) => {
if (!this.filename) throw new Error("No filename");
if (!this.extension) throw new Error("No extension");
Expand All @@ -44,32 +48,32 @@ export class FTPExporter extends BaseExporter {
// if (!this.directory) throw new Error("No directory");
if (!this.getFormattedTitle()) throw new Error("No title");

const final_filename =
const finalFilename =
sanitize(this.getFormattedTitle()) + "." + this.extension;

const filesystem_path = path.join(this.directory, final_filename);
const linux_path = filesystem_path.replace(/\\/g, "/");
const web_path = encodeURIComponent(linux_path);
const filesystemPath = path.join(this.directory, finalFilename);
const linuxPath = filesystemPath.replace(/\\/g, "/");
const webPath = encodeURIComponent(linuxPath);

this.remote_file = linux_path;
this.remote_file = linuxPath;

const local_name = this.filename
const localName = this.filename
.replace(/\\/g, "/")
.replace(/^C:/, "");
const local_path = local_name.includes(" ")
? `'${local_name}'`
: local_name;
const localPath = localName.includes(" ")
? `'${localName}'`
: localName;

let ftp_url = `ftp://${this.host}/${web_path}`;
let ftpUrl = `ftp://${this.host}/${webPath}`;
if (this.username && this.password) {
ftp_url = `ftp://${this.username}:${this.password}@${this.host}/${web_path}`;
ftpUrl = `ftp://${this.username}:${this.password}@${this.host}/${webPath}`;
}

if (ftp_url.includes(" ")) ftp_url = `'${ftp_url}'`;
if (ftpUrl.includes(" ")) ftpUrl = `'${ftpUrl}'`;

const bin = "curl";

const args = ["-v", "-g", "-T", local_path, ftp_url];
const args = ["-v", "-g", "-T", localPath, ftpUrl];
//

console.log(`${bin} ${args.join(" ")}`);
Expand All @@ -96,22 +100,22 @@ export class FTPExporter extends BaseExporter {
if (code !== 0) {
reject(new Error(`Failed to clear, code ${code}`));
} else {
resolve(linux_path);
resolve(linuxPath);
}
});
});
}

// verify that the file exists over ftp
async verify(): Promise<boolean> {
const web_path = encodeURIComponent(this.remote_file);
public async verify(): Promise<boolean> {
const webPath = encodeURIComponent(this.remote_file);

const bin = "curl";
const args = [
"--list-only",
`ftp://${this.username}:${this.password}@${
this.host
}/${path.dirname(web_path)}`,
}/${path.dirname(webPath)}`,
];

const job = await execSimple(bin, args, "ftp file check");
Expand Down
29 changes: 18 additions & 11 deletions server/src/Exporters/File.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import { Job } from "@/Core/Job";
import { log, LOGLEVEL } from "@/Core/Log";
import { xClearInterval, xInterval } from "@/Helpers/Timeout";
import fs from "node:fs";
import path from "node:path";
import sanitize from "sanitize-filename";
import { BaseExporter } from "./Base";
import { xClearInterval, xInterval } from "@/Helpers/Timeout";

/**
* Basic file exporter to copy the VOD to a directory on the local filesystem.
*/
export class FileExporter extends BaseExporter {
public type = "File";

public directory = "";

private final_path = "";

public supportsDirectories = true;
private final_path = "";

setDirectory(directory: string): void {
public setDirectory(directory: string): void {
if (!directory) throw new Error("No directory");
this.directory = directory;
}

async export(): Promise<boolean | string> {
public override async export(): Promise<boolean | string> {
if (!this.filename) throw new Error("No filename");
if (!this.extension) throw new Error("No extension");
if (!this.getFormattedTitle()) throw new Error("No title");

const final_filename =
const finalFilename =
sanitize(this.getFormattedTitle()) + "." + this.extension;

this.final_path = path.join(this.directory, final_filename);
this.final_path = path.join(this.directory, finalFilename);

if (fs.existsSync(this.final_path)) {
throw new Error(`File already exists: ${this.final_path}`);
Expand Down Expand Up @@ -67,9 +67,16 @@ export class FileExporter extends BaseExporter {
return fs.existsSync(this.final_path) ? this.final_path : false;
}

async verify(): Promise<boolean> {
public override async verify(): Promise<boolean> {
return await new Promise<boolean>((resolve, reject) => {
resolve(fs.existsSync(this.final_path));
// resolve(fs.existsSync(this.final_path));
fs.access(this.final_path, fs.constants.F_OK, (err) => {
if (err) {
reject(err);
} else {
resolve(true);
}
});
});
}
}
Loading

0 comments on commit 194ca35

Please sign in to comment.