Server/project/src/utils/HttpFileUtil.ts

28 lines
823 B
TypeScript
Raw Normal View History

import fs from "node:fs";
import { ServerResponse } from "node:http";
2023-03-03 15:23:46 +00:00
import { inject, injectable } from "tsyringe";
import { HttpServerHelper } from "@spt-aki/helpers/HttpServerHelper";
2023-03-03 15:23:46 +00:00
@injectable()
export class HttpFileUtil
{
2023-11-15 20:35:05 -05:00
constructor(@inject("HttpServerHelper") protected httpServerHelper: HttpServerHelper)
2023-03-03 15:23:46 +00:00
{
}
public sendFile(resp: ServerResponse, filePath: string): void
2023-03-03 15:23:46 +00:00
{
const pathSlic = filePath.split("/");
2023-11-15 20:35:05 -05:00
const type = this.httpServerHelper.getMimeText(pathSlic[pathSlic.length - 1].split(".").at(-1))
|| this.httpServerHelper.getMimeText("txt");
const fileStream = fs.createReadStream(filePath);
2023-03-03 15:23:46 +00:00
fileStream.on("open", () =>
2023-03-03 15:23:46 +00:00
{
resp.setHeader("Content-Type", type);
fileStream.pipe(resp);
});
}
2023-11-15 20:35:05 -05:00
}