]> Sergey Matveev's repositories - godlighty.git/blobdiff - rc/mime.go
Built-in MIMEs compression capability
[godlighty.git] / rc / mime.go
index 0b8d532d8cbfdfa0b9c6be361c5db25904fba605..9aea81e6a9aea56d5b3d2e6ba9f098218bb5b7f9 100644 (file)
@@ -14,39 +14,32 @@ const MIMEDir = "mime"
 var (
        //go:embed mime
        mimes embed.FS
-
-       CompressibleMIMEBundles = map[string]struct{}{
-               "mail":     struct{}{},
-               "playlist": struct{}{},
-               "subtitle": struct{}{},
-               "text":     struct{}{},
-               "web":      struct{}{},
-               "xml":      struct{}{},
-       }
-       CompressibleMIMEExts = []string{
-               ".dvi",
-               ".eps",
-               ".fb2",
-               ".meta4",
-               ".metalink",
-               ".ps",
-       }
 )
 
-func parseMIME(p string) map[string]string {
+type MIMEEntry struct {
+       ext          string
+       mediaType    string
+       compressible bool
+}
+
+func parseMIME(p string) []MIMEEntry {
        tsv, err := mimes.ReadFile(path.Join(MIMEDir, p))
        if err != nil {
                panic(err)
        }
-       m := make(map[string]string)
+       entries := make([]MIMEEntry, 0)
        for _, line := range strings.Split(string(tsv), "\n") {
                if len(line) == 0 || line[0] == '#' {
                        continue
                }
-               cols := strings.SplitN(line, "\t", 2)
-               m[cols[0]] = cols[1]
+               cols := strings.Split(line, "\t")
+               entry := MIMEEntry{ext: cols[0], mediaType: cols[1]}
+               if len(cols) > 2 && cols[2] == "c" {
+                       entry.compressible = true
+               }
+               entries = append(entries, entry)
        }
-       return m
+       return entries
 }
 
 func init() {
@@ -56,18 +49,11 @@ func init() {
        }
        for _, entry := range entries {
                bundle := entry.Name()
-               _, allCompressible := CompressibleMIMEBundles[bundle]
-               for ext, mediaType := range parseMIME(bundle) {
-                       godlighty.ContentTypes[ext] = mediaType
-                       if allCompressible {
-                               godlighty.CompressibleContentTypes[mediaType] = struct{}{}
+               for _, e := range parseMIME(bundle) {
+                       godlighty.ContentTypes[e.ext] = e.mediaType
+                       if e.compressible {
+                               godlighty.CompressibleContentTypes[e.mediaType] = struct{}{}
                        }
                }
        }
-       CompressibleMIMEBundles = nil
-       for _, ext := range CompressibleMIMEExts {
-               mediaType := godlighty.ContentTypes[ext]
-               godlighty.CompressibleContentTypes[mediaType] = struct{}{}
-       }
-       CompressibleMIMEExts = nil
 }