Skip to content

Commit

Permalink
fix: MIME type served for non-html files
Browse files Browse the repository at this point in the history
  • Loading branch information
00Fjongl committed Dec 25, 2024
1 parent 5829cd0 commit 94f87dc
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions src/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -132,32 +132,42 @@ app.register(fastifyStatic, {
});

app.register(fastifyStatic, {
root: fileURLToPath(new URL('../views/archive/gfiles/rarch', import.meta.url)),
root: fileURLToPath(
new URL('../views/archive/gfiles/rarch', import.meta.url)
),
prefix: '/serving/',
decorateReply: false,
});

app.register(fastifyStatic, {
root: fileURLToPath(new URL('../views/archive/gfiles/rarch/cores', import.meta.url)),
root: fileURLToPath(
new URL('../views/archive/gfiles/rarch/cores', import.meta.url)
),
prefix: '/cores/',
decorateReply: false,
});

app.register(fastifyStatic, {
root: fileURLToPath(new URL('../views/archive/gfiles/rarch/info', import.meta.url)),
root: fileURLToPath(
new URL('../views/archive/gfiles/rarch/info', import.meta.url)
),
prefix: '/info/',
decorateReply: false,
});

app.register(fastifyStatic, {
root: fileURLToPath(new URL('../views/archive/gfiles/rarch/cores', import.meta.url)),
root: fileURLToPath(
new URL('../views/archive/gfiles/rarch/cores', import.meta.url)
),
prefix: '/uauth/',
decorateReply: false,
});

// NEVER commit roms due to piracy concerns
app.register(fastifyStatic, {
root: fileURLToPath(new URL('../views/archive/gfiles/rarch/roms', import.meta.url)),
root: fileURLToPath(
new URL('../views/archive/gfiles/rarch/roms', import.meta.url)
),
prefix: '/roms/',
decorateReply: false,
});
Expand Down Expand Up @@ -280,16 +290,21 @@ app.get('/:path', (req, reply) => {
if (reqPath && !(reqPath in pages))
return reply.code(404).type('text/html').send(preloaded404);

reply.type('text/html').send(
// Set the index the as the default page. Serve as an html file by default.
const fileName = reqPath ? pages[reqPath] : pages.index,
supportedTypes = {
default: 'text/html',
html: 'text/html',
txt: 'text/plain',
xml: "application/xml",
ico: 'image/vnd.microsoft.icon',
},
type = supportedTypes[fileName.slice(fileName.lastIndexOf('.') + 1)];

reply.type(type || supportedTypes.default);
reply.send(
paintSource(
loadTemplates(
tryReadFile(
'../views/' +
// Set the index the as the default page.
(reqPath ? pages[reqPath] : pages.index),
import.meta.url
)
)
loadTemplates(tryReadFile('../views/' + fileName, import.meta.url))
)
);
});
Expand Down

0 comments on commit 94f87dc

Please sign in to comment.