import express from 'express'; import multer from 'multer'; import path from 'path'; import { fileURLToPath } from 'url'; import fs from 'fs'; import axios from 'axios'; import sharp from 'sharp'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const app = express(); const upload = multer({ storage: multer.memoryStorage() }); const PORT = process.env.PORT || 7860; const GOOGLE_SCRIPT_URL = "https://script.google.com/macros/s/AKfycbxXrcJa-ixI17suqC-4aO_AN1tI3tpp8qEs8lKAMHSBn9-mwbsxPSaAlgjyBZi68pBPFg/exec"; // === שנה כאן את הטקסט למיתוג שלך === const BRANDING_TEXT = "#Save_The_Bell 055-723-8041"; let activeEvent = { name: "אירוע חדש", eventType: "", date: new Date().toISOString(), frameTitle: "מזכרת מהאירוע", frameSubtitle: "04.12.2025", useFrame: true, isLocked: false, maxPhotosPerDevice: 3 // <--- הוספה יחידה: ברירת מחדל }; app.use(express.json({ limit: '50mb' })); app.use(express.static(path.join(__dirname, 'dist'), { index: false })); // === פרוקסי ל-FAL === app.all("/api/fal/proxy", async (req, res) => { const targetUrl = req.headers["x-fal-target-url"]; if (!targetUrl) return res.status(400).json({ error: "Missing x-fal-target-url header" }); try { const response = await fetch(targetUrl, { method: req.method, headers: { "Authorization": `Key ${process.env.FAL_KEY}`, "Content-Type": "application/json", "Accept": "application/json" }, body: req.method === "GET" ? undefined : JSON.stringify(req.body) }); const data = await response.json(); res.status(response.status).json(data); } catch (err) { console.error("FAL Proxy Error:", err); res.status(500).json({ error: "Proxy Error" }); } }); // === פונקציית המגנט === async function addPolaroidFrame(imageBuffer, title, subtitle) { try { const originalMetadata = await sharp(imageBuffer).metadata(); const isLandscape = originalMetadata.width > originalMetadata.height; let CANVAS_WIDTH, CANVAS_HEIGHT, FOOTER_HEIGHT; const BORDER_THICKNESS = 40; // הגדרות מידות if (isLandscape) { CANVAS_WIDTH = 2000; CANVAS_HEIGHT = 1500; FOOTER_HEIGHT = 180; } else { CANVAS_WIDTH = 1500; CANVAS_HEIGHT = 2000; FOOTER_HEIGHT = 220; } const targetPhotoWidth = CANVAS_WIDTH - (BORDER_THICKNESS * 2); const targetPhotoHeight = CANVAS_HEIGHT - FOOTER_HEIGHT - BORDER_THICKNESS; // 1. עיבוד התמונה (Resize) const photo = await sharp(imageBuffer) .resize({ width: targetPhotoWidth, height: targetPhotoHeight, fit: 'cover', position: 'center' }) .toBuffer(); // 2. יצירת הטקסט והרקע הלבן const textCenterY = CANVAS_HEIGHT - (FOOTER_HEIGHT / 2); const titleSize = isLandscape ? 55 : 65; const subTitleSize = isLandscape ? 30 : 35; // === הוספת המיתוג בצד שמאל (אנכי) - בדיוק כמו בקוד שלך === const brandingSVG = ` ${BRANDING_TEXT} `; const svgText = ` ${title} ${subtitle} ${brandingSVG} `; // 3. הרכבה סופית const finalImage = await sharp(Buffer.from(svgText)) .composite([ { input: photo, top: BORDER_THICKNESS, left: BORDER_THICKNESS } ]) .jpeg({ quality: 100 }) .toBuffer(); return finalImage; } catch (err) { console.error("Frame Error:", err); return imageBuffer; } } // --- API --- app.get('/api/event', (req, res) => { res.json({ activeEvent, status: "Ready" }); }); app.post('/api/event', (req, res) => { if (req.body.name) activeEvent.name = req.body.name; if (req.body.eventType !== undefined) activeEvent.eventType = req.body.eventType; if (req.body.frameTitle !== undefined) activeEvent.frameTitle = req.body.frameTitle; if (req.body.frameSubtitle !== undefined) activeEvent.frameSubtitle = req.body.frameSubtitle; if (req.body.useFrame !== undefined) activeEvent.useFrame = req.body.useFrame; if (req.body.isLocked !== undefined) activeEvent.isLocked = req.body.isLocked; // <--- הוספה יחידה: קבלת ההגבלה מהדשבורד ---> if (req.body.maxPhotosPerDevice !== undefined) activeEvent.maxPhotosPerDevice = req.body.maxPhotosPerDevice; res.json({ success: true, activeEvent }); }); app.post('/api/upload', upload.single('photo'), async (req, res) => { if (!req.file) return res.status(400).json({ error: "אין תמונה" }); try { let finalBuffer = req.file.buffer; // === כאן קורית המסגרת === if (activeEvent.useFrame) { console.log("🖼️ Generating Magnet Frame..."); finalBuffer = await addPolaroidFrame(req.file.buffer, activeEvent.frameTitle, activeEvent.frameSubtitle); } else { console.log("⚠️ Frame generation disabled in settings"); } const response = await axios.post(GOOGLE_SCRIPT_URL, { photo: finalBuffer.toString('base64'), mimeType: 'image/jpeg', filename: 'magnet_print.jpg', caption: `📸 ${activeEvent.name}` }); if (response.data.status === 'error') throw new Error(response.data.message); res.json({ success: true, message: "נשלח" }); } catch (error) { console.error(error); res.status(500).json({ error: "שגיאה בשליחה" }); } }); app.get('*', (req, res) => { const indexPath = path.join(__dirname, 'dist', 'index.html'); if (fs.existsSync(indexPath)) res.sendFile(indexPath); else res.status(500).send('Building...'); }); app.listen(PORT, () => console.log(`Server running on ${PORT}`));