Skip to content

Commit

Permalink
Fix case downloads #2670
Browse files Browse the repository at this point in the history
  • Loading branch information
iamleeg committed May 5, 2022
1 parent c7ec23c commit 6cc1b50
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions data-serving/data-service/src/controllers/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,21 @@ export class CasesController {
}

const date = new Date().toISOString().slice(0, 10);
const query = req.body.query.replace(/[:\s]/g, '_');
const filename = `gh_${date}_${query}`;
const request_description = req.body.query ? req.body.query.replace(/[:\s]/g, '_') : 'requested_cases';
const filename = `gh_${date}_${request_description}`;

let doc: CaseDocument;

if (req.body.format == 'csv' || req.body.format == 'tsv') {
res.setHeader('Content-Type', `text/${req.body.format}`);
// assume default format is CSV
const format = req.body.format ?? 'csv';

if (format == 'csv' || format == 'tsv') {
res.setHeader('Content-Type', `text/${format}`);
res.setHeader(
'Content-Disposition',
`attachment; filename="${filename}.${req.body.format}"`,
`attachment; filename="${filename}.${format}"`,
);
const delimiter: string = req.body.format == 'tsv' ? '\t' : ',';
const delimiter: string = format == 'tsv' ? '\t' : ',';

const columnsString = this.csvHeaders.join(delimiter);
res.write(columnsString);
Expand All @@ -251,7 +254,7 @@ export class CasesController {
doc = await cursor.next();
}
res.end();
} else if (req.body.format == 'json') {
} else if (format == 'json') {
res.setHeader('Content-Type', 'application/json');
res.setHeader(
'Content-Disposition',
Expand All @@ -275,10 +278,9 @@ export class CasesController {
res.write(']');
res.end();
} else {
logger.error(`Invalid format requested ${req.body.format}`);
res.status(400).json(
`Invalid format requested ${req.body.format}`,
);
const message = `Invalid format requested ${format}`;
logger.error(message);
res.status(400).json(message);
return;
}
} catch (err) {
Expand Down

0 comments on commit 6cc1b50

Please sign in to comment.