gallery 2 api
parent
e520169fb9
commit
5152ca2e23
Binary file not shown.
File diff suppressed because it is too large
Load Diff
|
@ -60,15 +60,19 @@
|
||||||
"express-mongo-sanitize": "^2.0.0",
|
"express-mongo-sanitize": "^2.0.0",
|
||||||
"express-rate-limit": "^5.0.0",
|
"express-rate-limit": "^5.0.0",
|
||||||
"form-data": "^4.0.1",
|
"form-data": "^4.0.1",
|
||||||
|
"formdata-node": "^6.0.3",
|
||||||
"helmet": "^4.1.0",
|
"helmet": "^4.1.0",
|
||||||
"http-status": "^1.4.0",
|
"http-status": "^1.4.0",
|
||||||
|
"install": "^0.13.0",
|
||||||
"joi": "^17.3.0",
|
"joi": "^17.3.0",
|
||||||
"jsonwebtoken": "^8.5.1",
|
"jsonwebtoken": "^8.5.1",
|
||||||
"moment": "^2.24.0",
|
"moment": "^2.24.0",
|
||||||
"mongoose": "^8.7.1",
|
"mongoose": "^8.7.1",
|
||||||
"morgan": "^1.9.1",
|
"morgan": "^1.9.1",
|
||||||
"mysql2": "^3.11.0",
|
"mysql2": "^3.11.0",
|
||||||
|
"node-fetch": "^3.3.2",
|
||||||
"nodemailer": "^6.3.1",
|
"nodemailer": "^6.3.1",
|
||||||
|
"npm": "^11.0.0",
|
||||||
"passport": "^0.4.0",
|
"passport": "^0.4.0",
|
||||||
"passport-jwt": "^4.0.0",
|
"passport-jwt": "^4.0.0",
|
||||||
"pm2": "^5.1.0",
|
"pm2": "^5.1.0",
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
const mongoose = require('mongoose');
|
||||||
|
|
||||||
|
// Define the GalleryImage schema
|
||||||
|
const galleryImageSchema = new mongoose.Schema({
|
||||||
|
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }, // Assuming you have a User model
|
||||||
|
gameName: { type: String, required: true },
|
||||||
|
gameID: { type: String, required: true },
|
||||||
|
screenshotUrl: { type: String, required: true },
|
||||||
|
message: { type: String, default: 'Image received' },
|
||||||
|
}, { timestamps: true });
|
||||||
|
|
||||||
|
// Check if the model already exists in mongoose.models
|
||||||
|
const GalleryImage = mongoose.models.GalleryImage || mongoose.model('GalleryImage', galleryImageSchema);
|
||||||
|
|
||||||
|
module.exports = GalleryImage;
|
|
@ -0,0 +1,10 @@
|
||||||
|
const mongoose = require("mongoose");
|
||||||
|
|
||||||
|
const galleryImageScema = new mongoose.Schema({
|
||||||
|
gameName: { type: String, required: true },
|
||||||
|
userId: { type: String, required: true },
|
||||||
|
gameID: { type: String, required: true },
|
||||||
|
screenshotUrl: { type: String },
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = mongoose.model("GalleryImage", galleryImageScema);
|
|
@ -0,0 +1,21 @@
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
function base64ToImageFile(base64, fileName) {
|
||||||
|
const matches = base64.match(/^data:(.+);base64,(.+)$/);
|
||||||
|
if (!matches) {
|
||||||
|
throw new Error("Invalid Base64 string");
|
||||||
|
}
|
||||||
|
|
||||||
|
const mimeType = matches[1]; // e.g., image/png
|
||||||
|
const base64Data = matches[2]; // Actual base64 string
|
||||||
|
|
||||||
|
const buffer = Buffer.from(base64Data, 'base64');
|
||||||
|
const filePath = path.join(__dirname, fileName);
|
||||||
|
|
||||||
|
fs.writeFileSync(filePath, buffer);
|
||||||
|
|
||||||
|
return filePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { base64ToImageFile };
|
|
@ -0,0 +1,36 @@
|
||||||
|
const GalleryImage = require('../../models/getGalleyImage');
|
||||||
|
|
||||||
|
const getGalleryImage = async (req, res) => {
|
||||||
|
try {
|
||||||
|
// Extract query parameters for filtering (if provided)
|
||||||
|
const { gameName, userId, gameID} = req.query;
|
||||||
|
|
||||||
|
// Build a filter object based on the query parameters
|
||||||
|
const filter = {};
|
||||||
|
|
||||||
|
if (gameName) {
|
||||||
|
filter.gameName = gameName;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (userId) {
|
||||||
|
filter.userId = userId;
|
||||||
|
}
|
||||||
|
if (gameID) {
|
||||||
|
filter.gameID = gameID;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch gallery images with filters and selected fields
|
||||||
|
const galleryImages = await GalleryImage.find(filter).select('gameName userId gameID screenshotUrl'); // Select only these fields
|
||||||
|
|
||||||
|
if (!galleryImages.length) {
|
||||||
|
return res.status(404).json({ message: 'No images found' });
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).json(galleryImages); // Return the filtered gallery images with selected fields
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching images:', error.message);
|
||||||
|
return res.status(500).json({ error: 'Something went wrong' });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = getGalleryImage;
|
|
@ -0,0 +1,78 @@
|
||||||
|
const GalleryImage = require('../../models/imageGallery');
|
||||||
|
const axios = require('axios');
|
||||||
|
const FormData = require('form-data'); // Import form-data for Node.js
|
||||||
|
|
||||||
|
const saveGalleryImage = async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { userId, gameName, gameID, screenShot } = req.body;
|
||||||
|
|
||||||
|
// Validate base64 format
|
||||||
|
const isValidBase64 = validateBase64(screenShot);
|
||||||
|
if (!isValidBase64) {
|
||||||
|
return res.status(400).json({ error: 'Invalid base64 image data' });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract image details
|
||||||
|
const imageData = extractImageDataFromBase64(screenShot);
|
||||||
|
const contentType = imageData.contentType || 'image/jpeg'; // Default to JPEG
|
||||||
|
|
||||||
|
// Convert base64 to buffer
|
||||||
|
const buffer = Buffer.from(screenShot.replace(/^data:image\/(png|jpg|jpeg);base64,/, ''), 'base64');
|
||||||
|
|
||||||
|
// Create FormData
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('file1', buffer, { filename: 'screenshot.jpg', contentType });
|
||||||
|
formData.append('folder', 'gameGallery');
|
||||||
|
formData.append('bucket', 'polly-bs');
|
||||||
|
|
||||||
|
// Upload image to external API using axios
|
||||||
|
const uploadResponse = await axios.post(
|
||||||
|
'https://preschool-curriculum.in/api/one/v1/file/upload',
|
||||||
|
formData,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
...formData.getHeaders(), // Add form-data headers like Content-Type boundary
|
||||||
|
// Add any necessary authentication headers if required
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (uploadResponse.status !== 200) {
|
||||||
|
throw new Error('Image upload failed: ' + uploadResponse.statusText);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract and store the uploaded image URL from the response
|
||||||
|
const screenshotUrl = uploadResponse.data.urls[0];
|
||||||
|
|
||||||
|
// Create and save gallery image data
|
||||||
|
const galleryImageData = new GalleryImage({
|
||||||
|
userId,
|
||||||
|
gameName,
|
||||||
|
gameID,
|
||||||
|
screenshotUrl,
|
||||||
|
message: 'Image received',
|
||||||
|
});
|
||||||
|
await galleryImageData.save();
|
||||||
|
|
||||||
|
return res.status(200).json(galleryImageData);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error:', error.message);
|
||||||
|
return res.status(500).json({ error: 'Something went wrong' });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Validate base64 image
|
||||||
|
function validateBase64(base64String) {
|
||||||
|
const regex = /^data:image\/(png|jpg|jpeg);base64,/;
|
||||||
|
return regex.test(base64String);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract image data (like content type) from base64 string
|
||||||
|
function extractImageDataFromBase64(base64String) {
|
||||||
|
const matches = base64String.match(/^data:image\/(png|jpg|jpeg);base64,/);
|
||||||
|
return {
|
||||||
|
contentType: matches ? `image/${matches[1]}` : 'image/jpeg',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = saveGalleryImage;
|
|
@ -31,6 +31,8 @@ const aiMarkDrawing = require("../api/aiMarkDrawing");
|
||||||
const aiFeedbackOnReportWithFollowup = require("../api/aiFeedbackOnReportWithFollowup");
|
const aiFeedbackOnReportWithFollowup = require("../api/aiFeedbackOnReportWithFollowup");
|
||||||
const aiTextToSpeech = require("../api/aiTextToSpeech");
|
const aiTextToSpeech = require("../api/aiTextToSpeech");
|
||||||
const aiEvaluateImageToStar = require("../api/aiEvaluateImageToStar");
|
const aiEvaluateImageToStar = require("../api/aiEvaluateImageToStar");
|
||||||
|
const saveGalleryImage = require("../api/saveGalleryImage");
|
||||||
|
const getGalleryImage = require("../api/getGalleryImage");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -190,8 +192,18 @@ router.get("/ping", (req, res) => {
|
||||||
router.post("/aiEvaluateImageToStar", (req, res) => {
|
router.post("/aiEvaluateImageToStar", (req, res) => {
|
||||||
aiEvaluateImageToStar(req, res);
|
aiEvaluateImageToStar(req, res);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Save Drawing Game Gallery Image
|
||||||
|
router.post("/saveGalleryImage", (req, res) => {
|
||||||
|
saveGalleryImage(req, res);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get Drawing Game Gallery Image
|
||||||
|
router.get("/getGalleryImage", (req, res) => {
|
||||||
|
getGalleryImage(req, res);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
Loading…
Reference in New Issue