refactor: pretty much the whole backend module hierarchy

This commit is contained in:
Aleksi Lassila
2025-02-11 02:40:41 +02:00
parent 6969525464
commit fa27f19975
96 changed files with 1786 additions and 2033 deletions

View File

@@ -35,8 +35,8 @@ class TorrentProvider extends SourceProvider {
name: string = 'torrent';
settingsManager: SettingsManager = new TorrentSettingsManager();
get proxyUrl() {
return `/api/sources/${this.name}/proxy`;
getProxyUrl(sourceId: string) {
return `/api/sources/${sourceId}/proxy`;
}
getMovieStreams = async (
@@ -136,7 +136,7 @@ class TorrentProvider extends SourceProvider {
throw new Error('Torrent not found');
}
const src = `${this.proxyUrl}/magnet?link=${encodeURIComponent(torrent?.link)}&reiverr_token=${context.token}`;
const src = `${this.getProxyUrl(context.sourceId)}/magnet?link=${encodeURIComponent(torrent?.link)}&reiverr_token=${context.token}`;
const files = await getFiles(context.userId, torrent.link);
@@ -146,7 +146,7 @@ class TorrentProvider extends SourceProvider {
.filter((f) => subtitleExtensions.some((ext) => f.name.endsWith(ext)))
.map((f) => ({
kind: 'subtitles',
src: `${this.proxyUrl}/magnet?link=${encodeURIComponent(torrent.link)}&reiverr_token=${context.token}&file=${f.name}`,
src: `${this.getProxyUrl(context.sourceId)}/magnet?link=${encodeURIComponent(torrent.link)}&reiverr_token=${context.token}&file=${f.name}`,
label: f.name,
lang: 'unknown',
}));
@@ -193,7 +193,7 @@ class TorrentProvider extends SourceProvider {
throw new Error('Torrent not found');
}
const src = `${this.proxyUrl}/magnet?link=${encodeURIComponent(torrent.link)}&reiverr_token=${context.token}&season=${metadata.season}&episode=${metadata.episode}`;
const src = `${this.getProxyUrl(context.sourceId)}/magnet?link=${encodeURIComponent(torrent.link)}&reiverr_token=${context.token}&season=${metadata.season}&episode=${metadata.episode}`;
const files = await getFiles(context.userId, torrent.link);
@@ -201,7 +201,7 @@ class TorrentProvider extends SourceProvider {
.filter((f) => subtitleExtensions.some((ext) => f.name.endsWith(ext)))
.map((f) => ({
kind: 'subtitles',
src: `${this.proxyUrl}/magnet?link=${encodeURIComponent(torrent.link)}&reiverr_token=${context.token}&file=${f.name}`,
src: `${this.getProxyUrl(context.sourceId)}/magnet?link=${encodeURIComponent(torrent.link)}&reiverr_token=${context.token}&file=${f.name}`,
label: f.name,
lang: 'unknown',
}));

View File

@@ -2,57 +2,6 @@ import * as path from 'path';
import * as fs from 'fs';
import * as torrentStream from 'torrent-stream';
// class StreamCache<T> {
// private streamCacheFile = path.join(
// __dirname,
// '..',
// '..',
// 'stream-cache.json',
// );
// cache: Record<string, T> = this.readStreamCache();
// constructor() {}
// private writeStreamCache(cache: Record<string, T>) {
// this.cache = cache;
// fs.writeFileSync(this.streamCacheFile, JSON.stringify(cache));
// }
// private readStreamCache(): Record<string, T> {
// if (fs.existsSync(this.streamCacheFile)) {
// const data = fs.readFileSync(this.streamCacheFile, 'utf8');
// return JSON.parse(data);
// }
// return {};
// }
// set(key: string, value: T) {
// this.cache[key] = value;
// this.writeStreamCache(this.cache);
// }
// update(key: string, fn: (value: T | undefined) => T) {
// const n = fn(this.get(key));
// this.cache[key] = n;
// this.writeStreamCache(this.cache);
// return n;
// }
// remove(key: string) {
// delete this.cache[key];
// this.writeStreamCache(this.cache);
// }
// get(key: string): T | undefined {
// return this.cache[key];
// }
// getAll() {
// return this.cache;
// }
// }
class FileCache<T> {
private cache: T;
@@ -126,46 +75,8 @@ class EngineCache {
});
}
}
// this.purge();
}
// private async purge() {
// const metadata = this.userTorrentMetadata.get();
// const activeTorrents = Object.keys(this.engineCache);
// const toDelete: string[] = activeTorrents.filter((infoHash) =>
// Object.values(metadata).some((m) => m.infoHash === infoHash),
// );
// for (const userId of Object.keys(metadata)) {
// const userMetadata = metadata[userId];
// if (userMetadata.lastAccessed < Date.now() - this.maxTorrentKeepAlive) {
// toDelete.push(userMetadata.infoHash);
// }
// }
// const torrents = await Promise.all(
// Object.entries(this.engineCache).map(
// async ([key, value]) => [key, await value] as const,
// ),
// );
// torrents.sort(([_, a], [__, b]) => {
// return a!.metadata.lastAccessed - b!.metadata.lastAccessed;
// });
// console.log('torrents sorted', torrents);
// const tasks = torrents.map(async ([infoHash, torrent], index) => {
// if (index < this.maxActiveTorrentsPerUser) return undefined;
// return this.destroyEngine(infoHash);
// });
// await Promise.all(tasks);
// }
private async destroyEngine(infoHash: string) {
const engine = await this.engineCache[infoHash];
@@ -205,7 +116,6 @@ class EngineCache {
res(engine);
});
engine.on('download', (e) => console.log('onDownload', magnetLink, e));
// engine.on('torrent', (e) => console.log('onTorrent', magnetLink, e));
engine.on('upload', (e) => console.log('onUpload', magnetLink, e));
});

View File

@@ -1,6 +1,9 @@
import type { PluginSettings, UserContext } from '../../plugin-types';
import type {
SourceProviderSettings,
UserContext,
} from '@aleksilassila/reiverr-plugin';
export interface TorrentSettings extends PluginSettings {
export interface TorrentSettings extends SourceProviderSettings {
apiKey: string;
baseUrl: string;
}