2021-05-27 11:06:26 +00:00
|
|
|
import * as Comm from 'app/server/lib/Comm';
|
2021-07-21 08:46:03 +00:00
|
|
|
import {parseExportFileName, parseExportParameters} from 'app/server/lib/Export';
|
|
|
|
import {makeCSV} from 'app/server/lib/ExportCSV';
|
|
|
|
import {makeXLSX} from 'app/server/lib/ExportXLSX';
|
|
|
|
import * as log from 'app/server/lib/log';
|
|
|
|
import * as contentDisposition from 'content-disposition';
|
|
|
|
import * as express from 'express';
|
2021-05-27 11:06:26 +00:00
|
|
|
|
|
|
|
export async function generateCSV(req: express.Request, res: express.Response, comm: Comm) {
|
|
|
|
log.info('Generating .csv file...');
|
2021-07-21 08:46:03 +00:00
|
|
|
const {
|
|
|
|
viewSectionId,
|
|
|
|
filters,
|
|
|
|
sortOrder
|
|
|
|
} = parseExportParameters(req);
|
2021-05-27 11:06:26 +00:00
|
|
|
|
2021-07-21 08:46:03 +00:00
|
|
|
const clientId = req.query.clientId;
|
|
|
|
const docFD = parseInt(req.query.docFD, 10);
|
2021-05-27 11:06:26 +00:00
|
|
|
const client = comm.getClient(clientId);
|
|
|
|
const docSession = client.getDocSession(docFD);
|
|
|
|
const activeDoc = docSession.activeDoc;
|
|
|
|
|
|
|
|
// Generate a decent name for the exported file.
|
2021-07-21 08:46:03 +00:00
|
|
|
const name = parseExportFileName(activeDoc, req);
|
2021-05-27 11:06:26 +00:00
|
|
|
try {
|
2021-07-21 08:46:03 +00:00
|
|
|
const data = await makeCSV(activeDoc, viewSectionId, sortOrder, filters, req);
|
2021-05-27 11:06:26 +00:00
|
|
|
res.set('Content-Type', 'text/csv');
|
2021-07-21 08:46:03 +00:00
|
|
|
res.setHeader('Content-Disposition', contentDisposition(name + '.csv'));
|
2021-05-27 11:06:26 +00:00
|
|
|
res.send(data);
|
|
|
|
} catch (err) {
|
|
|
|
log.error("Exporting to CSV has failed. Request url: %s", req.url, err);
|
|
|
|
const errHtml =
|
|
|
|
`<!doctype html>
|
|
|
|
<html>
|
|
|
|
<body>There was an unexpected error while generating a csv file.</body>
|
|
|
|
</html>
|
|
|
|
`;
|
|
|
|
res.status(400).send(errHtml);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 08:46:03 +00:00
|
|
|
export async function generateXLSX(req: express.Request, res: express.Response, comm: Comm) {
|
|
|
|
log.debug(`Generating .xlsx file`);
|
|
|
|
const clientId = req.query.clientId;
|
|
|
|
const docFD = parseInt(req.query.docFD, 10);
|
|
|
|
const client = comm.getClient(clientId);
|
|
|
|
const docSession = client.getDocSession(docFD);
|
|
|
|
const activeDoc = docSession.activeDoc;
|
|
|
|
try {
|
|
|
|
const data = await makeXLSX(activeDoc, req);
|
|
|
|
res.set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
|
|
res.setHeader('Content-Disposition', contentDisposition((req.query.title || activeDoc.docName) + '.xlsx'));
|
|
|
|
res.send(data);
|
|
|
|
log.debug('XLSX file generated');
|
|
|
|
} catch (err) {
|
|
|
|
log.error("Exporting to XLSX has failed. Request url: %s", req.url, err);
|
|
|
|
// send a generic information to client
|
|
|
|
const errHtml =
|
|
|
|
`<!doctype html>
|
|
|
|
<html>
|
|
|
|
<body>There was an unexpected error while generating a xlsx file.</body>
|
|
|
|
</html>
|
|
|
|
`;
|
|
|
|
res.status(400).send(errHtml);
|
2021-05-27 11:06:26 +00:00
|
|
|
}
|
|
|
|
}
|