mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-14 00:31:25 +10:00
FlatLibrary provider (#127)
This commit is contained in:
@ -1,4 +1,3 @@
|
||||
import type { Readable } from "stream";
|
||||
import type { LibraryBackend } from "~/prisma/client";
|
||||
|
||||
export abstract class LibraryProvider<CFG> {
|
||||
@ -57,7 +56,7 @@ export abstract class LibraryProvider<CFG> {
|
||||
version: string,
|
||||
filename: string,
|
||||
options?: { start?: number; end?: number },
|
||||
): Promise<Readable | undefined>;
|
||||
): Promise<ReadableStream | undefined>;
|
||||
}
|
||||
|
||||
export class GameNotFoundError extends Error {}
|
||||
|
||||
@ -3,12 +3,11 @@ import {
|
||||
GameNotFoundError,
|
||||
VersionNotFoundError,
|
||||
type LibraryProvider,
|
||||
} from "./provider";
|
||||
} from "../provider";
|
||||
import { LibraryBackend } from "~/prisma/client";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import droplet from "@drop-oss/droplet";
|
||||
import type { Readable } from "stream";
|
||||
|
||||
export const FilesystemProviderConfig = type({
|
||||
baseDir: "string",
|
||||
@ -86,24 +85,29 @@ export class FilesystemProvider
|
||||
return manifest;
|
||||
}
|
||||
|
||||
// TODO: move this over to the droplet.readfile function it works
|
||||
async peekFile(game: string, version: string, filename: string) {
|
||||
const filepath = path.join(this.config.baseDir, game, version);
|
||||
if (!fs.existsSync(filepath)) return undefined;
|
||||
const stat = droplet.peekFile(filepath, filename);
|
||||
return { size: stat };
|
||||
}
|
||||
|
||||
async readFile(
|
||||
game: string,
|
||||
version: string,
|
||||
filename: string,
|
||||
options?: { start?: number; end?: number },
|
||||
): Promise<Readable | undefined> {
|
||||
const filepath = path.join(this.config.baseDir, game, version, filename);
|
||||
) {
|
||||
const filepath = path.join(this.config.baseDir, game, version);
|
||||
if (!fs.existsSync(filepath)) return undefined;
|
||||
const stream = fs.createReadStream(filepath, options);
|
||||
const stream = droplet.readFile(
|
||||
filepath,
|
||||
filename,
|
||||
options?.start,
|
||||
options?.end,
|
||||
);
|
||||
if (!stream) return undefined;
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
async peekFile(game: string, version: string, filename: string) {
|
||||
const filepath = path.join(this.config.baseDir, game, version, filename);
|
||||
if (!fs.existsSync(filepath)) return undefined;
|
||||
const stat = fs.statSync(filepath);
|
||||
return { size: stat.size };
|
||||
}
|
||||
}
|
||||
109
server/internal/library/providers/flat.ts
Normal file
109
server/internal/library/providers/flat.ts
Normal file
@ -0,0 +1,109 @@
|
||||
import { ArkErrors, type } from "arktype";
|
||||
import type { LibraryProvider } from "../provider";
|
||||
import { VersionNotFoundError } from "../provider";
|
||||
import { LibraryBackend } from "~/prisma/client";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import droplet from "@drop-oss/droplet";
|
||||
|
||||
export const FlatFilesystemProviderConfig = type({
|
||||
baseDir: "string",
|
||||
});
|
||||
|
||||
export class FlatFilesystemProvider
|
||||
implements LibraryProvider<typeof FlatFilesystemProviderConfig.infer>
|
||||
{
|
||||
private config: typeof FlatFilesystemProviderConfig.infer;
|
||||
private myId: string;
|
||||
|
||||
constructor(rawConfig: unknown, id: string) {
|
||||
const config = FlatFilesystemProviderConfig(rawConfig);
|
||||
if (config instanceof ArkErrors) {
|
||||
throw new Error(
|
||||
`Failed to create filesystem provider: ${config.summary}`,
|
||||
);
|
||||
}
|
||||
|
||||
this.myId = id;
|
||||
this.config = config;
|
||||
|
||||
if (!fs.existsSync(this.config.baseDir))
|
||||
throw "Base directory does not exist.";
|
||||
}
|
||||
|
||||
type() {
|
||||
return LibraryBackend.FlatFilesystem;
|
||||
}
|
||||
id() {
|
||||
return this.myId;
|
||||
}
|
||||
|
||||
/**
|
||||
* These are basically our versions, but also our games.
|
||||
* @returns list of valid games
|
||||
*/
|
||||
async listGames() {
|
||||
const versionDirs = fs.readdirSync(this.config.baseDir);
|
||||
const validVersionDirs = versionDirs.filter((e) => {
|
||||
const fullDir = path.join(this.config.baseDir, e);
|
||||
return droplet.hasBackendForPath(fullDir);
|
||||
});
|
||||
return validVersionDirs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Doesn't do anything, just returns "default"
|
||||
* @param _game Ignored
|
||||
* @returns
|
||||
*/
|
||||
async listVersions(_game: string) {
|
||||
return ["default"];
|
||||
}
|
||||
|
||||
async versionReaddir(game: string, _version: string) {
|
||||
const versionDir = path.join(this.config.baseDir, game);
|
||||
if (!fs.existsSync(versionDir)) throw new VersionNotFoundError();
|
||||
return droplet.listFiles(versionDir);
|
||||
}
|
||||
|
||||
async generateDropletManifest(
|
||||
game: string,
|
||||
_version: string,
|
||||
progress: (err: Error | null, v: number) => void,
|
||||
log: (err: Error | null, v: string) => void,
|
||||
) {
|
||||
const versionDir = path.join(this.config.baseDir, game);
|
||||
if (!fs.existsSync(versionDir)) throw new VersionNotFoundError();
|
||||
const manifest = await new Promise<string>((r, j) =>
|
||||
droplet.generateManifest(versionDir, progress, log, (err, result) => {
|
||||
if (err) return j(err);
|
||||
r(result);
|
||||
}),
|
||||
);
|
||||
return manifest;
|
||||
}
|
||||
async peekFile(game: string, _version: string, filename: string) {
|
||||
const filepath = path.join(this.config.baseDir, game);
|
||||
if (!fs.existsSync(filepath)) return undefined;
|
||||
const stat = droplet.peekFile(filepath, filename);
|
||||
return { size: stat };
|
||||
}
|
||||
async readFile(
|
||||
game: string,
|
||||
_version: string,
|
||||
filename: string,
|
||||
options?: { start?: number; end?: number },
|
||||
) {
|
||||
const filepath = path.join(this.config.baseDir, game);
|
||||
if (!fs.existsSync(filepath)) return undefined;
|
||||
const stream = droplet.readFile(
|
||||
filepath,
|
||||
filename,
|
||||
options?.start,
|
||||
options?.end,
|
||||
);
|
||||
if (!stream) return undefined;
|
||||
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user