Server IP : 162.213.251.212 / Your IP : 18.119.108.166 [ Web Server : LiteSpeed System : Linux business55.web-hosting.com 4.18.0-553.lve.el8.x86_64 #1 SMP Mon May 27 15:27:34 UTC 2024 x86_64 User : allssztx ( 535) PHP Version : 8.1.31 Disable Function : NONE Domains : 1 Domains MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /proc/thread-self/root/proc/thread-self/root/home/allssztx/needapair.com/routes/cms/ |
Upload File : |
const express = require("express") const router = express.Router() const Content = require("../../schema/Content") const multer = require('multer') const path = require('path') const fs = require('fs') // const upload = multer({ dest: 'uploads/' }) const storage = multer.diskStorage({ destination: function (req, file, cb) { const dir = './public/uploads/'; const fullPath = path.resolve(dir); console.log("fullPath", fullPath) if (!fs.existsSync(dir)) { fs.mkdirSync(dir); } cb(null, fullPath); }, filename: function (req, file, cb) { cb(null, file.fieldname + "" + Date.now() + Math.ceil(Math.random() * 999) + '.' + file.originalname.split('.')[1]); } }); const imageFilter = (req, file, cb) => { console.log("file", file) // only image files if (file.mimetype === "image/jpeg" || file.mimetype === "image/png") { cb(null, true); } else { cb(null, false); } }; const uploadImage = multer({ storage: storage, fileFilter: imageFilter }); router.post('/', uploadImage.single('image'), async (req, res) => { try { const imageFile = req.file; const { id, title, heading, subheading, tagline, paragraph, listItem, linktext, linkhref } = req.body let link; if (linktext) { link = { text: linktext, link: linkhref } } // const content = await Content.create({ title, heading, subheading, tagline, paragraph, listItem, link, imageUrl }) const updateObject = { title, heading, subheading, tagline, paragraph, listItem, link }; // Add imageUrl to the update object only if it's not null if (imageFile !== null && imageFile !== undefined) { updateObject.imageUrl = `uploads/${imageFile?.filename}`; } Content.findByIdAndUpdate(id, updateObject, { new: true }, (err, updatedDocument) => { if (err) { console.error('Error:', err); } else { console.log('Updated Document:', updatedDocument); } }); res.status(200).redirect('/admin/dashboard?message=Content Updated...') // console.log("content", C) } catch (error) { console.log(error.message) res.status(500).json({error: error.message}) } }) module.exports = router