@opengis/fastify-table
Version:
core-plugins
51 lines (50 loc) • 1.91 kB
JavaScript
import path from "node:path";
import downloadFile from "../file/downloadFile.js";
import getGRPC from "./grpc.js";
const grpc = getGRPC();
const file2json = async ({ buffer: buffer1, ext: ext1, filepath = "", encoding: encoding1 = "UTF-8", debug, }) => {
if (!filepath && !(buffer1?.length && ext1))
return null;
const ext = ext1 || path.extname(filepath || "");
const encoding = { CP1251: "windows-1251" }[encoding1] || encoding1;
const buffer = buffer1 ||
(await downloadFile(filepath, { provider: "fs", buffer: true, debug })) ||
"{}";
if (debug)
return { filepath, buffer };
if ([".xls", ".xlsx"].includes(ext)) {
const { result } = await grpc.excelToJson({
base64: buffer.toString("base64"),
type: "xls",
});
const json = typeof result === "object" ? result : JSON.parse(result || {});
return json;
}
if ([".csv", ".txt"].includes(ext)) {
const { result } = await grpc.excelToJson({
base64: buffer.toString("base64"),
type: "csv",
encoding,
});
const json = typeof result === "object" ? result : JSON.parse(result || {});
return json;
}
if ([".zip"].includes(ext)) {
const { result } = await grpc.shpToGeojson({
base64: buffer.toString("base64"),
encoding,
});
const json = typeof result === "object" ? result : JSON.parse(result);
return json;
}
if ([".xml"].includes(ext)) {
const { result } = await grpc.xmlToJson({ xml: buffer.toString("utf-8") });
const json = typeof result === "object" ? result : JSON.parse(result);
return json;
}
if ([".geojson", ".json"].includes(ext)) {
return JSON.parse(buffer.toString("utf-8"));
}
return filepath.replace(/"/g, "");
};
export default file2json;