File tree Expand file tree Collapse file tree 7 files changed +435
-168
lines changed
Expand file tree Collapse file tree 7 files changed +435
-168
lines changed Original file line number Diff line number Diff line change 1+ import { NextResponse } from "next/server" ;
2+ import { getServerSession } from "next-auth" ;
3+ import { authOptions } from "@/app/api/auth/[...nextauth]/route" ;
4+ import prisma from "@/server/utills/db" ;
5+
6+ interface Material {
7+ productId : string ;
8+ quantity : number ;
9+ }
10+
11+ export async function POST ( req : Request ) {
12+ const session = await getServerSession ( authOptions ) ;
13+ if ( ! session ) {
14+ return new NextResponse ( "Unauthorized" , { status : 401 } ) ;
15+ }
16+
17+ const { projectId, filePath } = await req . json ( ) ;
18+
19+ if ( ! projectId || ! filePath ) {
20+ return new NextResponse ( "Project ID and file path are required" , {
21+ status : 400 ,
22+ } ) ;
23+ }
24+
25+ try {
26+ const materials : Material [ ] = [ ] ;
27+
28+ for ( const material of materials ) {
29+ await prisma . projectProduct . create ( {
30+ data : {
31+ projectId : projectId ,
32+ productId : material . productId ,
33+ quantity : material . quantity ,
34+ } ,
35+ } ) ;
36+ }
37+
38+ return NextResponse . json ( { message : "Materials uploaded successfully" } ) ;
39+ } catch ( error ) {
40+ console . error ( "Error uploading materials:" , error ) ;
41+ return new NextResponse ( "Internal Server Error" , { status : 500 } ) ;
42+ }
43+ }
Original file line number Diff line number Diff line change 1+ import { NextResponse } from "next/server" ;
2+ import fs from "fs" ;
3+ import path from "path" ;
4+
5+ export async function GET ( ) {
6+ const uploadDir = path . join ( process . cwd ( ) , "server" , "uploads" ) ;
7+
8+ try {
9+ const files = await fs . promises . readdir ( uploadDir ) ;
10+ const pdfFiles = files
11+ . filter ( ( file ) => file . endsWith ( ".pdf" ) )
12+ . map ( ( file ) => ( {
13+ name : file ,
14+ path : `uploads/${ file } ` ,
15+ } ) ) ;
16+
17+ return NextResponse . json ( pdfFiles ) ;
18+ } catch ( error ) {
19+ console . error ( "Error reading upload directory:" , error ) ;
20+ return new NextResponse ( "Internal Server Error" , { status : 500 } ) ;
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ import { NextResponse } from "next/server" ;
2+ import { getServerSession } from "next-auth" ;
3+ import { authOptions } from "@/app/api/auth/[...nextauth]/route" ;
4+ import { v4 as uuidv4 } from "uuid" ;
5+ import { promises as fs } from "fs" ;
6+ import path from "path" ;
7+
8+ export async function POST ( req : Request ) {
9+ const session = await getServerSession ( authOptions ) ;
10+ if ( ! session ) {
11+ return new NextResponse ( "Unauthorized" , { status : 401 } ) ;
12+ }
13+
14+ const formData = await req . formData ( ) ;
15+ const file = formData . get ( "uploadedFile" ) as File ;
16+
17+ if ( ! file ) {
18+ return new NextResponse ( "No file uploaded" , { status : 400 } ) ;
19+ }
20+
21+ if ( file . type !== "application/pdf" ) {
22+ return new NextResponse ( "Only PDF files are allowed" , { status : 400 } ) ;
23+ }
24+
25+ const uniqueFileName = `${ uuidv4 ( ) } -${ file . name } ` ;
26+ const uploadPath = path . join (
27+ process . cwd ( ) ,
28+ "server" ,
29+ "uploads" ,
30+ uniqueFileName
31+ ) ;
32+
33+ await fs . mkdir ( path . dirname ( uploadPath ) , { recursive : true } ) ;
34+
35+ const buffer = Buffer . from ( await file . arrayBuffer ( ) ) ;
36+ await fs . writeFile ( uploadPath , Uint8Array . from ( buffer ) ) ;
37+
38+ return NextResponse . json ( { filePath : `uploads/${ uniqueFileName } ` } ) ;
39+ }
You can’t perform that action at this time.
0 commit comments