takeSnap
Suvodip 2025-03-11 16:52:39 +05:30
parent 0d4323df9e
commit b7da294a17
5 changed files with 631 additions and 60 deletions

612
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -48,18 +48,19 @@
],
"dependencies": {
"aws-sdk": "^2.1669.0",
"axios": "^1.7.7",
"axios": "^1.8.2",
"bcryptjs": "^2.4.3",
"body-parser": "^1.20.2",
"compression": "^1.7.4",
"cors": "^2.8.5",
"cross-env": "^7.0.0",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"express": "^4.21.2",
"express-mongo-sanitize": "^2.0.0",
"express-rate-limit": "^5.0.0",
"form-data": "^4.0.1",
"form-data": "^4.0.2",
"formdata-node": "^6.0.3",
"fs": "^0.0.1-security",
"helmet": "^4.1.0",
"http-status": "^1.4.0",
"install": "^0.13.0",
@ -75,7 +76,9 @@
"npm": "^11.0.0",
"passport": "^0.4.0",
"passport-jwt": "^4.0.0",
"path": "^0.12.7",
"pm2": "^5.1.0",
"puppeteer": "^24.4.0",
"swagger-jsdoc": "^6.0.8",
"swagger-ui-express": "^4.1.6",
"validator": "^13.0.0",

BIN
screenshot.png Normal file

Binary file not shown.

View File

@ -0,0 +1,65 @@
const puppeteer = require('puppeteer');
const fs = require('fs');
const FormData = require('form-data');
const axios = require('axios');
const UPLOAD_URL = 'https://game-du.teachertrainingkolkata.in/files';
const UPDATE_URL = 'https://game-du.teachertrainingkolkata.in/items/game_cross_varient3/1';
const DIRECTUS_TOKEN = 'your_directus_token'; // Replace with actual token
const takeSnap = async (req, res) => {
const { url } = req.body;
if (!url) {
return res.status(400).json({ error: 'URL is required' });
}
const FILE_PATH = `screenshot.png`;
try {
const browser = await puppeteer.launch({ headless: 'new' });
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'load' });
await page.screenshot({ path: FILE_PATH });
await browser.close();
console.log('Screenshot captured!');
// Upload Screenshot
const fileBuffer = fs.readFileSync(FILE_PATH);
const formData = new FormData();
formData.append('file', fileBuffer, FILE_PATH);
const uploadResponse = await axios.post(UPLOAD_URL, formData, {
headers: {
'Authorization': `Bearer ${DIRECTUS_TOKEN}`,
...formData.getHeaders()
}
});
if (!uploadResponse.data.data || !uploadResponse.data.data.id) {
throw new Error('File upload failed');
}
const fileId = uploadResponse.data.data.id;
console.log('File uploaded with ID:', fileId);
// Update Directus Item
await axios.patch(UPDATE_URL, { thumbnail: fileId }, {
headers: {
'Authorization': `Bearer ${DIRECTUS_TOKEN}`,
'Content-Type': 'application/json'
}
});
console.log('Directus item updated successfully');
res.json({ success: true, fileId });
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: error.message });
}
};
module.exports = takeSnap;

View File

@ -35,6 +35,7 @@ const getGameInfo = require("../api/getGameInfo");
const saveGalleryImage = require("../api/saveGalleryImage");
const getGalleryImage = require("../api/getGalleryImage");
const getGameData = require("../api/getGameData");
const takeSnap = require("../api/takeSnap");
// const aiTest = require("../api/aiTest");
@ -214,6 +215,10 @@ router.get("/ping", (req, res) => {
getGameData(req, res);
});
router.post("/takeSnap", (req, res) => {
takeSnap(req, res);
});
// Get Drawing Game Gallery Image
// router.post("/aiTest", (req, res) => {
// aiTest(req, res);