3 Commits
Author SHA1 Message Date
Worph bcd1eea5a2 gitattributes
Build and Push Docker Image / build (push) Failing after 13s
2026-07-14 16:01:30 +02:00
Worph bee5734251 fix webdav dependencies
Build and Push Docker Image / build (push) Failing after 14s
2026-07-12 19:20:15 +02:00
Worph 0d57dbeee3 add cache 2026-07-05 22:30:06 +02:00
3 changed files with 256 additions and 139 deletions
+6
View File
@@ -0,0 +1,6 @@
# FIX CRLF always when developer has not set
# Linux/Mac: git config --global core.autocrlf input
# Windows: git config --global core.autocrlf true
# Auto detect text files and perform LF normalization
* text=auto
* eol=lf
+174 -112
View File
@@ -9,7 +9,8 @@
* PLUGIN FILE ACCESS ARCHITECTURE * PLUGIN FILE ACCESS ARCHITECTURE
* ============================================================================ * ============================================================================
* *
* File access via WebDAV (WEBDAV_URL environment variable): * File access via WebDAV, on the meta-core named by each /process request
* (its /urls -> webdavUrlInternal; WEBDAV_URL overrides):
* - Read media files: GET /webdav/watch/... or /webdav/test/... * - Read media files: GET /webdav/watch/... or /webdav/test/...
* - Write output: PUT /webdav/plugin/tmdb/... * - Write output: PUT /webdav/plugin/tmdb/...
* - Cache: Local /cache mount (for JSON cache files) * - Cache: Local /cache mount (for JSON cache files)
@@ -18,6 +19,7 @@
* - No output mount needed on plugin containers * - No output mount needed on plugin containers
* - Consistent read/write architecture via HTTP * - Consistent read/write architecture via HTTP
* - Works in any orchestration environment * - Works in any orchestration environment
* - Posters land in the same core the CIDs are written to, always
* *
* ============================================================================ * ============================================================================
* *
@@ -30,21 +32,12 @@
*/ */
import axios from 'axios'; import axios from 'axios';
import { statSync, openSync, readSync, closeSync } from 'fs';
import { createHash } from 'crypto'; import { createHash } from 'crypto';
import * as path from 'path'; import * as path from 'path';
import type { PluginManifest, ProcessRequest, CallbackPayload } from './types.js'; import type { PluginManifest, ProcessRequest, CallbackPayload } from './types.js';
import { MetaCoreClient } from './meta-core-client.js'; import { MetaCoreClient } from './meta-core-client.js';
import { readJson, writeJson } from './cache.js'; import { readJson, writeJson } from './cache.js';
import { createWebDAVClient, WebDAVClient } from './webdav-client.js'; import { getWebDAVClient, WebDAVClient } from './webdav-client.js';
// Initialize WebDAV client - required for plugin operation
const webdavClient = createWebDAVClient();
if (webdavClient) {
console.log('[tmdb] WebDAV client initialized for file access');
} else {
console.warn('[tmdb] WARNING: WEBDAV_URL not set - plugin will not be able to write output files');
}
const IMAGE_BASE_URL = 'https://image.tmdb.org/t/p/original'; const IMAGE_BASE_URL = 'https://image.tmdb.org/t/p/original';
@@ -52,54 +45,14 @@ const IMAGE_BASE_URL = 'https://image.tmdb.org/t/p/original';
* ============================================================================ * ============================================================================
* PLUGIN OUTPUT PATH - Written via WebDAV * PLUGIN OUTPUT PATH - Written via WebDAV
* ============================================================================ * ============================================================================
* Output files (posters, backdrops) are written via WebDAV PUT requests. * Output files (posters, backdrops) are written via WebDAV PUT requests, to the
* Path: /files/plugin/tmdb/<filename> (accessible at WEBDAV_URL/plugin/tmdb/) * WebDAV of the meta-core that issued the /process call.
* Path: /files/plugin/tmdb/<filename>
* CACHE_PATH (/cache) - Local mount for plugin cache (handled by cache.ts) * CACHE_PATH (/cache) - Local mount for plugin cache (handled by cache.ts)
* ============================================================================ * ============================================================================
*/ */
const PLUGIN_OUTPUT_WEBDAV_PATH = '/files/plugin/tmdb'; const PLUGIN_OUTPUT_WEBDAV_PATH = '/files/plugin/tmdb';
/**
* Compute midhash256 CID for a file (matches meta-hash algorithm)
*
* Algorithm:
* - For files <= 1MB: Hashes entire file content + 8-byte size prefix
* - For files > 1MB: Hashes middle 1MB + 8-byte size prefix
* - Returns CIDv1 with custom codec 0x1000
*/
function computeMidHash256Sync(filePath: string): string {
const SAMPLE_SIZE = 1024 * 1024; // 1MB
// varint encoding of 0x1000 (4096) for both codec and hash function code
const MIDHASH_VARINT = Buffer.from([0x80, 0x20]);
// Get file size
const stats = statSync(filePath);
const fileSize = stats.size;
// Create size buffer (64-bit big-endian)
const sizeBuffer = Buffer.allocUnsafe(8);
sizeBuffer.writeBigUInt64BE(BigInt(fileSize), 0);
// Extract sample data
let sampleData: Buffer;
if (fileSize <= SAMPLE_SIZE) {
// Small file: read entire content
const fd = openSync(filePath, 'r');
sampleData = Buffer.allocUnsafe(fileSize);
readSync(fd, sampleData, 0, fileSize, 0);
closeSync(fd);
} else {
// Large file: read middle 1MB
const middleOffset = Math.floor((fileSize - SAMPLE_SIZE) / 2);
const fd = openSync(filePath, 'r');
sampleData = Buffer.allocUnsafe(SAMPLE_SIZE);
readSync(fd, sampleData, 0, SAMPLE_SIZE, middleOffset);
closeSync(fd);
}
return computeMidHash256FromData(fileSize, sampleData);
}
/** /**
* Compute midhash256 CID via WebDAV * Compute midhash256 CID via WebDAV
*/ */
@@ -170,16 +123,6 @@ function computeMidHash256FromData(fileSize: number, sampleData: Buffer): string
return cid; return cid;
} }
/**
* Compute midhash256 CID for a file (auto-selects WebDAV or filesystem)
*/
async function computeMidHash256(filePath: string): Promise<string> {
if (webdavClient) {
return computeMidHash256WebDAV(webdavClient, filePath);
}
return computeMidHash256Sync(filePath);
}
export const manifest: PluginManifest = { export const manifest: PluginManifest = {
id: 'tmdb', id: 'tmdb',
name: 'TMDB Metadata', name: 'TMDB Metadata',
@@ -352,6 +295,7 @@ async function downloadImageToWebDAV(
* no need to store any path in the parent file's metadata. * no need to store any path in the parent file's metadata.
*/ */
async function downloadAndHashImage( async function downloadAndHashImage(
webdavClient: WebDAVClient | null,
imagePath: string, imagePath: string,
imageType: string, imageType: string,
title: string, title: string,
@@ -409,6 +353,9 @@ export async function process(
): Promise<void> { ): Promise<void> {
const startTime = Date.now(); const startTime = Date.now();
const metaCore = new MetaCoreClient(request.metaCoreUrl); const metaCore = new MetaCoreClient(request.metaCoreUrl);
// WebDAV of the very core we're enriching, so poster blobs and the poster
// CIDs we write can never point at different cores. Cached per core URL.
const webdavClient = await getWebDAVClient(request.metaCoreUrl);
try { try {
const { cid, existingMeta } = request; const { cid, existingMeta } = request;
@@ -451,15 +398,26 @@ export async function process(
console.log(`[tmdb] Force recompute enabled for ${request.filePath ?? existingMeta?.fileName ?? cid}`); console.log(`[tmdb] Force recompute enabled for ${request.filePath ?? existingMeta?.fileName ?? cid}`);
} }
// Get midhash for caching const label = request.filePath ?? existingMeta?.fileName ?? cid;
const midhash = existingMeta?.['cid_midhash256'];
// Check cache (skip if forceRecompute) // Exact-file cache, keyed by the file's byte-hash. Only records carrying
// real file bytes have `cid_midhash256`; gateway/usenet records don't —
// which is exactly why the per-tmdbid cache below exists. Stores a full
// entry now, but tolerates the legacy raw-payload format from older builds.
const midhash = existingMeta?.['cid_midhash256'];
if (midhash && !forceRecompute) { if (midhash && !forceRecompute) {
const cachedData = await readJson<any>(`${midhash}_tmdb.json`); const cached = await readJson<any>(`${midhash}_tmdb.json`);
if (cachedData) { if (cached) {
console.log(`[tmdb] Using cached TMDB data for ${request.filePath ?? existingMeta?.fileName ?? cid}`); console.log(`[tmdb] Using byte-hash cached TMDB data for ${label}`);
await applyTmdbData(metaCore, cid, cachedData); const isEntry = cached.data !== undefined;
await applyTmdbData(
metaCore,
webdavClient,
cid,
isEntry ? cached.data : cached,
// Legacy raw payloads carry no image CIDs → let applyTmdbData recompute.
isEntry ? { posterCid: cached.posterCid, backdropCid: cached.backdropCid } : undefined
);
await sendCallback({ await sendCallback({
taskId: request.taskId, taskId: request.taskId,
status: 'completed', status: 'completed',
@@ -469,33 +427,37 @@ export async function process(
} }
} }
let tmdbData = null; // Resolve the show/movie entry, deduped per tmdbid. A record that already
// carries a tmdbid anchor (every gateway torrent/usenet hit does) takes
// Resolve by a known TMDB id first (authoritative — e.g. a gateway // the cached + single-flighted fast path, so N releases of one show share
// anchor or jellyfin-nfo). Completes the full cycle (poster + plot + // ONE TMDB fetch + poster seed instead of N.
// genres) from an id alone, with no fuzzy re-search. let entry: TmdbCacheEntry | null = null;
const knownTmdbId = existingMeta?.tmdbid; const knownTmdbId = existingMeta?.tmdbid;
if (knownTmdbId) { if (knownTmdbId) {
const mediaType = const mediaType =
existingMeta?.videoType === 'tvshow' || existingMeta?.videoType === 'tv' existingMeta?.videoType === 'tvshow' || existingMeta?.videoType === 'tv'
? 'tv' ? 'tv'
: 'movie'; : 'movie';
tmdbData = await getByTmdbId(knownTmdbId, mediaType); entry = await resolveEntryById(webdavClient, knownTmdbId, mediaType);
} }
// Try by IMDB ID // Fallback: no usable id → resolve by IMDB id or fuzzy title, then cache
// the result by its resolved tmdbid so later releases of the same show
// take the fast path above.
if (!entry) {
let tmdbData: any = null;
const imdbId = existingMeta?.imdbid; const imdbId = existingMeta?.imdbid;
if (!tmdbData && imdbId) { if (imdbId) {
tmdbData = await findByImdbId(imdbId); tmdbData = await findByImdbId(imdbId);
} }
// Try by title search
if (!tmdbData) { if (!tmdbData) {
let title = existingMeta?.originalTitle || existingMeta?.fileName; let title = existingMeta?.originalTitle || existingMeta?.fileName;
const year = existingMeta?.movieYear; const year = existingMeta?.movieYear;
const videoType = existingMeta?.videoType; const videoType = existingMeta?.videoType;
// Strip trailing year from title if present (e.g., "Sintel 2010" -> "Sintel") // Strip trailing year from title if present ("Sintel 2010" -> "Sintel").
if (title && year) { if (title && year) {
const yearRegex = new RegExp(`\\s*[\\(\\[]?${year}[\\)\\]]?\\s*$`); const yearRegex = new RegExp(`\\s*[\\(\\[]?${year}[\\)\\]]?\\s*$`);
title = title.replace(yearRegex, '').trim(); title = title.replace(yearRegex, '').trim();
@@ -507,16 +469,27 @@ export async function process(
} }
if (tmdbData) { if (tmdbData) {
await applyTmdbData(metaCore, cid, tmdbData); const images = await computeImageCids(webdavClient, tmdbData);
entry = { data: tmdbData, ...images };
// Cache TMDB data for future use if (tmdbData.id) {
if (midhash) { await writeJson(tmdbCacheKey(String(tmdbData.id), mediaTypeOf(tmdbData)), entry);
await writeJson(`${midhash}_tmdb.json`, tmdbData); }
}
} }
console.log(`[tmdb] Fetched TMDB data for ${request.filePath ?? existingMeta?.fileName ?? cid}`); if (entry) {
await applyTmdbData(metaCore, webdavClient, cid, entry.data, {
posterCid: entry.posterCid,
backdropCid: entry.backdropCid,
});
// Keep the exact-file cache warm too, so a future re-enrichment of
// this same file skips straight to the byte-hash hit.
if (midhash) {
await writeJson(`${midhash}_tmdb.json`, entry);
}
console.log(`[tmdb] Enriched ${label} (tmdbid=${entry.data.id})`);
} else { } else {
console.log(`[tmdb] No TMDB match found for ${request.filePath ?? existingMeta?.fileName ?? cid}`); console.log(`[tmdb] No TMDB match found for ${label}`);
} }
await sendCallback({ await sendCallback({
@@ -534,14 +507,109 @@ export async function process(
} }
} }
/** Resolved TMDB payload plus the seeded poster/backdrop CIDs, cached per
* tmdbid. Every torrent/usenet release of an episode carries the same
* `tmdbid`, and TMDB only gives us show/movie-level fields (poster, plot,
* genres — no per-episode data), so N releases can share ONE fetch + seed. */
interface TmdbCacheEntry {
data: any;
posterCid: string | null;
backdropCid: string | null;
}
// In-process single-flight: collapses concurrent resolves of the same key onto
// one promise, so a burst of same-tmdbid records that all miss the cold cache
// triggers a single TMDB fetch (no thundering herd) — the rest await it.
const tmdbEntryInflight = new Map<string, Promise<TmdbCacheEntry | null>>();
// Persistent cache filename for a resolved show/movie. Includes mediaType +
// language so a tv/movie id clash or a language switch can't return stale text.
function tmdbCacheKey(tmdbId: string, mediaType: string): string {
return `tmdbid_${mediaType}_${tmdbId}_${metadataLanguage}_tmdb.json`;
}
// Media type from a raw TMDB payload's own shape (tv payloads carry
// name/first_air_date). Mirrors the check in applyTmdbData.
function mediaTypeOf(data: any): string {
return data?.first_air_date !== undefined ||
data?.name !== undefined ||
data?.original_name !== undefined
? 'tv'
: 'movie';
}
// Download + hash the poster/backdrop for a resolved payload. Record-independent
// (the image filename is keyed by tmdbId, so the blob dedups across records), so
// the CIDs can be computed once per show and reused for every release.
async function computeImageCids(
webdavClient: WebDAVClient | null,
data: any
): Promise<{ posterCid: string | null; backdropCid: string | null }> {
const tmdbId = data?.id ? String(data.id) : '';
const displayTitle =
data?.title || data?.name || data?.original_title || data?.original_name || 'Unknown';
const releaseDate = data?.release_date || data?.first_air_date;
const year = releaseDate ? releaseDate.split('-')[0] : undefined;
const posterCid =
data?.poster_path && tmdbId
? await downloadAndHashImage(webdavClient, data.poster_path, 'poster', displayTitle, year, tmdbId)
: null;
const backdropCid =
data?.backdrop_path && tmdbId
? await downloadAndHashImage(webdavClient, data.backdrop_path, 'backdrop', displayTitle, year, tmdbId)
: null;
return { posterCid, backdropCid };
}
// Resolve the full entry (payload + image CIDs) for a KNOWN tmdbid, served from
// the persistent per-tmdbid cache when possible and single-flighted otherwise.
// This is the hot path for gateway records (torrent + usenet), which always
// carry a `tmdbid` anchor — so N releases of one show share one TMDB fetch.
async function resolveEntryById(
webdavClient: WebDAVClient | null,
tmdbId: string,
mediaType: string
): Promise<TmdbCacheEntry | null> {
const key = tmdbCacheKey(tmdbId, mediaType);
if (!forceRecompute) {
const cached = await readJson<TmdbCacheEntry>(key);
if (cached?.data) {
return cached;
}
}
const inflight = tmdbEntryInflight.get(key);
if (inflight) {
return inflight;
}
const pending = (async (): Promise<TmdbCacheEntry | null> => {
console.log(`[tmdb] Resolving TMDB ${mediaType}/${tmdbId} (cache miss)`);
const data = await getByTmdbId(tmdbId, mediaType);
if (!data) {
return null;
}
const images = await computeImageCids(webdavClient, data);
const entry: TmdbCacheEntry = { data, ...images };
await writeJson(key, entry);
return entry;
})();
tmdbEntryInflight.set(key, pending);
try {
return await pending;
} finally {
tmdbEntryInflight.delete(key);
}
}
/** /**
* Apply TMDB data to KV store using same keys as old TMDBProcessor * Apply TMDB data to KV store using same keys as old TMDBProcessor
*/ */
async function applyTmdbData( async function applyTmdbData(
metaCore: MetaCoreClient, metaCore: MetaCoreClient,
webdavClient: WebDAVClient | null,
cid: string, cid: string,
data: any data: any,
): Promise<void> { images?: { posterCid: string | null; backdropCid: string | null }
): Promise<{ posterCid: string | null; backdropCid: string | null }> {
const metadata: Record<string, string> = {}; const metadata: Record<string, string> = {};
// Basic IDs // Basic IDs
@@ -625,35 +693,29 @@ async function applyTmdbData(
// Add tmdb-verified tag (same as old processor) // Add tmdb-verified tag (same as old processor)
await metaCore.addToSet(cid, 'tags', 'tmdb-verified'); await metaCore.addToSet(cid, 'tags', 'tmdb-verified');
// Download poster and backdrop images to plugin output folder. The // Poster/backdrop CIDs. Reuse the precomputed values when the caller already
// watcher picks them up as first-class file entries, so all we need to // seeded them (a per-tmdbid cache hit — so we don't re-download + re-hash the
// store is the CID — `/api/file/{cid}` resolves through the standard // same image for every release of a show); otherwise download + hash now.
// reverse index, no path companion needed. // The image filename is keyed by tmdbId, so the blob is deduped either way —
if (data.poster_path && tmdbId) { // this just also skips the WebDAV round-trips. The watcher picks the images
const posterCid = await downloadAndHashImage( // up as first-class file entries, so storing the CID is all that's needed.
data.poster_path, let posterCid = images ? images.posterCid : null;
'poster', if (!images && data.poster_path && tmdbId) {
displayTitle, posterCid = await downloadAndHashImage(webdavClient, data.poster_path, 'poster', displayTitle, year, tmdbId);
year, }
tmdbId
);
if (posterCid) { if (posterCid) {
await metaCore.setProperty(cid, 'poster', posterCid); await metaCore.setProperty(cid, 'poster', posterCid);
console.log(`[tmdb] Set poster CID: ${posterCid}`); console.log(`[tmdb] Set poster CID: ${posterCid}`);
} }
}
if (data.backdrop_path && tmdbId) { let backdropCid = images ? images.backdropCid : null;
const backdropCid = await downloadAndHashImage( if (!images && data.backdrop_path && tmdbId) {
data.backdrop_path, backdropCid = await downloadAndHashImage(webdavClient, data.backdrop_path, 'backdrop', displayTitle, year, tmdbId);
'backdrop', }
displayTitle,
year,
tmdbId
);
if (backdropCid) { if (backdropCid) {
await metaCore.setProperty(cid, 'backdrop', backdropCid); await metaCore.setProperty(cid, 'backdrop', backdropCid);
console.log(`[tmdb] Set backdrop CID: ${backdropCid}`); console.log(`[tmdb] Set backdrop CID: ${backdropCid}`);
} }
}
return { posterCid, backdropCid };
} }
+55 -6
View File
@@ -136,15 +136,64 @@ export class WebDAVClient {
} }
/** /**
* Create a WebDAV client from environment variables * WebDAV client for the meta-core that drove a /process call.
*
* The endpoint is read from that core's own /urls (`webdavUrlInternal`),
* so blobs always land in the /files of the core we write metadata to. A feeder
* whose plugins run beside it but enrich a remote hub core needs no per-stack
* wiring, and it is impossible to write posters into one core while pointing
* CIDs at another.
*
* WEBDAV_URL still wins when set — an explicit override for tests and for
* topologies where the core's advertised internal URL isn't reachable from here.
*
* Resolutions are cached per meta-core URL; failures are not, so a core that is
* still starting up is retried on the next task.
*/ */
export function createWebDAVClient(): WebDAVClient | null { const clientCache = new Map<string, Promise<WebDAVClient | null>>();
const webdavUrl = process.env.WEBDAV_URL;
if (!webdavUrl) { async function resolveWebDAVUrl(metaCoreUrl: string): Promise<string | null> {
console.warn('[webdav-client] WEBDAV_URL not set, WebDAV client unavailable'); // /urls, not /api/urls — metaCoreUrl points at the meta-core backend, whose
return null; // discovery routes are unprefixed (same base the /meta/* writes use).
const base = metaCoreUrl.replace(/\/$/, '');
const response = await fetch(`${base}/urls`, { signal: AbortSignal.timeout(5000) });
if (!response.ok) {
throw new Error(`GET ${base}/urls failed: ${response.status} ${response.statusText}`);
} }
const urls = await response.json() as { webdavUrlInternal?: string; webdavUrl?: string };
return urls.webdavUrlInternal ?? urls.webdavUrl ?? null;
}
export async function getWebDAVClient(metaCoreUrl: string): Promise<WebDAVClient | null> {
const override = process.env.WEBDAV_URL;
if (override) {
return new WebDAVClient(override);
}
let pending = clientCache.get(metaCoreUrl);
if (!pending) {
pending = resolveWebDAVUrl(metaCoreUrl).then((webdavUrl) => {
if (!webdavUrl) {
console.warn(`[webdav-client] meta-core ${metaCoreUrl} advertises no WebDAV URL`);
return null;
}
console.log(`[webdav-client] Using WebDAV endpoint ${webdavUrl} (from ${metaCoreUrl})`);
return new WebDAVClient(webdavUrl); return new WebDAVClient(webdavUrl);
});
clientCache.set(metaCoreUrl, pending);
}
try {
const client = await pending;
if (!client) {
clientCache.delete(metaCoreUrl);
}
return client;
} catch (error) {
clientCache.delete(metaCoreUrl);
console.warn(`[webdav-client] Failed to resolve WebDAV URL from ${metaCoreUrl}: ${error instanceof Error ? error.message : String(error)}`);
return null;
}
} }