Kanal detay sayfasına "Kanalı Sil" eklendi — kanal + tüm session/ segment/candidate geçmişini (dosyalar dahil) siler, ana sayfaya yönlendirir. Yanlışlıkla/test amaçlı eklenen kanalları temizlemek için. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
77 lines
2.5 KiB
TypeScript
77 lines
2.5 KiB
TypeScript
"use server";
|
||
|
||
import { unlink } from "node:fs/promises";
|
||
import { prisma } from "@streamclipper/db";
|
||
import { revalidatePath } from "next/cache";
|
||
import { redirect } from "next/navigation";
|
||
|
||
export async function addChannel(formData: FormData) {
|
||
const name = String(formData.get("name") ?? "").trim();
|
||
const youtubeHandle = String(formData.get("youtubeHandle") ?? "").trim();
|
||
const channelId = String(formData.get("channelId") ?? "").trim();
|
||
|
||
if (!name || !youtubeHandle || !channelId) {
|
||
throw new Error("Kanal adı, handle ve channel_id alanları zorunlu.");
|
||
}
|
||
|
||
await prisma.channel.create({
|
||
data: { name, youtubeHandle, channelId, isActive: true },
|
||
});
|
||
|
||
revalidatePath("/");
|
||
}
|
||
|
||
export async function deleteRawSegment(segmentId: string) {
|
||
const segment = await prisma.rawSegment.findUnique({ where: { id: segmentId } });
|
||
if (!segment) return;
|
||
|
||
await prisma.candidateSegment.deleteMany({ where: { rawSegmentId: segmentId } });
|
||
await prisma.rawSegment.delete({ where: { id: segmentId } });
|
||
await unlink(segment.filePath).catch(() => {});
|
||
|
||
revalidatePath("/segments");
|
||
revalidatePath("/", "layout");
|
||
}
|
||
|
||
export async function deleteCandidateSegment(candidateId: string) {
|
||
await prisma.candidateSegment.delete({ where: { id: candidateId } });
|
||
|
||
revalidatePath("/segments");
|
||
revalidatePath("/", "layout");
|
||
}
|
||
|
||
export async function deleteChannel(channelId: string) {
|
||
const sessions = await prisma.streamSession.findMany({
|
||
where: { channelId },
|
||
include: { segments: true },
|
||
});
|
||
const rawSegmentIds = sessions.flatMap((s) => s.segments.map((seg) => seg.id));
|
||
const filePaths = sessions.flatMap((s) => s.segments.map((seg) => seg.filePath));
|
||
|
||
await prisma.candidateSegment.deleteMany({ where: { rawSegmentId: { in: rawSegmentIds } } });
|
||
await prisma.rawSegment.deleteMany({ where: { sessionId: { in: sessions.map((s) => s.id) } } });
|
||
await prisma.streamSession.deleteMany({ where: { channelId } });
|
||
await prisma.channel.delete({ where: { id: channelId } });
|
||
|
||
await Promise.all(filePaths.map((p) => unlink(p).catch(() => {})));
|
||
|
||
revalidatePath("/", "layout");
|
||
redirect("/");
|
||
}
|
||
|
||
export async function updateYtdlpCookies(formData: FormData) {
|
||
const value = String(formData.get("cookies") ?? "").trim();
|
||
|
||
if (!value) {
|
||
throw new Error("Cookie içeriği boş olamaz.");
|
||
}
|
||
|
||
await prisma.appSetting.upsert({
|
||
where: { key: "ytdlp_cookies" },
|
||
create: { key: "ytdlp_cookies", value },
|
||
update: { value },
|
||
});
|
||
|
||
revalidatePath("/settings");
|
||
}
|