24 lines
831 B
TypeScript
24 lines
831 B
TypeScript
import RestResponse from "./RestResponse.ts";
|
|
import { Handler } from "./Router.ts";
|
|
|
|
export default function staticHandler(path: string): Handler {
|
|
return {
|
|
async handle(req) {
|
|
try {
|
|
const realPath = await Deno.realPath(`${path}/${req.url}`);
|
|
const stream = await Deno.open(realPath, { read: true });
|
|
const i = realPath.lastIndexOf('.');
|
|
const res = new RestResponse().body(stream.readable);
|
|
await stream.read(new Uint8Array(1));
|
|
await stream.seek(0, Deno.SeekMode.Start);
|
|
|
|
if (i >= 0) res.contentType(realPath.substring(i + 1));
|
|
return res;
|
|
}
|
|
catch (e) {
|
|
console.log(e);
|
|
return undefined;
|
|
}
|
|
}
|
|
};
|
|
} |