takeSnap
parent
319bc33d4a
commit
1576fa92c1
BIN
screenshot.png
BIN
screenshot.png
Binary file not shown.
|
@ -1,58 +1,21 @@
|
|||
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 takeSnap = async (req, res) => {
|
||||
const { url } = req.body;
|
||||
console.log('req.body.url', req.body.url);
|
||||
const puppeteer = require('puppeteer');
|
||||
|
||||
if (!url) {
|
||||
return res.status(400).json({ error: 'URL is required' });
|
||||
}
|
||||
|
||||
const FILE_PATH = `screenshot.png`;
|
||||
|
||||
try {
|
||||
const browser = await puppeteer.launch({ headless: 'new' });
|
||||
(async () => {
|
||||
const url = req.body.url;
|
||||
const browser = await puppeteer.launch();
|
||||
const page = await browser.newPage();
|
||||
await page.goto(url, { waitUntil: 'load' });
|
||||
await page.goto(url, { waitUntil: 'networkidle2' });
|
||||
|
||||
await page.screenshot({ path: 'screenshot.png', fullPage: true });
|
||||
|
||||
await page.screenshot({ path: FILE_PATH });
|
||||
await browser.close();
|
||||
console.log('Screenshot saved as screenshot.png');
|
||||
})();
|
||||
|
||||
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: formData.getHeaders() // No Authorization header
|
||||
});
|
||||
|
||||
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: { 'Content-Type': 'application/json' } // No Authorization header
|
||||
});
|
||||
|
||||
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;
|
||||
|
||||
|
|
Loading…
Reference in New Issue