@opengis/bi
Version:
BI data visualization module
59 lines (46 loc) • 1.79 kB
JavaScript
import path from 'node:path';
import { existsSync } from 'node:fs';
import { rm, mkdir, writeFile } from 'node:fs/promises';
import { createHash } from 'node:crypto';
import { config, logger } from '@opengis/fastify-table/utils.js';
export default async function downloadRemoteFile({
rootDir, url, table,
}) {
if (!rootDir) {
return { error: 'param rootDir is required', status: 400 };
}
if (!url) {
return { error: 'param url is required', status: 400 };
}
if (!table) {
return { error: 'param table is required', status: 400 };
}
try {
const response = await fetch(url);
if (response?.status !== 200) {
return { message: 'file not found', status: response?.status };
}
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
const contentType = response.headers.get('Content-Type');
// eslint-disable-next-line newline-per-chained-call
const extName = contentType === 'text/plain' ? path.extname(url) : `.${response.headers.get('Content-Type')?.split(';')?.shift()?.split('/')?.pop()?.replace(/\+|\-|\./g, '')}`; // path.extname(url)
const filePath = `/files/tmp/${createHash('md5').update([url, table].join()).digest('hex')}${extName}`;
const fullPath = path.join(rootDir, filePath);
if (config?.local) {
console.log(url, fullPath, existsSync(fullPath));
}
await mkdir(path.dirname(fullPath), { recursive: true });
if (existsSync(fullPath) && config?.local) {
await rm(fullPath);
}
if (!existsSync(fullPath)) {
await writeFile(fullPath, buffer);
}
return { filePath };
}
catch (err) {
logger.file('dataset/create/error', { url, error: err.toString() });
return { error: err.toString(), status: 500 };
}
}