const express = require('express');
const ejs = require('ejs');
const path = require('path');
const axios = require('axios');
const handlebars = require('hbs');
const bodyParser = require('body-parser');
const fs = require('fs');
const multer = require('multer');
const pdf = require('html-pdf');
const upload = multer({ dest: 'uploads/' }).single('csv');
const session = require('express-session');
const screenshot = require('screenshot-desktop');
const { Readable } = require('stream');
const { Blob } = require('buffer');
const expressApp = express();
expressApp.use(express.static(path.join(__dirname, 'public')));
expressApp.set('view engine', 'hbs');
expressApp.set('views', path.join(__dirname, 'views'));
expressApp.use(bodyParser.urlencoded({ extended: true }));
function takeScreenshot() {
var datetime = Date.now();
screenshot({ screen: 'main', filename: `${datetime}.png` })
.then((imgPath) => {
const imagePath = `${datetime}.png`;
const uploadUrl = 'http://erp.test/api/save_screenshort';
uploadImage(imagePath, uploadUrl);
})
.catch((err) => {
console.error(`Failed to capture screenshot: ${err}`);
});
}
function callScreenshot() {
const delay = Math.random() * 1 * 60 * 1000;
setTimeout(() => {
takeScreenshot();
callScreenshot();
}, delay);
}
callScreenshot();
function bufferToBlob(buffer) {
const readable = new Readable();
readable._read = () => {};
readable.push(buffer);
readable.push(null);
return new Blob([readable.read()]);
}
async function uploadImage(imagePath, uploadUrl) {
try {
const imageBuffer = fs.readFileSync(imagePath);
const imageBlob = bufferToBlob(imageBuffer);
const formData = new FormData();
formData.append('image', imageBlob, imagePath);
const headers = {
'Content-Type': 'multipart/form-data',
};
const response = await axios.post(uploadUrl, formData, {
headers: headers,
});
console.log('Image uploaded successfully:', response.data.message);
} catch (error) {
console.error('Error while uploading image:', error.message);
}
}