"use server"; import { channelWriters } from "yt-dlp-transcript-common/controller/channelWriters"; import { adoptLocalMediaReference } from "yt-dlp-transcript-common/controller/adoptLocalMediaReference"; import { revalidatePath } from "next/cache"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { prepareLocalMediaRecording, type LocalRecordingPreparationRequest, previewLocalMedia } from "yt-dlp-transcript-common/controller/localMediaImport"; import { startLocalMediaImportJob, type LocalMediaJobRequest } from "yt-dlp-transcript-common/controller/localMediaImportJob"; // These are private editor actions, behind the same deployment access boundary // as the channel's existing actions. Do not expose them through the public export. // Client input is validated again by the controllers. No absolute source paths // are accepted for import; selections refer to the stored source and preview. export async function previewLibraryAction(slug: string) { try { return { ok: true as const, preview: await previewLocalMedia(getPaths(), slug) }; } catch (e) { const message = (e as Error).message ?? ""; if (message.startsWith("The library directory is inside")) return { ok: false as const, error: message }; return { ok: false as const, error: "Could not scan the library. Check its mount, channel settings, and relocation status." }; } } export async function importLibraryAction(slug: string, request: LocalMediaJobRequest) { try { return await startLocalMediaImportJob(getPaths(), slug, request); } catch { return { ok: false as const, error: "Could not start the import. Preview the library again and check channel availability." }; } } // One occupied recording per explicit click, not a silent merge during preview. // The controller revalidates all client tokens and exact companion bytes, refuses // active channel writers, and publishes without replacing an existing reference. // This uses the same private editor access boundary as library configuration. export async function linkExistingLibraryRecordingAction(slug: string, selection: {sourceToken: string; media: string; fingerprint: string}) { let result; try { result = await adoptLocalMediaReference(getPaths(), slug, selection, {apply: true}); } catch { return {ok: false as const, error: "Could not verify and link this recording. Preview again and check that source and archived metadata/transcript files match and the channel is idle."}; } // Publication already happened. Cache invalidation failure must not turn a // successful attachment into an apparent failure or encourage blind retries. try { revalidatePath(`/channels/${slug}/local-media`); } catch { /* A manual refresh is sufficient. */ } return {ok: true as const, result}; } export async function prepareLibraryRecordingAction(slug: string, request: LocalRecordingPreparationRequest) { try { if (channelWriters(slug, {includeQueued: true}).length) { return {ok: false as const, error: "Wait for channel work to finish before preparing details."}; } return {ok: true as const, review: await prepareLocalMediaRecording(getPaths(), slug, request)}; } catch { return {ok: false as const, error: "Could not prepare details. Check the title, date and duration, then preview again if the recording changed. Inspection requires ffprobe on the editor server; you can explicitly choose manual entry instead."}; } }