Skip to content

Commit

Permalink
Added feature to post the blog from draft also filter the blog accord…
Browse files Browse the repository at this point in the history
…ing to category
  • Loading branch information
Sawan-Kushwah committed Nov 2, 2024
1 parent f166626 commit 8110a63
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 198 deletions.
29 changes: 24 additions & 5 deletions backend/controllers/addBlogController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import BlogPost from '../models/addBlog.js';
import multer from 'multer';
import path from 'path';
import fs from 'fs';

// Configure multer for file uploads
const storage = multer.diskStorage({
Expand All @@ -19,11 +20,28 @@ const upload = multer({ storage: storage });
// Function to save a new blog post
export async function saveBlog(req, res) {
try {
const { title, category, summary, excerpt, tags, publish } = req.body;
const featuredImage = req.file ? req.file.path : null; // Assuming `req.file` is set if an image is uploaded
const { title, category, summary, excerpt, tags, publish, featuredImage } = req.body;

// If an image is uploaded, use its path
let imagePath = req.file ? req.file.path : null;

// Handle base64 image if provided
if (featuredImage && featuredImage.startsWith('data:image/')) {
// Extract the base64 part of the image data
const base64Image = featuredImage.split(';base64,').pop();
// Convert base64 string to Buffer
const buffer = Buffer.from(base64Image, 'base64');

// Create a path to save the image
const imagePathFromBase64 = `uploads/${Date.now()}.png`; // Use a unique name based on timestamp

// Save the image buffer to a file
await fs.promises.writeFile(imagePathFromBase64, buffer);
imagePath = imagePathFromBase64; // Set imagePath to the saved file path
}

// Log request body and file info

console.log(req.body);
console.log(req.file);

// Check if required fields are provided
if (!title || !category || !summary || !excerpt) {
Expand All @@ -38,7 +56,7 @@ export async function saveBlog(req, res) {
excerpt,
tags,
publish,
featuredImage
featuredImage: imagePath // Use the determined image path
});

console.log(newBlog);
Expand All @@ -57,6 +75,7 @@ export async function saveBlog(req, res) {
}
}


// Function to retrieve all blog posts
export async function getAllBlog(req, res) {
try {
Expand Down
Loading

0 comments on commit 8110a63

Please sign in to comment.