config.mime

Massimo Melina committed Dec 27, 2021 at 16:42 UTC 6e17c041a1ad479c9634e4132250a9f1cb9a7226
4 files changed +20 -1
README.md
+11
@@ -22,6 +22,17 @@ You won't find all previous features here (yet), but still we got:
22
23 At the moment there's no administration UI. You must edit configuration files.
24
25 +## Config
26 +
27 +General configuration is read by default from file `config.yaml`.
28 +When not specified, default values will be used.
29 +Supported entries are:
30 +- `port` where to accept http connections. Default is 80.
31 +- `mime` command what mime-type to be returned with some files.
32 + E.g.: `"*.jpg": image/jpeg`
33 + You can specify multiple entries, or separate multiple file masks with a p|pe.
34 + You can use the special value `auto` to attempt automatic detection.
35 +
36 ## Virtual File System (VFS)
37
38 The virtual file system is a tree of nodes.
config.yaml
+2
@@ -1 +1,3 @@
1 port: 80
2 +mime:
3 + "*.jpg|*.png|*.mp3|*.txt": auto
src/serveFile.ts
+7
@@ -4,6 +4,10 @@ import fs from 'fs/promises'
4 import { METHOD_NOT_ALLOWED, NO_CONTENT } from './const'
5 import { MIME_AUTO, VfsNode } from './vfs'
6 import mimetypes from 'mime-types'
7 +import { getConfig } from './config'
8 +import mm from 'micromatch'
9 +import _ from 'lodash'
10 +import path from 'path'
11
12 export function serveFile(node: VfsNode) : Koa.Middleware {
13 return async (ctx) => {
@@ -12,6 +16,9 @@ export function serveFile(node: VfsNode) : Koa.Middleware {
16 return
17 const { range } = ctx.request.header
18 ctx.set('Accept-Ranges', 'bytes')
19 + const mimeCfg = getConfig('mime')
20 + const fn = path.basename(source!)
21 + mime = mime || _.find(mimeCfg, (v,k) => mm.isMatch(fn, k))
22 if (mime === MIME_AUTO)
23 mime = mimetypes.lookup(source) || ''
24 if (mime)
todo.md
-1
@@ -3,7 +3,6 @@
3 - folders before?
4 - upload
5 - log file
6 -- config: mime
6 - node.comment
7 - streamable zip archives (no compression, resumable?)
8 - config: max speed (total/per-ip)