@opengis/fastify-table
Version:
core-plugins
32 lines (31 loc) • 1.27 kB
JavaScript
import path from "node:path";
import { existsSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { prefix, metaDir, uploadChunkDirectory, fetchTimeoutMs, } from "./index.js";
import isFileExists from "../file/isFileExists.js";
export default async function getUploadStatus({ host, id, }) {
// return local file metadata
if (!host) {
const metaExists = existsSync(path.join(metaDir, `${id}.json`));
// check file upload status: finished/inprogress
const meta = metaExists
? JSON.parse(await readFile(path.join(metaDir, `${id}.json`), "utf8"))
: {};
const fileExists = await isFileExists(path
.join(uploadChunkDirectory, `${id}.${meta.extension}`)
.replace(/\\/g, "/"));
return {
...meta,
uploadChunkDirectory,
exists: !!fileExists,
finished: meta.uploaded && meta.size && meta.uploaded === meta.size,
};
}
// request remote file upload status
const resp = await fetch(`${host}/${prefix}/${id}`, {
signal: AbortSignal.timeout(fetchTimeoutMs),
}).catch((err) => ({
json: () => Promise.resolve({ error: err.toString(), code: 501 }),
}));
return resp.json();
}