v0.5.166: /export→/v2/export редирект, экспорт только админам
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* ui/routes/export.js — GET /export.
|
||||
* Только для администратора (adminMode).
|
||||
* Проксирует GET /api/v1/entries/export (text/plain).
|
||||
*/
|
||||
|
||||
const { Router } = require('express');
|
||||
const api = require('../api-client');
|
||||
|
||||
function createRouter() {
|
||||
const router = Router();
|
||||
|
||||
router.get('/export', async (req, res) => {
|
||||
if (!req.user || !req.user.adminMode) {
|
||||
return res.status(403).send('Только для администратора');
|
||||
}
|
||||
const token = api.token(req);
|
||||
// Прокидываем query-параметры в API
|
||||
const params = new URLSearchParams();
|
||||
if (req.query.company) params.set('company', req.query.company);
|
||||
if (req.query.client_id) params.set('client_id', req.query.client_id);
|
||||
const qs = params.toString();
|
||||
const companyQuery = qs ? '?' + qs : '';
|
||||
try {
|
||||
const r = await api.get('/api/v1/entries/export' + companyQuery, token);
|
||||
res.set('Content-Type', 'text/plain; charset=utf-8');
|
||||
// ?view=1 → inline (просмотр), иначе → attachment (скачивание)
|
||||
// ?filename=X → своё имя, по умолчанию white-list.txt
|
||||
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.send(r.data);
|
||||
} catch (e) {
|
||||
res.status(500).send('Ошибка экспорта: ' + e.message);
|
||||
}
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
module.exports = { createRouter };
|
||||
Reference in New Issue
Block a user