redirect if folder is accessed without trailing slash

Massimo Melina committed Dec 15, 2021 at 14:47 UTC b89baf27e109cd4775b25557f513a205df48ad25
2 files changed +8 -2
dev-notes.md
+1 -1
@@ -18,7 +18,7 @@
18 - vfs: serve an html for a folder?
19 - "default" property for vfsNode?
20 - sticky breadcrumbs
21 -- interruption of long requests if client aborted
21 +- interruption of long requests if client aborted (searching/listing)
22 - frontend
23 - don't depend on cdn
24 - webdav?
src/index.ts
+7 -1
@@ -6,6 +6,7 @@ import { serveFrontend } from './frontend'
6 import { API_URI, argv, DEV, FRONTEND_URI } from './const'
7 import { createReadStream } from 'fs'
8 import { vfs } from './vfs'
9 +import { stat } from 'fs/promises'
10
11 const PORT = argv.port || 80
12
@@ -29,7 +30,12 @@ srv.use(async (ctx, next) => {
30 return await serveFrontend(ctx,next)
31 if (path.startsWith(FRONTEND_URI))
32 return await serveFrontendPrefixed(ctx,next)
32 - const source = vfs.urlToNode(decodeURI(path))?.source
33 + const node = vfs.urlToNode(decodeURI(path))
34 + if (!node)
35 + return await next()
36 + const { source } = node
37 + if (!source || (await stat(source)).isDirectory()) // this folder was requested without the trailing /
38 + return ctx.redirect(ctx.path+'/')
39 if (source)
40 ctx.body = createReadStream(source as string)
41 })