export view=2 HTML table
This commit is contained in:
+40
-6
@@ -13,8 +13,9 @@
|
||||
//
|
||||
// ПАРАМЕТРЫ:
|
||||
// ?companyId=X — фильтр по компании (внутренний id)
|
||||
// ?view=1 — показать в браузере (иначе — скачивание)
|
||||
// ?filename=X — имя файла (по умолчанию white-list.txt)
|
||||
// ?view=1 — text/plain inline (копирование)
|
||||
// ?view=2 — HTML таблица с выровненными IP
|
||||
// ?filename=X — имя файла (скачивание, когда нет view)
|
||||
// ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
const express = require('express');
|
||||
@@ -29,13 +30,26 @@ function createExportRouter() {
|
||||
const companyId = req.query.companyId ? parseInt(req.query.companyId) : null;
|
||||
const cidrs = await q.getExportCIDRs(companyId);
|
||||
const aggregated = aggregateCIDRs(cidrs);
|
||||
|
||||
// ?view=2 — HTML таблица с выровненными IP
|
||||
if (req.query.view === '2') {
|
||||
res.set('Content-Type', 'text/html; charset=utf-8');
|
||||
return res.send(renderTable(aggregated));
|
||||
}
|
||||
|
||||
// ?view=1 — text/plain inline
|
||||
if (req.query.view === '1') {
|
||||
const text = aggregated.join('\n') + (aggregated.length ? '\n' : '');
|
||||
res.set('Content-Type', 'text/plain; charset=utf-8');
|
||||
res.set('Content-Disposition', 'inline');
|
||||
return res.send(text);
|
||||
}
|
||||
|
||||
// Скачивание (по умолчанию)
|
||||
const text = aggregated.join('\n') + (aggregated.length ? '\n' : '');
|
||||
|
||||
res.set('Content-Type', 'text/plain; charset=utf-8');
|
||||
|
||||
const fname = (req.query.filename || 'white-list.txt').replace(/[^\w\-_. ]/g, '_');
|
||||
const disp = req.query.view === '1' ? 'inline' : 'attachment';
|
||||
res.set('Content-Disposition', disp + '; filename="' + fname + '"');
|
||||
res.set('Content-Disposition', 'attachment; filename="' + fname + '"');
|
||||
res.send(text);
|
||||
} catch (e) {
|
||||
console.error('[v2:export] Error:', e);
|
||||
@@ -46,4 +60,24 @@ function createExportRouter() {
|
||||
return router;
|
||||
}
|
||||
|
||||
function padIP(cidr) {
|
||||
const [ip, mask] = cidr.split('/');
|
||||
const octets = ip.split('.').map(o => o.padStart(3, ' '));
|
||||
return octets.join('.') + '/' + mask;
|
||||
}
|
||||
|
||||
function renderTable(cidrs) {
|
||||
const rows = cidrs.map(c => '<tr><td><code>' + padIP(c) + '</code></td></tr>').join('\n');
|
||||
return '<!DOCTYPE html><html><head><meta charset="utf-8"><title>WhiteList Export</title>'
|
||||
+ '<style>body{font-family:monospace;background:#fff;color:#111;padding:1rem}'
|
||||
+ 'h2{font-size:1rem;color:#666;margin-bottom:.5rem}'
|
||||
+ 'table{border-collapse:collapse}'
|
||||
+ 'td{padding:.15rem 1rem .15rem 0}'
|
||||
+ 'code{font-size:.9rem}'
|
||||
+ '</style></head><body>'
|
||||
+ '<h2>WhiteList — ' + cidrs.length + ' записей</h2>'
|
||||
+ '<table>' + rows + '</table>'
|
||||
+ '</body></html>';
|
||||
}
|
||||
|
||||
module.exports = { createExportRouter };
|
||||
|
||||
Reference in New Issue
Block a user