convert flacs to aac for streaming

This commit is contained in:
2026-06-16 12:49:30 +02:00
parent f5e8b87846
commit 7aa1746f97
12 changed files with 401 additions and 8 deletions

View File

@@ -94,9 +94,14 @@ export const music = createTable(
id: d.uuid().primaryKey().notNull(),
title: d.varchar({ length: 100 }).notNull(),
description: d.text(),
// Original high-quality upload (e.g. FLAC), offered as a download.
fileUrl: d.varchar("file_url", { length: 500 }).notNull(),
fileKey: d.varchar("file_key", { length: 200 }).notNull(),
fileName: d.varchar("file_name", { length: 200 }).notNull(),
// Lighter, streaming-friendly transcode (e.g. AAC) used by the player.
streamUrl: d.varchar("stream_url", { length: 500 }),
streamKey: d.varchar("stream_key", { length: 200 }),
streamName: d.varchar("stream_name", { length: 200 }),
createdAt: d
.timestamp({ withTimezone: true })
.default(sql`CURRENT_TIMESTAMP`)

View File

@@ -5,7 +5,7 @@ import { eq } from "drizzle-orm";
import { isAdmin } from "~/app/actions";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { createMusicInputSchema, updateMusicInputSchema } from "~/lib/trpc/music/schemas";
import { createMusicInputSchema, updateMusicInputSchema, setStreamInputSchema } from "~/lib/trpc/music/schemas";
import { utapi } from "../uploadthing";
export const musicRouter = router({
list: publicProcedure.query(async () => {
@@ -33,6 +33,22 @@ export const musicRouter = router({
const { id, ...data } = input;
return db.update(music).set(data).where(eq(music.id, id)).returning();
}),
setStream: publicProcedure
.input(setStreamInputSchema)
.mutation(async ({ input }) => {
const admin = await isAdmin();
if (!admin) throw new TRPCError({ code: "FORBIDDEN", message: "Access denied" });
const { id, ...data } = input;
const existing = await db.select().from(music).where(eq(music.id, id)).limit(1);
const prev = existing.at(0);
if (!prev) throw new TRPCError({ code: "NOT_FOUND", message: "Track not found" });
const res = await db.update(music).set(data).where(eq(music.id, id)).returning();
// Drop the previous stream transcode so we don't orphan it on UploadThing.
if (prev.streamKey && prev.streamKey !== data.streamKey) {
utapi.deleteFiles(prev.streamKey);
}
return res.at(0);
}),
delete: publicProcedure
.input(z.object({id:z.string().uuid()}))
.mutation(async ({ input }) => {
@@ -41,7 +57,8 @@ export const musicRouter = router({
let res = await db.delete(music).where(eq(music.id, input.id)).returning();
let ret = res.at(0)
if (ret) {
utapi.deleteFiles(ret.fileKey)
const keys = [ret.fileKey, ret.streamKey].filter((k): k is string => !!k);
utapi.deleteFiles(keys)
}
return ret;
}),