
const express = require("express");
const path = require("path");
const fs = require("fs");

const app = express();

app.use((req, res, next) => {
    res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
    res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
    next();
});

app.use(express.static(__dirname));
app.use(express.json({limit: '5mb'}));

// Endpoint para salvar imagem do card
app.post('/save-card-image', (req, res) => {
    const { image } = req.body;
    if (!image || !image.startsWith('data:image/png;base64,')) {
        return res.status(400).json({ message: 'Imagem inválida!' });
    }
    const base64Data = image.replace(/^data:image\/png;base64,/, "");
    const imgDir = path.join(__dirname, 'img');
    if (!fs.existsSync(imgDir)) {
        fs.mkdirSync(imgDir);
    }
    const filename = `card-espelho-${Date.now()}.png`;
    const filePath = path.join(imgDir, filename);
    fs.writeFile(filePath, base64Data, 'base64', (err) => {
        if (err) {
            return res.status(500).json({ message: 'Erro ao salvar imagem!' });
        }
        res.json({ message: `Imagem salva como ${filename}` });
    });
});

app.listen(3000, () => {
    console.log("Rodando em http://localhost:3000/cardsini.html");
});
