1- import { VercelRequest , VercelResponse } from ' @vercel/node' ;
2- import cors from ' cors' ;
3- import multer from ' multer' ;
1+ import { VercelRequest , VercelResponse } from " @vercel/node" ;
2+ import cors from " cors" ;
3+ import multer from " multer" ;
44
5- import { cmsService } from ' ../../src/services/cms' ;
6- import { authenticate , AuthenticatedRequest } from ' ../../src/utils/auth' ;
7- import { sendSuccess , sendError } from ' ../../src/utils/response' ;
5+ import { cmsService } from " ../../src/services/cms.js" ;
6+ import { authenticate , AuthenticatedRequest } from " ../../src/utils/auth.js" ;
7+ import { sendSuccess , sendError } from " ../../src/utils/response.js" ;
88
99// CORS configuration
1010const corsOptions = {
11- origin : process . env . CORS_ORIGIN || '*' ,
12- methods : [ ' POST' , ' OPTIONS' ] ,
13- allowedHeaders : [ ' Content-Type' , ' Authorization' ]
11+ origin : process . env . CORS_ORIGIN || "*" ,
12+ methods : [ " POST" , " OPTIONS" ] ,
13+ allowedHeaders : [ " Content-Type" , " Authorization" ] ,
1414} ;
1515
1616// Apply CORS middleware
@@ -25,7 +25,6 @@ function runCors(req: VercelRequest, res: VercelResponse) {
2525 } ) ;
2626}
2727
28-
2928// Multer configuration for memory storage
3029const upload = multer ( {
3130 storage : multer . memoryStorage ( ) ,
@@ -34,19 +33,25 @@ const upload = multer({
3433 } ,
3534 fileFilter : ( _req , file , cb ) => {
3635 // Check if file is an image
37- const allowedMimes = [ 'image/jpeg' , 'image/jpg' , 'image/png' , 'image/webp' , 'image/gif' ] ;
36+ const allowedMimes = [
37+ "image/jpeg" ,
38+ "image/jpg" ,
39+ "image/png" ,
40+ "image/webp" ,
41+ "image/gif" ,
42+ ] ;
3843 if ( allowedMimes . includes ( file . mimetype ) ) {
3944 cb ( null , true ) ;
4045 } else {
41- cb ( new Error ( ' Only image files are allowed' ) ) ;
46+ cb ( new Error ( " Only image files are allowed" ) ) ;
4247 }
43- }
48+ } ,
4449} ) ;
4550
4651// Wrapper to promisify multer
4752function runMulter ( req : VercelRequest , res : VercelResponse ) {
4853 return new Promise ( ( resolve , reject ) => {
49- upload . single ( ' image' ) ( req as any , res as any , ( error : unknown ) => {
54+ upload . single ( " image" ) ( req as any , res as any , ( error : unknown ) => {
5055 if ( error ) {
5156 return reject ( error ) ;
5257 }
@@ -55,53 +60,82 @@ function runMulter(req: VercelRequest, res: VercelResponse) {
5560 } ) ;
5661}
5762
58-
59- export default async function handler ( req : AuthenticatedRequest , res : VercelResponse ) {
63+ export default async function handler (
64+ req : AuthenticatedRequest ,
65+ res : VercelResponse
66+ ) {
6067 await runCors ( req , res ) ;
6168
6269 // Handle preflight requests
63- if ( req . method === ' OPTIONS' ) {
70+ if ( req . method === " OPTIONS" ) {
6471 return res . status ( 200 ) . end ( ) ;
6572 }
6673
67- if ( req . method !== 'POST' ) {
68- return sendError ( res , 'METHOD_NOT_ALLOWED' , `Method ${ req . method } not allowed` , 405 ) ;
74+ if ( req . method !== "POST" ) {
75+ return sendError (
76+ res ,
77+ "METHOD_NOT_ALLOWED" ,
78+ `Method ${ req . method } not allowed` ,
79+ 405
80+ ) ;
6981 }
7082
7183 // Check authentication
7284 if ( ! authenticate ( req ) ) {
73- return sendError ( res , 'UNAUTHORIZED' , 'Invalid or missing authentication token' , 401 ) ;
85+ return sendError (
86+ res ,
87+ "UNAUTHORIZED" ,
88+ "Invalid or missing authentication token" ,
89+ 401
90+ ) ;
7491 }
7592
76- const projectId = process . env . REEARTH_CMS_PROJECT_ID || ' default-project-id' ;
93+ const projectId = process . env . REEARTH_CMS_PROJECT_ID || " default-project-id" ;
7794
7895 try {
7996 // Process file upload
8097 await runMulter ( req , res ) ;
81-
98+
8299 const file = ( req as any ) . file ;
83-
100+
84101 if ( ! file ) {
85- return sendError ( res , ' NO_FILE' , ' No image file provided' , 400 ) ;
102+ return sendError ( res , " NO_FILE" , " No image file provided" , 400 ) ;
86103 }
87104
88105 // Upload to CMS
89106 const uploadedAsset = await cmsService . uploadAsset ( projectId , file ) ;
90107
91108 return sendSuccess ( res , uploadedAsset , 201 ) ;
92-
93109 } catch ( error : unknown ) {
94- console . error ( 'Upload error:' , error ) ;
95-
96- if ( error instanceof Error && error . message === 'Only image files are allowed' ) {
97- return sendError ( res , 'INVALID_FILE_TYPE' , 'Only image files are allowed' , 400 ) ;
110+ console . error ( "Upload error:" , error ) ;
111+
112+ if (
113+ error instanceof Error &&
114+ error . message === "Only image files are allowed"
115+ ) {
116+ return sendError (
117+ res ,
118+ "INVALID_FILE_TYPE" ,
119+ "Only image files are allowed" ,
120+ 400
121+ ) ;
98122 }
99-
100- if ( error && typeof error === 'object' && 'code' in error && error . code === 'LIMIT_FILE_SIZE' ) {
101- return sendError ( res , 'FILE_TOO_LARGE' , 'File size exceeds 10MB limit' , 413 ) ;
123+
124+ if (
125+ error &&
126+ typeof error === "object" &&
127+ "code" in error &&
128+ error . code === "LIMIT_FILE_SIZE"
129+ ) {
130+ return sendError (
131+ res ,
132+ "FILE_TOO_LARGE" ,
133+ "File size exceeds 10MB limit" ,
134+ 413
135+ ) ;
102136 }
103-
104- return sendError ( res , ' UPLOAD_FAILED' , ' File upload failed' , 500 ) ;
137+
138+ return sendError ( res , " UPLOAD_FAILED" , " File upload failed" , 500 ) ;
105139 }
106140}
107141
@@ -110,4 +144,4 @@ export const config = {
110144 api : {
111145 bodyParser : false ,
112146 } ,
113- } ;
147+ } ;
0 commit comments