serve from file instead of object
This commit is contained in:
parent
7fd4342405
commit
ea72b74447
|
|
@ -193,21 +193,49 @@ export const createServer = async () => {
|
||||||
|
|
||||||
if (core[pathname]) content = core[pathname];
|
if (core[pathname]) content = core[pathname];
|
||||||
else if (site[pathname]) content = site[pathname];
|
else if (site[pathname]) content = site[pathname];
|
||||||
else if (pub[pathname]){
|
else if (pub[pathname]) {
|
||||||
//read content from file
|
// Serve file directly from public folder
|
||||||
content = await Bun.file(dir(`public/${pathname}`)).text();
|
const filePath = dir(`public/${pathname}`);
|
||||||
//detect file type
|
const file = Bun.file(filePath);
|
||||||
const fileType = await fileTypeFromBlob(Bun.file(dir(`public/${pathname}`)));
|
|
||||||
if(fileType){
|
// Check if file exists
|
||||||
//set content type
|
if (!(await file.exists())) {
|
||||||
opt?.headers?.set("Content-Type", fileType.mime);
|
return new Response("File not found", { status: 404 });
|
||||||
}
|
|
||||||
|
|
||||||
return new Response(content, {
|
|
||||||
status: 200,
|
|
||||||
headers: opt?.headers || new Headers(),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Detect file type
|
||||||
|
const fileType = await fileTypeFromBlob(file);
|
||||||
|
|
||||||
|
// Set content type
|
||||||
|
if (fileType) {
|
||||||
|
opt?.headers?.set("Content-Type", fileType.mime);
|
||||||
|
} else {
|
||||||
|
// Fallback to extension-based MIME type detection
|
||||||
|
const ext = pathname.split('.').pop()?.toLowerCase();
|
||||||
|
const mimeTypes: Record<string, string> = {
|
||||||
|
'html': 'text/html',
|
||||||
|
'css': 'text/css',
|
||||||
|
'js': 'application/javascript',
|
||||||
|
'json': 'application/json',
|
||||||
|
'png': 'image/png',
|
||||||
|
'jpg': 'image/jpeg',
|
||||||
|
'jpeg': 'image/jpeg',
|
||||||
|
'gif': 'image/gif',
|
||||||
|
'svg': 'image/svg+xml',
|
||||||
|
'ico': 'image/x-icon',
|
||||||
|
'txt': 'text/plain',
|
||||||
|
};
|
||||||
|
if (ext && mimeTypes[ext]) {
|
||||||
|
opt?.headers?.set("Content-Type", mimeTypes[ext]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the file directly (Bun will stream it efficiently)
|
||||||
|
return new Response(file, {
|
||||||
|
status: 200,
|
||||||
|
headers: opt?.headers || new Headers(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue