Server/project/src/utils/HttpFileUtil.ts

28 lines
815 B
TypeScript
Raw Normal View History

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