11const express = require ( 'express' ) ;
22const router = express . Router ( ) ;
33const mongoose = require ( 'mongoose' ) ;
4+ const multer = require ( 'multer' ) ;
5+
6+ const FILE_TYPE_MAP = {
7+ 'image/png' : 'png' ,
8+ 'image/jpeg' : 'jpeg' ,
9+ 'image/jpg' : 'jpg' ,
10+ }
11+
12+ const storage = multer . diskStorage ( {
13+ destination : function ( req , file , cb ) {
14+ const isValid = FILE_TYPE_MAP [ file . mimetype ] ;
15+ let uploadError = new Error ( 'Invalid Image Type' ) ;
16+ if ( isValid ) {
17+ uploadError = null
18+ }
19+ cb ( uploadError , '/public/uploads' )
20+ } ,
21+ filename : function ( req , file , cb ) {
22+ const fileName = file . originalname . split ( '' ) . json ( '-' ) ;
23+ const extension = FILE_TYPE_MAP [ file . mimetype ] ;
24+ cb ( null , `${ fileName } -${ Date . now ( ) } .${ extension } ` )
25+ }
26+ } )
27+
28+ const upload = multer ( { storage : storage } )
429
530const Product = require ( '../models/product' ) ;
631const Category = require ( '../models/category' ) ;
@@ -30,7 +55,7 @@ router.get('/:id', async (req, res) => {
3055 res . status ( 200 ) . send ( product )
3156} )
3257
33- router . post ( '/' , async ( req , res ) => {
58+ router . post ( '/' , upload . single ( 'image' ) , async ( req , res ) => {
3459
3560 if ( ! mongoose . isValidObjectId ( req . params . id ) ) {
3661 res . status ( 400 ) . send ( 'Invalid Product ID' )
@@ -40,11 +65,18 @@ router.post('/', async (req, res) => {
4065 if ( ! category )
4166 return res . status ( 400 ) . send ( 'Invalid Category' )
4267
68+ const file = req . file ;
69+ if ( ! file )
70+ return res . status ( 400 ) . send ( 'No image in the request' )
71+
72+ const fileName = file . filename ;
73+ const basePath = `${ req . protocol } ://${ req . get ( 'host' ) } /public/uploads/` ;
74+
4375 let product = new Product ( {
4476 name : req . body . name ,
4577 description : req . body . description ,
4678 richDescription : req . body . richDescription ,
47- image : req . body . image ,
79+ image : ` ${ basePath } ${ fileName } ` ,
4880 brand : req . body . brand ,
4981 price : req . body . price ,
5082 category : req . body . category ,
@@ -120,5 +152,32 @@ router.get('/get/featured/:count', async (req, res) => {
120152 res . status ( 200 ) . send ( products ) ;
121153} )
122154
155+ router . put ( '/gallery-images/:id' , upload . array ( 'images' , 10 ) , async ( req , res ) => {
156+
157+ if ( ! mongoose . isValidObjectId ( req . params . id ) ) {
158+ res . status ( 400 ) . send ( 'Invalid Product ID' )
159+ }
160+
161+ const files = req . files ;
162+ let imagesPaths = [ ] ;
163+ const basePath = `${ req . protocol } ://${ req . get ( 'host' ) } /public/uploads/` ;
164+ if ( files ) {
165+ files . map ( file => {
166+ imagesPaths . push ( `${ basePath } ${ file . fileName } ` ) ;
167+ } )
168+ }
169+
170+ const product = await Product . findByIdAndUpdate ( req . params . id , {
171+
172+ image : imagesPaths ,
173+ } ,
174+ {
175+ new : true
176+ } )
177+
178+ if ( ! product )
179+ return res . status ( 500 ) . send ( 'Product cannot be updated' )
180+ res . send ( product ) ;
181+ } )
123182
124183module . exports = router ;
0 commit comments