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();
|
||||
@@ -1,6 +1,6 @@
|
||||
import pg from 'pg';
|
||||
import log from './utils/log.js'
|
||||
import fs from 'fs'
|
||||
import fs from 'fs';
|
||||
import config from '../config.json' with {type: "json"};
|
||||
|
||||
const { Pool } = pg;
|
||||
|
||||
@@ -10,8 +10,8 @@ import CategoryRouter from './routers/category.js';
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: false, limit: "200mb", parameterLimit: 100000 }));
|
||||
app.use(express.json({ limit: "200mb", parameterLimit: 1000000 }));
|
||||
|
||||
app.use('/api/user/', UserRouter);
|
||||
app.use('/api/group/', GroupRouter);
|
||||
|
||||
@@ -2,10 +2,16 @@ import { Router } from 'express';
|
||||
import auth from '../middlewares/auth.js';
|
||||
import AbstractProductController from '../controllers/abstractproduct.js';
|
||||
import existance from '../middlewares/existance.js';
|
||||
import multer from 'multer';
|
||||
import path from 'path';
|
||||
|
||||
const upload = multer(({
|
||||
dest: path.join(path.resolve(path.dirname('')), "/temp")
|
||||
}));
|
||||
|
||||
const AbstractProductRouter = new Router();
|
||||
|
||||
AbstractProductRouter.post('/create', auth.authenticate, existance.groupExists, auth.userIsInGroup, AbstractProductController.create);
|
||||
AbstractProductRouter.post('/update', auth.authenticate, existance.groupExists, auth.userIsInGroup, existance.abstractProductExists, AbstractProductController.update);
|
||||
AbstractProductRouter.post('/create', upload.single("file"), auth.authenticate, existance.groupExists, auth.userIsInGroup, AbstractProductController.create);
|
||||
AbstractProductRouter.post('/update', upload.single("file"), auth.authenticate, existance.groupExists, auth.userIsInGroup, existance.abstractProductExists, AbstractProductController.update);
|
||||
|
||||
export default AbstractProductRouter;
|
||||
@@ -4,6 +4,6 @@ const statuses = {
|
||||
not_found: "not found",
|
||||
invalid_syntax: "invalid syntax",
|
||||
unknown: "unknown"
|
||||
}
|
||||
};
|
||||
|
||||
export default statuses
|
||||
export default statuses;
|
||||
Reference in New Issue
Block a user