Skip to content

Commit

Permalink
Fix issue with directory listings on subdomains
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrichina committed Oct 2, 2024
1 parent a69d267 commit a5cb58c
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ const serveFile = async ctx => {
// TODO: What should be Cache-Control
// Return CID-based Etag like IPFS gateways
ctx.set('Etag', `W/"${cidToString(cid)}"`);
const ipfsPath = `/ipfs/${cidToString(rootCid)}/${path || ''}`;
const ipfsPath = `/ipfs/${cidToString(rootCid)}/${path}`;
const basePath = (ctx.usingSubdomain ? '/' : `/ipfs/${cidToString(rootCid)}/`) + path;
ctx.body = `
<html>
<head>
Expand All @@ -67,7 +68,7 @@ const serveFile = async ctx => {
<body>
<h1>Index of ${ipfsPath}</h1>
<ul>
${node.links.map(link => `<li><a href="${ipfsPath}${link.name}">${link.name}</a></li>`).join('\n')}
${node.links.map(link => `<li><a href="${basePath}${link.name}">${link.name}</a></li>`).join('\n')}
</ul>
</body>
</html>
Expand Down Expand Up @@ -172,6 +173,7 @@ const handleSubdomain = async (ctx, next) => {
ctx.params = ctx.params || {};
ctx.params.cid = subdomain;
ctx.params.path = ctx.path.slice(1); // Remove leading slash
ctx.usingSubdomain = true;
await serveFile(ctx);
} catch (error) {
// If it's not a valid CID, continue to the next middleware
Expand Down

0 comments on commit a5cb58c

Please sign in to comment.