1- import { NextResponse } from 'next/server' ;
2- import { db } from '@src/server/db' ;
3- import { file } from '@src/server/db/schema/file' ;
4- import { and , eq } from 'drizzle-orm' ;
5- import { getServerAuthSession } from '@src/server/auth' ;
61import fs from 'fs' ;
72import path from 'path' ;
8-
3+ import { and , eq } from 'drizzle-orm' ;
4+ import { NextResponse } from 'next/server' ;
95import { z } from 'zod' ;
6+ import { getServerAuthSession } from '@src/server/auth' ;
7+ import { db } from '@src/server/db' ;
8+ import { file } from '@src/server/db/schema/file' ;
109
1110const allowedTypes = [ 'image/png' , 'image/jpeg' , 'application/pdf' ] ;
1211
@@ -15,10 +14,10 @@ const uploadFormSchema = z.object({
1514 prefix : z . string ( ) . min ( 1 , { message : 'Prefix is missing' } ) ,
1615 courseNumber : z . string ( ) . min ( 1 , { message : 'Course number is missing' } ) ,
1716 sectionCode : z . string ( ) . min ( 1 , { message : 'Section code is missing' } ) ,
18- professor : z . string ( ) . min ( 1 , { message : 'Professor is missing' } ) ,
17+ professor : z . string ( ) . min ( 1 , { message : 'Professor is missing' } ) ,
1918 term : z . enum ( [ 'Spring' , 'Summer' , 'Fall' ] ) ,
20- year : z . coerce . number ( { message : 'Year is missing' } ) ,
21- } )
19+ year : z . coerce . number ( { message : 'Year is missing' } ) ,
20+ } ) ;
2221
2322// Upload file to database w/ file metadata (Local)
2423export async function POST ( req : Request ) {
@@ -42,17 +41,11 @@ export async function POST(req: Request) {
4241 const newFile = data . file as File ;
4342
4443 if ( ! ( newFile instanceof File ) ) {
45- return NextResponse . json (
46- { error : 'Invalid file upload' } ,
47- { status : 400 } ,
48- ) ;
44+ return NextResponse . json ( { error : 'Invalid file upload' } , { status : 400 } ) ;
4945 }
50-
46+
5147 if ( newFile . size === 0 ) {
52- return NextResponse . json (
53- { error : 'File is empty' } ,
54- { status : 400 } ,
55- ) ;
48+ return NextResponse . json ( { error : 'File is empty' } , { status : 400 } ) ;
5649 }
5750
5851 if ( ! allowedTypes . includes ( newFile . type ) ) {
@@ -76,10 +69,7 @@ export async function POST(req: Request) {
7669 } ) ;
7770
7871 if ( ! sectionData ) {
79- return NextResponse . json (
80- { error : 'Section not found' } ,
81- { status : 404 } ,
82- ) ;
72+ return NextResponse . json ( { error : 'Section not found' } , { status : 404 } ) ;
8373 }
8474
8575 try {
@@ -96,10 +86,10 @@ export async function POST(req: Request) {
9686 const fileMetadata = {
9787 authorId : session . user . id ,
9888 sectionId : sectionData . id ,
99- fileTitle : newFile . name , // required by schema
100- fileName : newFile . name , // required by schema
89+ fileTitle : newFile . name , // required by schema
90+ fileName : newFile . name , // required by schema
10191 } ;
102-
92+
10393 const result = await db . insert ( file ) . values ( fileMetadata ) . returning ( ) ;
10494
10595 return NextResponse . json (
@@ -108,9 +98,6 @@ export async function POST(req: Request) {
10898 ) ;
10999 } catch ( err ) {
110100 console . error ( 'File upload error:' , err ) ;
111- return NextResponse . json (
112- { error : 'File upload failed' } ,
113- { status : 500 } ,
114- ) ;
101+ return NextResponse . json ( { error : 'File upload failed' } , { status : 500 } ) ;
115102 }
116103}
0 commit comments