frontend: show size and timestamp
Massimo Melina committed
Dec 12, 2021 at 18:18 UTC
02e84b6efbd35c31da615db06062e92012bf78a1
10 files changed
+126
-98
frontend/public/index.html
+1
-23
@@ -10,34 +10,12 @@
10
content="Web site created using create-react-app"
11
/>
12
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13
- <!--
14
- manifest.json provides metadata used when your web app is installed on a
15
- user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16
- -->
13
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18
- <!--
19
- Notice the use of %PUBLIC_URL% in the tags above.
20
- It will be replaced with the URL of the `public` folder during the build.
21
- Only files inside the `public` folder can be referenced from the HTML.
22
-
23
- Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24
- work correctly both with client-side routing and a non-root public URL.
25
- Learn how to configure a non-root public URL by running `npm run build`.
26
- -->
14
+ <link href="https://fonts.googleapis.com/icon?family=Material+Icons+Outlined" rel="stylesheet" />
15
<title>File Server</title>
16
</head>
17
<body>
18
<noscript>You need to enable JavaScript to run this app.</noscript>
19
<div id="root"></div>
32
- <!--
33
- This HTML file is a template.
34
- If you open it directly in the browser, you will see an empty page.
35
-
36
- You can add webfonts, meta tags, or analytics to this file.
37
- The build step will place the bundled scripts into the <body> tag.
38
-
39
- To begin the development, run `npm start` or `yarn start`.
40
- To create a production bundle, use `npm run build` or `yarn build`.
41
- -->
20
</body>
21
</html>
frontend/public/logo512.png
Binary files a/frontend/public/logo512.png and /dev/null differ
frontend/src/App.css
+30
-30
@@ -1,38 +1,38 @@
1
-.App {
2
- text-align: center;
1
+ul.dir { padding:0 }
2
+ul.dir li {
3
+ display: block;
4
+ list-style-type: none;
5
+ margin-bottom: 0.3em;
6
+ padding: 0.3em;
7
+ border-top: 1px solid #ddd;
8
}
4
-
5
-.App-logo {
6
- height: 40vmin;
7
- pointer-events: none;
9
+ul.dir a {
10
+ float: left;
11
+ word-break: break-word;
12
}
9
-
10
-@media (prefers-reduced-motion: no-preference) {
11
- .App-logo {
12
- animation: App-logo-spin infinite 20s linear;
13
- }
13
+ul.dir .icon {
14
+ margin-left:.2em;
15
}
15
-
16
-.App-header {
17
- background-color: #282c34;
18
- min-height: 100vh;
19
- display: flex;
20
- flex-direction: column;
21
- align-items: center;
22
- justify-content: center;
23
- font-size: calc(10px + 2vmin);
24
- color: white;
16
+ul.dir .item-size {
17
+ margin-right: 1em;
18
+ display:inline-block;
19
}
20
27
-.App-link {
28
- color: #61dafb;
21
+.icon {
22
+ vertical-align: text-bottom;
23
+ margin: 0 0.2em;
24
}
25
31
-@keyframes App-logo-spin {
32
- from {
33
- transform: rotate(0deg);
34
- }
35
- to {
36
- transform: rotate(360deg);
37
- }
26
+ul.dir .entry-props {
27
+ float: right;
28
+ font-size: 90%;
29
+ margin-left: 12px;
30
+ margin-top: 0.2em
31
}
32
+
33
+a {
34
+ text-decoration: none;
35
+ color: #357;
36
+ border: 1px solid transparent;
37
+ padding: 0 0.1em;
38
+}
\ No newline at end of file
frontend/src/App.tsx
+47
-8
@@ -24,20 +24,59 @@ function Head() {
24
return null
25
}
26
27
-interface FileObj { n:string, s?:number }
27
+interface DirEntry { n:string, s?:number, m?:string, c?:string }
28
29
function FilesList() {
30
- const res = useApi('file_list', { path: useLocation().pathname })?.list
30
+ const path = useLocation().pathname
31
+ let res = useApi('file_list', { path })?.list
32
if (!res)
33
return h(Loading)
33
- return h('ul', {}, res.map(({ n }:FileObj) => {
34
- const isDir = n.endsWith('/')
35
- return h('li', { key: n },
36
- isDir ? h(Link, { to: n }, n)
37
- : h('a', { href: n }, n))
38
- }))
34
+ if (path > '/')
35
+ res = [{ n: '../' }, ...res]
36
+ return h('ul', { className:'dir' }, res.map((entry:DirEntry) => h(File, { key: entry.n, ...entry })))
37
+}
38
+
39
+function File({ n, m, c, s }: DirEntry) {
40
+ const isDir = n.endsWith('/')
41
+ const t = m||c ||null
42
+ return h('li', {},
43
+ isDir ? h(Link, { to: n }, h(Icon, { name:'folder' }), n)
44
+ : h('a', { href: n }, h(Icon, { name:'description' }), n),
45
+ h('div', { className:'entry-props' },
46
+ s !== undefined && h('span', { className:'entry-size' }, formatBytes(s)),
47
+ t && h('span', { className:'entry-ts' }, new Date(t).toLocaleString()),
48
+ ),
49
+ h('div', { style:{ clear:'both' } })
50
+ )
51
+}
52
+
53
+function Icon({ name }: { name:string }) {
54
+ return h('span',{
55
+ className: 'material-icons-outlined icon',
56
+ }, name)
57
}
58
59
function Loading() {
60
return 'loading' as React.ReactNode as React.ReactElement
61
}
62
+
63
+function formatBytes(n: number, post: string = 'B') {
64
+ if (isNaN(Number(n)))
65
+ return ''
66
+ let x = ['', 'K', 'M', 'G', 'T']
67
+ let prevMul = 1
68
+ let mul = 1024
69
+ let i = 0
70
+ while (i < x.length && n > mul) {
71
+ prevMul = mul
72
+ mul *= 1024
73
+ ++i
74
+ }
75
+ n /= prevMul
76
+ return round(n, 1) + ' ' + (x[i]||'') + post
77
+} // formatBytes
78
+
79
+function round(v: number, decimals: number = 0) {
80
+ decimals = Math.pow(10, decimals)
81
+ return Math.round(v * decimals) / decimals
82
+} // round
frontend/src/api.ts
+1
@@ -17,6 +17,7 @@ export function apiCall(cmd: string, params?: object) : Promise<any> {
17
export function useApi(cmd: string, params?: object) : any {
18
const [x, setX] = useState()
19
useEffect(()=>{
20
+ setX(undefined)
21
apiCall(cmd, params).then(setX, setX)
22
}, [cmd, JSON.stringify(params)]) //eslint-disable-line
23
return x
frontend/src/logo.svg
deleted
-1
@@ -1 +0,0 @@
1
-<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 841.9 595.3"><g fill="#61DAFB"><path d="M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3zm-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9zm-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9zm32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1zM421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32zm-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24 4.7 8 9.5 15.8 14.4 23.4zM420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32zm-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9zm-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6 0-15.7 22.9-35.6 58.3-50.6 8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2zM310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7zm237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1zm38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6zM320.8 78.4z"/><circle cx="420.9" cy="296.5" r="45.7"/><path d="M520.5 78.1z"/></g></svg>
\ No newline at end of file
package.json
+10
@@ -1,4 +1,14 @@
1
{
2
+ "name": "hfs4",
3
+ "version": "0.0.0",
4
+ "description": "HTTP File Server",
5
+ "keywords": [
6
+ "file server",
7
+ "http server"
8
+ ],
9
+ "homepage": "https://rejetto.com/hfs",
10
+ "license": "GPL-3.0",
11
+ "author": "Massimo Melina <a@rejetto.com> (https://rejetto.com)",
12
"scripts": {
13
"watch-server": "nodemon --ignore tests/ --watch src -e ts,tsx --exec ts-node src/index.ts",
14
"start": "node dist",
src/apis.ts
+20
-2
@@ -1,6 +1,24 @@
1
import glob from 'fast-glob'
2
+import Koa from 'koa'
3
3
-export default {
4
+type ApiHandler = (params?:any, ctx?:any) => any
5
+type ApiHandlers = Record<string, ApiHandler>
6
+
7
+export function apiMw(apis: ApiHandlers) : Koa.Middleware {
8
+ return async (ctx, next) => {
9
+ const params = ctx.request.body
10
+ console.log('API', ctx.method, ctx.path, params)
11
+ // @ts-ignore
12
+ ctx.assert(ctx.path in apis, 404, 'invalid api')
13
+ const cb = (apis as any)[ctx.path]
14
+ const res = await cb(params, ctx)
15
+ if (res)
16
+ ctx.body = res
17
+ await next()
18
+ }
19
+}
20
+
21
+export const frontEndApis: ApiHandlers = {
22
async file_list(params:any) {
23
const res = await glob('.' + (params.path || '/') + '*', {
24
stats: true,
@@ -20,4 +38,4 @@ export default {
38
})
39
return { list }
40
}
23
-}
\ No newline at end of file
41
+}
src/frontend.ts
+1
-1
@@ -38,5 +38,5 @@ function filePromise(path: string) : Promise<Buffer> {
38
}
39
40
function replaceFrontEndRes(body: string) {
41
- return body.replace(/((?:src|href) *= *['"])\/?/g, '$1'+FRONTEND_URI)
41
+ return body.replace(/((?:src|href) *= *['"])\/?(?![a-z]+:\/\/)/g, '$1'+FRONTEND_URI)
42
}
src/index.ts
+16
-33
@@ -2,7 +2,7 @@ import Koa from 'koa'
2
import serve from 'koa-static'
3
import mount from 'koa-mount'
4
import bodyParser from 'koa-bodyparser'
5
-import apis from './apis'
5
+import { apiMw, frontEndApis } from './apis'
6
import { serveFrontend } from './frontend'
7
import { API_URI, DEV, FRONTEND_URI } from './const'
8
@@ -10,46 +10,29 @@ const PORT = 80
10
11
const srv = new Koa()
12
srv.use(async (ctx, next) => {
13
- console.debug(ctx.request.method, ctx.url) // log all requests
13
+ // log all requests
14
+ console.debug(ctx.request.method, ctx.url)
15
+ // prevent cross-dir
16
// @ts-ignore
15
- ctx.assert(!ctx.originalUrl.includes('..'), 400, 'cross-dir') // prevent cross-dir
17
+ ctx.assert(!ctx.originalUrl.includes('..'), 400, 'cross-dir')
18
await next()
19
})
20
19
-srv.use(mount(API_URI, new Koa().use(bodyParser()).use(api(apis))))
21
+// serve apis
22
+srv.use(mount(API_URI, new Koa().use(bodyParser()).use(apiMw(frontEndApis))))
23
21
-const serveFiles = serve('.')
24
+// serve shared files and front-end files
25
+const serveFiles = serve('.', { hidden:true })
26
const serveFrontendPrefixed = mount(FRONTEND_URI.slice(0,-1), serveFrontend)
23
-
27
srv.use(async (ctx, next) => {
25
- if (ctx.method === 'GET') {
26
- if (ctx.url.endsWith('/'))
27
- await serveFrontend(ctx,next)
28
- else if (ctx.url.startsWith(FRONTEND_URI))
29
- await serveFrontendPrefixed(ctx,next)
30
- else
31
- await serveFiles(ctx,next)
32
- }
33
- else
34
- await next()
28
+ if (ctx.method !== 'GET')
29
+ return await next()
30
+ if (ctx.url.endsWith('/'))
31
+ return await serveFrontend(ctx,next)
32
+ if (ctx.url.startsWith(FRONTEND_URI))
33
+ return await serveFrontendPrefixed(ctx,next)
34
+ await serveFiles(ctx,next)
35
})
36
37
srv.on('error', err => console.error('server error', err))
38
srv.listen(PORT, ()=> console.log('running on port', PORT, DEV, new Date().toLocaleString()))
39
-
40
-type ApiHandler = (params?:any, ctx?:any) => any
41
-type ApiHandlers = Record<string, ApiHandler>
42
-
43
-function api(apis: ApiHandlers) : Koa.Middleware {
44
- return async (ctx, next) => {
45
- const params = ctx.request.body
46
- console.log('API', ctx.method, ctx.path, params)
47
- // @ts-ignore
48
- ctx.assert(ctx.path in apis, 404, 'invalid api')
49
- const cb = (apis as any)[ctx.path]
50
- const res = await cb(params, ctx)
51
- if (res)
52
- ctx.body = res
53
- await next()
54
- }
55
-}