]> Sergey Matveev's repositories - godlighty.git/blob - rc/mime.go
Excess import
[godlighty.git] / rc / mime.go
1 package rc
2
3 import (
4         "embed"
5         "path"
6         "strings"
7
8         "go.stargrave.org/godlighty"
9 )
10
11 const MIMEDir = "mime"
12
13 var (
14         //go:embed mime
15         mimes embed.FS
16 )
17
18 type MIMEEntry struct {
19         ext          string
20         mediaType    string
21         compressible bool
22 }
23
24 func parseMIME(p string) []MIMEEntry {
25         tsv, err := mimes.ReadFile(path.Join(MIMEDir, p))
26         if err != nil {
27                 panic(err)
28         }
29         entries := make([]MIMEEntry, 0)
30         for _, line := range strings.Split(string(tsv), "\n") {
31                 if len(line) == 0 || line[0] == '#' {
32                         continue
33                 }
34                 cols := strings.Split(line, "\t")
35                 entry := MIMEEntry{ext: cols[0], mediaType: cols[1]}
36                 if len(cols) > 2 && cols[2] == "c" {
37                         entry.compressible = true
38                 }
39                 entries = append(entries, entry)
40         }
41         return entries
42 }
43
44 func init() {
45         entries, err := mimes.ReadDir(MIMEDir)
46         if err != nil {
47                 panic(err)
48         }
49         for _, entry := range entries {
50                 bundle := entry.Name()
51                 for _, e := range parseMIME(bundle) {
52                         godlighty.ContentTypes[e.ext] = e.mediaType
53                         if e.compressible {
54                                 godlighty.CompressibleContentTypes[e.mediaType] = struct{}{}
55                         }
56                 }
57         }
58 }