Add dynamic export file name

Closes #387
This commit is contained in:
rijkvanzanten
2020-09-21 17:56:35 -04:00
parent 15043c7a95
commit ba85e4e6a2
15 changed files with 76 additions and 14 deletions

View File

@@ -4,7 +4,7 @@ import env from "../env";
import { getCacheKey } from "../utils/get-cache-key";
import cache from '../cache';
import { Transform, transforms } from 'json2csv';
import { PassThrough } from "stream";
import { PassThrough } from 'stream';
export const respond: RequestHandler = asyncHandler(async (req, res) => {
if (req.method.toLowerCase() === 'get' && env.CACHE_ENABLED === true && cache && !req.sanitizedQuery.export) {
@@ -12,20 +12,44 @@ export const respond: RequestHandler = asyncHandler(async (req, res) => {
await cache.set(key, res.locals.payload);
}
if (req.sanitizedQuery.export === 'json') {
res.attachment('export.json');
res.set('Content-Type', 'application/json');
return res.status(200).send(JSON.stringify(res.locals.payload, null, '\t'));
}
if (req.sanitizedQuery.export) {
let filename = '';
if (req.sanitizedQuery.export === 'csv') {
res.attachment('export.csv');
res.set('Content-Type', 'text/csv');
const stream = new PassThrough();
stream.end(Buffer.from(JSON.stringify(res.locals.payload.data), 'utf-8'));
const json2csv = new Transform({ transforms: [transforms.flatten({ separator: '.' })] });
return stream.pipe(json2csv).pipe(res);
if (req.collection) {
filename += req.collection;
} else {
filename += 'Export';
}
filename += ' ' + getDateFormatted();
if (req.sanitizedQuery.export === 'json') {
res.attachment(`${filename}.json`);
res.set('Content-Type', 'application/json');
return res.status(200).send(JSON.stringify(res.locals.payload, null, '\t'));
}
if (req.sanitizedQuery.export === 'csv') {
res.attachment(`${filename}.csv`);
res.set('Content-Type', 'text/csv');
const stream = new PassThrough();
stream.end(Buffer.from(JSON.stringify(res.locals.payload.data), 'utf-8'));
const json2csv = new Transform({ transforms: [transforms.flatten({ separator: '.' })] });
return stream.pipe(json2csv).pipe(res);
}
}
return res.json(res.locals.payload);
});
function getDateFormatted() {
const date = new Date();
let month = String(date.getMonth() + 1);
if (month.length === 1) month = '0' + month;
let day = String(date.getDate());
if (day.length === 1) day = '0' + day;
return `${date.getFullYear()}-${month}-${day} at ${date.getHours()}.${date.getMinutes()}.${date.getSeconds()}`;
}