Merge pull request 'tmp' (#11) from tmp into master

Reviewed-on: #11
suvo-patch-1
Subhodip Ghosh 2025-01-31 07:51:51 +00:00
commit b77e3692d2
6 changed files with 2605 additions and 8 deletions

BIN
output.png Normal file

Binary file not shown.

2553
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -60,8 +60,10 @@
"express-mongo-sanitize": "^2.0.0",
"express-rate-limit": "^5.0.0",
"form-data": "^4.0.1",
"formdata-node": "^6.0.3",
"helmet": "^4.1.0",
"http-status": "^1.4.0",
"install": "^0.13.0",
"joi": "^17.3.0",
"jsonwebtoken": "^8.5.1",
"mariadb": "^3.4.0",
@ -69,7 +71,9 @@
"mongoose": "^8.7.1",
"morgan": "^1.9.1",
"mysql2": "^3.11.0",
"node-fetch": "^3.3.2",
"nodemailer": "^6.3.1",
"npm": "^11.0.0",
"passport": "^0.4.0",
"passport-jwt": "^4.0.0",
"pm2": "^5.1.0",

View File

@ -5,9 +5,9 @@ const gameSchema = new mongoose.Schema({
userId: { type: String, required: true },
gameID: { type: String, required: true },
gameTime: { type: String, required: false },
score: { type: String},
gameStar: { type: Number},
screenshotUrl: { type: String }, // Store the S3 URL of the screenshot
});
score: { type: String },
gameStar: { type: Number },
screenshotUrl: { type: String },
}, { collection: "gameData" });
module.exports = mongoose.model("Game", gameSchema);

View File

@ -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 };

View File

@ -1,4 +1,5 @@
const AWS = require("aws-sdk");
const mongoose = require("mongoose");
const Game = require("../../models/gameModel");
const s3 = new AWS.S3({
@ -7,12 +8,27 @@ const s3 = new AWS.S3({
region: process.env.AWS_REGION,
});
// MongoDB Connection
const mongoURI = process.env.MONGODB_URL;
const dbName = process.env.MONGO_DB_NAME;
mongoose.connect(mongoURI, {
dbName: dbName,
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() =>
console.log("Connected to MongoDB"))
.catch((err) => console.error("MongoDB connection error:", err));
const saveGameScore2 = async (req, res) => {
const { userId, gameName, gameID, gameTime, score, gameStar, screenShot } = req.body;
try {
if (!gameName || !userId || !gameID) {
return res.status(400).json({ error: "Missing required fields" });
}
const newGame = new Game({
gameName,
userId,
@ -21,21 +37,26 @@ const saveGameScore2 = async (req, res) => {
score,
gameStar,
});
// Save to MongoDB
const result = await newGame.save();
// Upload screenshot to S3 if provided
if (screenShot) {
let base64Image = screenShot.split(";base64,").pop();
const buffer = Buffer.from(base64Image, 'base64');
// const buffer = Buffer.from(screenShot, "base64");
let base64Image = screenShot.split(";base64,").pop();
const buffer = Buffer.from(base64Image, "base64");
const uploadParams = {
Bucket: process.env.S3_BUCKET_NAME,
Key: `images/${result._id}.png`,
Body: buffer,
ContentType: "image/png",
};
const s3Response = await s3.upload(uploadParams).promise();
newGame.screenshotUrl = s3Response.Location;
await newGame.save();
}
res.status(200).json({
message: "Game data saved successfully",
data: newGame,