Server/project/src/utils/HttpFileUtil.ts

23 lines
815 B
TypeScript
Raw Normal View History

import fs from "node:fs";
import { ServerResponse } from "node:http";
import { HttpServerHelper } from "@spt/helpers/HttpServerHelper";
import { inject, injectable } from "tsyringe";
2023-03-03 15:23:46 +00:00
@injectable()
export class HttpFileUtil {
constructor(@inject("HttpServerHelper") protected httpServerHelper: HttpServerHelper) {}
2023-03-03 15:23:46 +00:00
public sendFile(resp: ServerResponse, filePath: string): void {
const pathSlic = filePath.split("/");
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
}