fix mongo
This commit is contained in:
@@ -2,10 +2,9 @@ const express = require('express');
|
||||
const router = express.Router();
|
||||
const { verifyToken } = require('../middleware/auth');
|
||||
const BuyProduct = require('../models/BuyProduct');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const multer = require('multer');
|
||||
const UPLOADS_ROOT = path.join(__dirname, '..', '..', 'remote-assets', 'uploads', 'buy-products');
|
||||
const UPLOADS_ROOT = 'server/remote-assets/uploads/buy-products';
|
||||
const ensureDirectory = (dirPath) => {
|
||||
if (!fs.existsSync(dirPath)) {
|
||||
fs.mkdirSync(dirPath, { recursive: true });
|
||||
@@ -24,17 +23,28 @@ const ALLOWED_MIME_TYPES = new Set([
|
||||
'text/csv',
|
||||
]);
|
||||
|
||||
const getExtension = (filename) => {
|
||||
const lastDot = filename.lastIndexOf('.');
|
||||
return lastDot > 0 ? filename.slice(lastDot) : '';
|
||||
};
|
||||
|
||||
const getBasename = (filename) => {
|
||||
const lastDot = filename.lastIndexOf('.');
|
||||
const name = lastDot > 0 ? filename.slice(0, lastDot) : filename;
|
||||
const lastSlash = Math.max(name.lastIndexOf('/'), name.lastIndexOf('\\'));
|
||||
return lastSlash >= 0 ? name.slice(lastSlash + 1) : name;
|
||||
};
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination: (req, file, cb) => {
|
||||
const productId = req.params.id || 'common';
|
||||
const productDir = path.join(UPLOADS_ROOT, productId);
|
||||
const productDir = `${UPLOADS_ROOT}/${productId}`;
|
||||
ensureDirectory(productDir);
|
||||
cb(null, productDir);
|
||||
},
|
||||
filename: (req, file, cb) => {
|
||||
const originalExtension = path.extname(file.originalname) || '';
|
||||
const baseName = path
|
||||
.basename(file.originalname, originalExtension)
|
||||
const originalExtension = getExtension(file.originalname);
|
||||
const baseName = getBasename(file.originalname)
|
||||
.replace(/[^a-zA-Z0-9-_]+/g, '_')
|
||||
.toLowerCase();
|
||||
cb(null, `${Date.now()}_${baseName}${originalExtension}`);
|
||||
@@ -243,7 +253,7 @@ router.post('/:id/files', verifyToken, handleSingleFileUpload, async (req, res)
|
||||
return res.status(400).json({ error: 'File is required' });
|
||||
}
|
||||
|
||||
const relativePath = path.join('buy-products', id, req.file.filename).replace(/\\/g, '/');
|
||||
const relativePath = `buy-products/${id}/${req.file.filename}`;
|
||||
const file = {
|
||||
id: `file-${Date.now()}`,
|
||||
name: req.file.originalname,
|
||||
@@ -293,7 +303,7 @@ router.delete('/:id/files/:fileId', verifyToken, async (req, res) => {
|
||||
await product.save();
|
||||
|
||||
const storedPath = fileToRemove.storagePath || fileToRemove.url.replace(/^\/uploads\//, '');
|
||||
const absolutePath = path.join(__dirname, '..', '..', 'remote-assets', 'uploads', storedPath);
|
||||
const absolutePath = `server/remote-assets/uploads/${storedPath}`;
|
||||
|
||||
fs.promises.unlink(absolutePath).catch((unlinkError) => {
|
||||
if (unlinkError && unlinkError.code !== 'ENOENT') {
|
||||
|
||||
Reference in New Issue
Block a user