ability to upload images
This commit is contained in:
		@@ -1,6 +1,8 @@
 | 
			
		||||
import AbstractProductService from '../services/abstractproduct.js';
 | 
			
		||||
import statuses from '../utils/status.js';
 | 
			
		||||
import log from '../utils/log.js';
 | 
			
		||||
import fs from 'fs';
 | 
			
		||||
import path from 'path';
 | 
			
		||||
 | 
			
		||||
const TAG = "/controllers/abstractproduct.js";
 | 
			
		||||
 | 
			
		||||
@@ -8,6 +10,16 @@ class AbstractProductController {
 | 
			
		||||
    async create(req, res) {
 | 
			
		||||
        try {
 | 
			
		||||
            const { groupId, localId, barcode, name, net_weight, image_filename, category, unit } = req.body;
 | 
			
		||||
 | 
			
		||||
            const tempPath = req.file.path;
 | 
			
		||||
            const targetPath = path.join(path.resolve(path.dirname('')), `/uploads/${image_filename}.png`);
 | 
			
		||||
 | 
			
		||||
            if (path.extname(req.file.originalname).toLowerCase() !== ".png") {
 | 
			
		||||
                return res.status(400).send("Only .png files are allowed")
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            fs.renameSync(tempPath, targetPath);
 | 
			
		||||
 | 
			
		||||
            await AbstractProductService.create(groupId, localId, barcode, name, net_weight, image_filename, category, unit);
 | 
			
		||||
            return res.status(200).send("Successfull");
 | 
			
		||||
        } catch (e) {
 | 
			
		||||
@@ -25,13 +37,24 @@ class AbstractProductController {
 | 
			
		||||
        try {
 | 
			
		||||
            let { groupId, localId, barcode, name, net_weight, image_filename, category, unit } = req.body;
 | 
			
		||||
 | 
			
		||||
            const tempPath = req.file.path;
 | 
			
		||||
            const targetPath = path.join(path.resolve(path.dirname('')) + `/uploads/${image_filename}.png`);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            if (barcode) await AbstractProductService.updateBarcode(groupId, localId, barcode);
 | 
			
		||||
 | 
			
		||||
            if (name) await AbstractProductService.updateName(groupId, localId, name);
 | 
			
		||||
 | 
			
		||||
            if (net_weight) await AbstractProductService.updateNetWeight(groupId, localId, net_weight);
 | 
			
		||||
 | 
			
		||||
            if (image_filename) await AbstractProductService.updateImageFilename(groupId, localId, image_filename);
 | 
			
		||||
            if (image_filename && tempPath) {
 | 
			
		||||
                fs.renameSync(tempPath, targetPath);
 | 
			
		||||
                await AbstractProductService.updateImageFilename(groupId, localId, image_filename);
 | 
			
		||||
            } else if (image_filename && !tempPath) {
 | 
			
		||||
                return res.status(400).send("You must supply image file along with its hash");
 | 
			
		||||
            } else if (!image_file && tempPath) {
 | 
			
		||||
                return res.status(400).send("You must supply image file hash along with file");
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (category) await AbstractProductService.updateCategory(groupId, localId, category);
 | 
			
		||||
 | 
			
		||||
@@ -49,6 +72,8 @@ class AbstractProductController {
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // async updateImage(req, res) {}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export default new AbstractProductController();
 | 
			
		||||
		Reference in New Issue
	
	Block a user