Add labeled file materials with format badge
- Store human-readable label in LessonFile.name via optional label field on upload - Add PATCH endpoint to rename existing files inline - Admin: label input before upload, click-to-edit inline rename - Student: colored format badge (PDF/DOCX/XLSX/ZIP/etc) replaces paperclip emoji Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,7 @@ import { LessonContent } from "@/components/student/lesson-content";
|
||||
import { LessonCompleteButton } from "@/components/student/lesson-complete-button";
|
||||
import { HomeworkSection } from "@/components/student/homework-section";
|
||||
import { LessonComments } from "@/components/student/lesson-comments";
|
||||
import { FileFormatBadge } from "@/components/shared/file-format-badge";
|
||||
|
||||
interface Props {
|
||||
params: Promise<{ slug: string; lessonId: string }>;
|
||||
@@ -123,7 +124,7 @@ export default async function LessonPage({ params }: Props) {
|
||||
className="flex items-center gap-3 px-4 py-3 text-sm transition-colors hover:[border-color:var(--foreground)]"
|
||||
style={{ border: "2px solid var(--border)" }}
|
||||
>
|
||||
<span className="text-lg">📎</span>
|
||||
<FileFormatBadge url={file.url} />
|
||||
<span className="flex-1 font-medium">{file.name}</span>
|
||||
<span className="text-xs" style={{ color: "var(--muted-foreground)" }}>
|
||||
{formatSize(file.size)}
|
||||
|
||||
@@ -5,15 +5,21 @@ import { prisma } from "@/lib/prisma";
|
||||
import { uploadFile, deleteFile } from "@/lib/s3";
|
||||
import { randomUUID } from "crypto";
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
async function requireAdmin(req: NextRequest) {
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
if (!session || session.user.role !== "admin") {
|
||||
if (!session || session.user.role !== "admin") return null;
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
if (!await requireAdmin(req)) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const form = await req.formData();
|
||||
const file = form.get("file") as File | null;
|
||||
const lessonId = form.get("lessonId") as string | null;
|
||||
const label = (form.get("label") as string | null)?.trim() || null;
|
||||
if (!file || !lessonId) return NextResponse.json({ error: "Missing fields" }, { status: 400 });
|
||||
|
||||
const ext = file.name.split(".").pop() ?? "bin";
|
||||
@@ -22,15 +28,30 @@ export async function POST(req: NextRequest) {
|
||||
const url = await uploadFile(key, buffer, file.type);
|
||||
|
||||
const lessonFile = await prisma.lessonFile.create({
|
||||
data: { lessonId, name: file.name, url, size: file.size },
|
||||
data: { lessonId, name: label ?? file.name, url, size: file.size },
|
||||
});
|
||||
|
||||
return NextResponse.json(lessonFile);
|
||||
}
|
||||
|
||||
export async function PATCH(req: NextRequest) {
|
||||
if (!await requireAdmin(req)) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const { fileId, label } = await req.json();
|
||||
if (!fileId || typeof label !== "string") {
|
||||
return NextResponse.json({ error: "Missing fields" }, { status: 400 });
|
||||
}
|
||||
const updated = await prisma.lessonFile.update({
|
||||
where: { id: fileId },
|
||||
data: { name: label.trim() || undefined },
|
||||
});
|
||||
return NextResponse.json(updated);
|
||||
}
|
||||
|
||||
export async function DELETE(req: NextRequest) {
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
if (!session || session.user.role !== "admin") {
|
||||
if (!await requireAdmin(req)) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user