fix: frontend files were not cache-enabeld

Massimo Melina committed Jan 8, 2022 at 20:08 UTC fedaabaa94fe5e3649fa919940732077df32e6f7
5 files changed +28 -20
frontend/src/menu.ts
+2 -2
@@ -33,8 +33,8 @@ export function MenuPanel() {
33 }),
34 h(MenuButton, getSearchProps()),
35 h(MenuButton, {
36 - icon: 'sort',
37 - label: 'Sort',
36 + icon: 'options',
37 + label: 'Options',
38 onClick(){
39 const options = ['name','extension','size','time']
40 const close = newDialog({ Content })
src/index.ts
+3 -3
@@ -4,7 +4,7 @@ import bodyParser from 'koa-bodyparser'
4 import { apiMiddleware } from './apis'
5 import { serveFrontend } from './serveFrontend'
6 import { API_URI, DEV, FRONTEND_URI} from './const'
7 -import { serveFile } from './serveFile'
7 +import { serveFileNode } from './serveFile'
8 import { vfs } from './vfs'
9 import { isDirectory } from './misc'
10 import proxy from 'koa-better-http-proxy'
@@ -72,14 +72,14 @@ app.use(async (ctx, next) => {
72 if (node.default) {
73 const def = await vfs.urlToNode(decoded + node.default, ctx)
74 if (def)
75 - return serveFile(def)(ctx, next)
75 + return serveFileNode(def)(ctx, next)
76 }
77 ctx.set({ server:'HFS '+BUILD_TIMESTAMP })
78 return await serveFrontend(ctx, next)
79 }
80 if (source)
81 return source.includes('//') ? mount(path,proxy(source,{}))(ctx,next)
82 - : serveFile(node)(ctx,next)
82 + : serveFileNode(node)(ctx,next)
83 await next()
84 })
85
src/serveFile.ts
+16 -8
@@ -9,16 +9,20 @@ import mm from 'micromatch'
9 import _ from 'lodash'
10 import path from 'path'
11
12 -export function serveFile(node: VfsNode) : Koa.Middleware {
12 +export function serveFileNode(node: VfsNode) : Koa.Middleware {
13 + const { source, mime } = node
14 + return serveFile(source||'', mime)
15 +}
16 +
17 +export function serveFile(source:string, mime?:string) : Koa.Middleware {
18 return async (ctx) => {
14 - let { source, mime } = node
19 if (!source)
20 return
21 const { range } = ctx.request.header
22 ctx.set('Accept-Ranges', 'bytes')
23 const mimeCfg = getConfig('mime')
20 - const fn = path.basename(source!)
21 - mime = mime || _.find(mimeCfg, (v,k) => mm.isMatch(fn, k))
24 + const fn = path.basename(source)
25 + mime = mime ?? _.find(mimeCfg, (v,k) => mm.isMatch(fn, k))
26 if (mime === MIME_AUTO)
27 mime = mimetypes.lookup(source) || ''
28 if (mime)
@@ -30,6 +34,11 @@ export function serveFile(node: VfsNode) : Koa.Middleware {
34 }
35 if (ctx.method !== 'GET')
36 return ctx.status = METHOD_NOT_ALLOWED
37 + const stats = await fs.stat(source)
38 + ctx.set('Last-Modified', stats.mtime.toUTCString())
39 + ctx.status = 200
40 + if (ctx.fresh)
41 + return ctx.status = 304
42 if (!range)
43 return ctx.body = createReadStream(source)
44 const ranges = range.split('=')[1]
@@ -38,18 +47,17 @@ export function serveFile(node: VfsNode) : Koa.Middleware {
47 let bytes = ranges?.split('-')
48 if (!bytes?.length)
49 return ctx.throw(400, 'bad range')
41 - const stat = await fs.stat(source)
42 - const max = stat.size - 1
50 + const max = stats.size - 1
51 let start = Number(bytes[0])
52 let end = Number(bytes[1]) || max
53 if (end > max || start > max) {
54 ctx.status = 416
47 - ctx.set('Content-Range', `bytes ${stat.size}`)
55 + ctx.set('Content-Range', `bytes ${stats.size}`)
56 ctx.body = 'Requested Range Not Satisfiable'
57 return
58 }
59 ctx.status = 206
52 - ctx.set('Content-Range', `bytes ${start}-${end}/${stat.size}`)
60 + ctx.set('Content-Range', `bytes ${start}-${end}/${stats.size}`)
61 ctx.body = createReadStream(source, { start, end })
62 }
63 }
src/serveFrontend.ts
+7 -6
@@ -1,8 +1,8 @@
1 import proxy from 'koa-better-http-proxy'
2 import Koa from 'koa'
3 -import mime from 'mime-types'
4 -import { createReadStream, readFile } from 'fs'
3 +import { readFile } from 'fs'
4 import { DEV, FRONTEND_URI, METHOD_NOT_ALLOWED, NO_CONTENT } from './const'
5 +import { serveFile } from './serveFile'
6
7 export const serveFrontend = DEV ? serveProxyFrontend() : serveStaticFrontend()
8
@@ -34,12 +34,13 @@ function serveStaticFrontend() : Koa.Middleware {
34 ctx.type = 'html'
35 } else {
36 const fullPath = BASE + path.slice(1)
37 - if (path.includes('static/js')) // webpack
37 + if (path.includes('static/js')) {// webpack
38 ctx.body = String(await filePromise(fullPath))
39 - .replace(/(return")(static\/)/g, '$1'+FRONTEND_URI.substring(1)+'$2')
39 + .replace(/(return")(static\/)/g, '$1' + FRONTEND_URI.substring(1) + '$2')
40 + ctx.type = 'js'
41 + }
42 else
41 - ctx.body = createReadStream(fullPath)
42 - ctx.type = mime.lookup(path) || 'application/octet-stream'
43 + return serveFile(fullPath, 'auto')(ctx, next)
44 }
45 await next()
46 }
todo.md
-1
@@ -1,5 +1,4 @@
1 # To do
2 -- fix: icons are not being cached
2 - fix: plugins folder is not found
3 - anti-csrf
4 - upload