Оновлений генератор карток території

This commit is contained in:
2026-03-22 01:29:39 +02:00
parent 4bc9c11512
commit 415b9b1090
163 changed files with 94 additions and 36 deletions

4
.gitignore vendored
View File

@@ -89,4 +89,6 @@ Temporary Items
.ftppass
*.sqlite
*.sqlite
cards/

View File

@@ -4,55 +4,100 @@ const path = require('path');
const { chromium } = require('playwright');
const sharp = require('sharp');
const DIR = process.env.CARDS_PATH || '../cards';
let globalBrowser;
const queue = [];
let isProcessing = false;
async function genCards({ id, type }) {
if (!process.env.DOMAIN) throw new Error("❌ DOMAIN не заданий у .env");
if (!process.env.ADMIN_TOKEN) throw new Error("❌ ADMIN_TOKEN не заданий у .env");
if (!process.env.CARDS_PATH) throw new Error("❌ CARDS_PATH не заданий у .env");
// Ініціалізація браузера (один раз на весь життєвий цикл додатка)
async function getBrowser() {
if (!globalBrowser) {
globalBrowser = await chromium.launch({
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || undefined,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-gpu',
'--disable-dev-shm-usage'
]
});
}
return globalBrowser;
}
// Функція обробки черги
async function processQueue() {
if (isProcessing || queue.length === 0) return;
const name = type == 'homestead' ? `H${id}` : `T${id}`
isProcessing = true;
const { task, resolve, reject } = queue.shift();
const URL = `https://${process.env.DOMAIN}/api/${type}/${id}`; // Замени на свой URL
const AUTH_TOKEN = process.env.ADMIN_TOKEN;
const DIR = process.env.CARDS_PATH || '../cards';
const SCREENSHOT_FILE = path.resolve(`${DIR}/cache/${type}/${name}.png`);
const OUTPUT_FILE = path.resolve(`${DIR}/${type}/${name}.webp`);
try {
const result = await runGeneration(task);
resolve(result);
} catch (err) {
reject(err);
} finally {
isProcessing = false;
processQueue(); // беремо наступне завдання
}
}
// Основна логіка генерації
async function runGeneration({ id, type }) {
const { DOMAIN, ADMIN_TOKEN, CARDS_PATH } = process.env;
const name = type === 'homestead' ? `H${id}` : `T${id}`;
const URL = `https://${DOMAIN}/api/${type}/${id}`;
const baseDir = path.resolve(CARDS_PATH);
const cacheDir = path.join(baseDir, 'cache', type);
const outputDir = path.join(baseDir, type);
// --- Отримуємо дані ---
fs.mkdirSync(cacheDir, { recursive: true });
fs.mkdirSync(outputDir, { recursive: true });
const SCREENSHOT_FILE = path.join(cacheDir, `${name}.png`);
const OUTPUT_FILE = path.join(outputDir, `${name}.webp`);
// 1. Отримання даних
const res = await fetch(URL, {
headers: { 'Authorization': AUTH_TOKEN, 'Accept': 'application/json' }
headers: { 'Authorization': ADMIN_TOKEN, 'Accept': 'application/json' }
});
if (!res.ok) throw new Error(`Помилка запиту: ${res.status}`);
if (!res.ok) throw new Error(`API Error: ${res.status}`);
const data = await res.json();
// --- Генеруємо HTML ---
// 2. Підготовка HTML
const tmpFile = path.join(os.tmpdir(), `map_${type}_${id}_${Date.now()}.html`);
const html = `<!DOCTYPE html><html><head><meta charset="UTF-8" /><link rel="stylesheet" href="https://sheep-service.com/lib/components/leaflet/leaflet.css" /><script src="https://sheep-service.com/lib/components/leaflet/leaflet.js"></script><script src="https://sheep-service.com/lib/components/qrcode.min.js"></script><style>@import url("https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap");html, body {height: calc(100% + 50px);}* {border: 0;padding: 0;font-family: "Roboto", sans-serif;margin: 0;font-weight: 500;outline: none;}#content {width: 100%;height: 100%;position: absolute;z-index: 9999;display: flex;flex-direction: column;}#content > #title {width: calc(100% - 30px);min-height: 90px;background: rgb(255 255 255 / 51%);backdrop-filter: blur(10px);display: flex;flex-direction: row;justify-content: space-between;padding: 15px;align-items: center;color: #333;}#content > #title > div {display: flex;flex-direction: column;width: 100%;align-items: center;}#content > #title > h1 {font-size: 60px;font-weight: 700;margin: 0;min-width: 200px;text-align: center;}#content > #title > div > h2 {font-size: 35px;font-weight: 700;margin: 0;}#content > #title > div > h3 {font-size: 28px;font-weight: 500;margin: 0px 0 10px 0;}#qrcode {position: absolute;bottom: 20px;left: 20px;width: 100px;height: 100px;background: #fff;padding: 10px;border-radius: 2px;}.line-left {position: absolute;left: 0;top: 120px;width: 10px;height: calc(100% - 120px);background: rgb(255 255 255 / 51%);backdrop-filter: blur(10px);}.line-right {position: absolute;right: 0;top: 120px;width: 10px;height: calc(100% - 120px);background: rgb(255 255 255 / 51%);backdrop-filter: blur(10px);}.line-bottom {position: absolute;left: 10px;bottom: 0;width: calc(100% - 20px);height: 10px;background: rgb(255 255 255 / 51%);backdrop-filter: blur(10px);}#map {width: 100%;height: 100%;}</style></head><body><div id="content"><div id="title"><div><h3>Картка плану території</h3><h2 id="address"></h2></div><h1 id="number"></h1></div><div id="qrcode"></div><div class="line-left"></div><div class="line-right"></div><div class="line-bottom"></div></div><div id="map"></div><script>const urlParams = new URLSearchParams(window.location.search);let coordinates = ${JSON.stringify(data.points)};let center = [${data.geo.lat}, ${data.geo.lng}];let zoom = ${data.zoom};let type = "${type}";let address = "${data.title} ${data.number ?? ""}";let osm = L.tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",{});let mytile = L.tileLayer("https://sheep-service.com/map/{z}/{x}/{y}.webp",{maxZoom: 20,minZoom: 15,tms: true,});let map = L.map("map", {renderer: L.canvas(),center,zoom,zoomControl: false,layers: [osm, mytile],});let baseMaps = {OpenStreetMap: osm,"Sheep Service Map": mytile,};let polygonOptions =type === "homestead"? {color: "#f2bd53",radius: 500,fillOpacity: 0.3,dashArray: "20,15",dashOffset: "20",}: {color: "#585858",fillColor: "#f2bd53",fillOpacity: 0.8,};window.onload = async function () {let polygon = L.polygon(coordinates, polygonOptions).addTo(map);let text = 'https://www.google.com/maps/search/?api=1&query='+center[0]+','+center[1];new QRCode(document.getElementById("qrcode"), {text: text,width: 100,height: 100,correctLevel: QRCode.CorrectLevel.L,version: 5,});document.getElementById("address").innerText = address;document.getElementById("number").innerText = '${name}';};</script></body></html>`;
// --- Зберігаємо тимчасовий HTML ---
const tmpFile = path.join(os.tmpdir(), 'map.html');
fs.writeFileSync(tmpFile, html, 'utf-8');
// --- Зберігаємо ---
// const browser = await chromium.launch();
const browser = await chromium.launch({
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || '/usr/bin/chromium-browser',
args: ['--no-sandbox', '--disable-setuid-sandbox']
let page;
try {
const browser = await getBrowser();
page = await browser.newPage({ viewport: { width: 811, height: 531 } });
await page.goto(`file://${tmpFile}`, { waitUntil: 'networkidle', timeout: 30000 });
await page.waitForTimeout(2000);
const buffer = await page.screenshot();
// Зберігаємо файли
await fs.promises.writeFile(SCREENSHOT_FILE, buffer);
await sharp(buffer).webp({ quality: 85 }).toFile(OUTPUT_FILE);
console.log(`✅ Зображення оновлено: ${name}.webp`);
return true;
} finally {
if (page) await page.close();
if (fs.existsSync(tmpFile)) fs.unlinkSync(tmpFile); // Обов'язково видаляємо сміття
}
}
// Експортуємо функцію, яка додає завдання в чергу
function genCards(params) {
return new Promise((resolve, reject) => {
queue.push({ task: params, resolve, reject });
processQueue();
});
const page = await browser.newPage({ viewport: { width: 811, height: 531 } });
await page.goto(`file://${tmpFile}`);
await page.waitForTimeout(2000); // пауза 1 секунда для провантаження карти
await page.screenshot({ path: SCREENSHOT_FILE });
await browser.close();
await sharp(SCREENSHOT_FILE)
.webp()
.toFile(OUTPUT_FILE);
console.log(`Зображеня збережене: ${name}.png`);
// return fs.existsSync(OUTPUT_FILE);
}
module.exports = genCards;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 365 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 210 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

Some files were not shown because too many files have changed in this diff Show More