@opengis/fastify-table
Version:
core-plugins
18 lines (15 loc) • 577 B
JavaScript
import path from 'node:path';
import { readFileSync } from 'node:fs';
export default function formatResult({
filePath, formatAnswer, folder, reply,
}) {
if (formatAnswer === 'file') {
const encoding = path.extname(filePath) === '.xlsx' ? 'base64' : 'utf8';
const data = readFileSync(filePath, encoding);
return reply.headers({ 'Content-Disposition': `attachment; filename=${path.basename(filePath)}` }).send(Buffer.from(data, encoding));
}
return {
filePath: filePath.replace(/\\/g, '/')?.replace(folder, ''),
status: 200,
};
}