]> Sergey Matveev's repositories - godlighty.git/blob - rc/mime.go
Initial commit
[godlighty.git] / rc / mime.go
1 package rc
2
3 import (
4         "embed"
5         _ "embed"
6         "path"
7         "strings"
8
9         "go.stargrave.org/godlighty"
10 )
11
12 const MIMEDir = "mime"
13
14 var (
15         //go:embed mime
16         mimes embed.FS
17
18         CompressibleMIMEBundles = map[string]struct{}{
19                 "mail":     struct{}{},
20                 "playlist": struct{}{},
21                 "subtitle": struct{}{},
22                 "text":     struct{}{},
23                 "web":      struct{}{},
24                 "xml":      struct{}{},
25         }
26         CompressibleMIMEExts = []string{
27                 ".dvi",
28                 ".eps",
29                 ".fb2",
30                 ".meta4",
31                 ".metalink",
32                 ".ps",
33         }
34 )
35
36 func parseMIME(p string) map[string]string {
37         tsv, err := mimes.ReadFile(path.Join(MIMEDir, p))
38         if err != nil {
39                 panic(err)
40         }
41         m := make(map[string]string)
42         for _, line := range strings.Split(string(tsv), "\n") {
43                 if len(line) == 0 || line[0] == '#' {
44                         continue
45                 }
46                 cols := strings.SplitN(line, "\t", 2)
47                 m[cols[0]] = cols[1]
48         }
49         return m
50 }
51
52 func init() {
53         entries, err := mimes.ReadDir(MIMEDir)
54         if err != nil {
55                 panic(err)
56         }
57         for _, entry := range entries {
58                 bundle := entry.Name()
59                 _, allCompressible := CompressibleMIMEBundles[bundle]
60                 for ext, mediaType := range parseMIME(bundle) {
61                         godlighty.ContentTypes[ext] = mediaType
62                         if allCompressible {
63                                 godlighty.CompressibleContentTypes[mediaType] = struct{}{}
64                         }
65                 }
66         }
67         CompressibleMIMEBundles = nil
68         for _, ext := range CompressibleMIMEExts {
69                 mediaType := godlighty.ContentTypes[ext]
70                 godlighty.CompressibleContentTypes[mediaType] = struct{}{}
71         }
72         CompressibleMIMEExts = nil
73 }