Website : rimsha.abasa.com
backdoor
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
var
/
www
/
ah7-backend
/
src
/
utils
/
Filename :
StoreFoodData.js
back
Copy
// src/utils/storeFoodData.js import Food from "../models/food.model.js"; import mongoose from "mongoose"; import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3"; import { v4 as uuidv4 } from "uuid"; import path from "path"; import fs from "fs"; import { fileURLToPath } from "url"; import dotenv from "dotenv"; dotenv.config(); // Get directory path const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // Initialize S3 client const s3Client = new S3Client({ region: process.env.AWS_REGION, credentials: { accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY } }); /** * Helper function to get MIME type from file extension * @param {string} ext - File extension * @returns {string} MIME type */ function getMimeType(ext) { switch (ext.toLowerCase()) { case '.jpg': case '.jpeg': return 'image/jpeg'; case '.png': return 'image/png'; case '.gif': return 'image/gif'; case '.webp': return 'image/webp'; default: return 'application/octet-stream'; } } /** * Uploads local image to S3 bucket and returns the URL * @param {string} imagePath - Path to the image file * @returns {Promise<string|null>} S3 URL or null if upload fails */ async function uploadLocalImageToS3(imagePath) { if (!imagePath || imagePath.trim() === '') { return null; } // If it's already a URL, return it directly if (imagePath.startsWith('http')) { return imagePath; } // Remove 'public/' prefix if it exists const cleanPath = imagePath.startsWith('public/') ? imagePath.substring(7) : imagePath; // Construct full path to the image const fullImagePath = path.join(__dirname, '../../public', cleanPath); // Check if file exists if (!fs.existsSync(fullImagePath)) { console.warn(`Image file not found at path: ${fullImagePath}`); return null; } try { // Read file data const fileBuffer = fs.readFileSync(fullImagePath); const fileName = path.basename(cleanPath); const fileExt = path.extname(fileName); const mimeType = getMimeType(fileExt); // Generate S3 key (replace spaces with +) const sanitizedFileName = fileName.replace(/\s+/g, '+'); const fileKey = `food-images/${uuidv4()}-${sanitizedFileName}`; // Upload to S3 const uploadParams = { Bucket: process.env.AWS_BUCKET_NAME, Key: fileKey, Body: fileBuffer, ContentType: mimeType, }; await s3Client.send(new PutObjectCommand(uploadParams)); return `https://${process.env.AWS_BUCKET_NAME}.s3.${process.env.AWS_REGION}.amazonaws.com/${fileKey}`; } catch (error) { console.error(`Error uploading image to S3:`, error); return null; } } /** * Processes and stores food data in MongoDB * @param {Object} jsonData - JSON data containing food items * @returns {Promise<void>} */ const storeFoodJsonToMongoDB = async (jsonData) => { try { if (!Array.isArray(jsonData.food_logs)) { throw new Error("Invalid JSON data: 'food_logs' key is missing or not an array"); } // Process in batches to avoid overwhelming the database const batchSize = 20; for (let i = 0; i < jsonData.food_logs.length; i += batchSize) { const batch = jsonData.food_logs.slice(i, i + batchSize); await Promise.all(batch.map(async (foodItem) => { try { // Validate required fields if (!foodItem.food_name || foodItem.calories === undefined) { console.warn(`Skipping item with missing required fields: ${JSON.stringify(foodItem)}`); return; } // Check if food item already exists const existingFood = await Food.findOne({ food_name: foodItem.food_name }); if (existingFood) { console.log(`Food "${foodItem.food_name}" already exists. Skipping.`); return; } // Process image const imageUrl = foodItem.image ? await uploadLocalImageToS3(foodItem.image) : null; // Create new food document const newFood = new Food({ food_name: foodItem.food_name, image: imageUrl, calories: foodItem.calories !== undefined ? foodItem.calories : null, total_fat: foodItem.total_fat !== undefined ? foodItem.total_fat : null, protein: foodItem.protein !== undefined ? foodItem.protein : null, carbohydrates: foodItem.carbohydrates !== undefined ? foodItem.carbohydrates : null, sugars: foodItem.sugars !== undefined ? foodItem.sugars : null, fiber: foodItem.fiber !== undefined ? foodItem.fiber : null, sodium: foodItem.sodium !== undefined ? foodItem.sodium : null, cholesterol: foodItem.cholesterol !== undefined ? foodItem.cholesterol : null, }); await newFood.save(); console.log(`Successfully saved: ${foodItem.food_name}`); } catch (error) { console.error(`Error processing food item "${foodItem.food_name}":`, error.message); } })); // Small delay between batches to avoid rate limiting if (i + batchSize < jsonData.food_logs.length) { await new Promise(resolve => setTimeout(resolve, 500)); } } console.log("All food data has been processed and stored in MongoDB!"); } catch (error) { console.error("Error while storing food data:", error.message); throw error; } }; export default storeFoodJsonToMongoDB;