89 lines
2.3 KiB
JavaScript
89 lines
2.3 KiB
JavaScript
const express = require("express");
|
|
const multer = require("multer");
|
|
const pool = require("../config/db");
|
|
const path = require("path");
|
|
const fs = require("fs-extra");
|
|
|
|
const router = express.Router();
|
|
|
|
// Ensure the uploads directory exists
|
|
const uploadDir = path.join(__dirname, "../uploads");
|
|
fs.ensureDirSync(uploadDir);
|
|
|
|
// Configure Multer for file uploads
|
|
const storage = multer.diskStorage({
|
|
destination: (req, file, cb) => {
|
|
cb(null, uploadDir);
|
|
},
|
|
filename: (req, file, cb) => {
|
|
cb(null, Date.now() + "_" + file.originalname); // Unique filename
|
|
},
|
|
});
|
|
|
|
const upload = multer({
|
|
storage: storage,
|
|
limits: { fileSize: 2 * 1024 * 1024 * 1024 },
|
|
}); // 2GB limit
|
|
|
|
// Upload file API
|
|
router.post("/upload", upload.single("file"), async (req, res) => {
|
|
try {
|
|
const { client_id, file_name } = req.body;
|
|
if (!req.file) {
|
|
return res.status(400).json({ error: "No file uploaded" });
|
|
}
|
|
|
|
const filePath = `uploads/${req.file.filename}`;
|
|
|
|
await pool.query(
|
|
"INSERT INTO tbl_files (transid,client_id,file_name, file_path) VALUES (UUID(),?,?, ?)",
|
|
[client_id, file_name, filePath]
|
|
);
|
|
|
|
res.json({ message: "File uploaded successfully!", file_path: filePath });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
router.post("/update-partner-logo", upload.single("file"), async (req, res) => {
|
|
try {
|
|
const { client_id, file_name } = req.body;
|
|
if (!req.file) {
|
|
return res.status(400).json({ error: "No file uploaded" });
|
|
}
|
|
|
|
const filePath = `uploads/${req.file.filename}`;
|
|
|
|
await pool.query("update tbl_partners set logo_url = ? where transid = ?", [
|
|
filePath,
|
|
client_id,
|
|
]);
|
|
|
|
res.json({ message: "File uploaded successfully!", file_path: filePath });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
router.delete("/del/:fileid", async (req, res) => {
|
|
try {
|
|
const { fileid } = req.params;
|
|
const [files] = await pool.query(
|
|
"delete from tbl_files where transid =? ",
|
|
[fileid]
|
|
);
|
|
|
|
const [filesfromads] = await pool.query(
|
|
"delete from tbl_client_partner_mapping where fileid =? ",
|
|
[fileid]
|
|
);
|
|
|
|
res.json(files);
|
|
} catch (err) {
|
|
res.status(500), json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|