blob: 8cbf6777f19258fc719b7c3a523ba31229d955e6 [file] [log] [blame]
Anthony Martin55675922013-06-04 17:30:45 -07001// Copyright 2013 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package mime
6
7import (
8 "bufio"
9 "os"
10 "strings"
11)
12
13var typeFiles = []string{
14 "/sys/lib/mimetypes",
15}
16
17func loadMimeFile(filename string) {
18 f, err := os.Open(filename)
19 if err != nil {
20 return
21 }
22 defer f.Close()
23
24 scanner := bufio.NewScanner(f)
25 for scanner.Scan() {
26 fields := strings.Fields(scanner.Text())
27 if len(fields) <= 2 || fields[0][0] != '.' {
28 continue
29 }
30 if fields[1] == "-" || fields[2] == "-" {
31 continue
32 }
33 setExtensionType(fields[0], fields[1]+"/"+fields[2])
34 }
35 if err := scanner.Err(); err != nil {
36 panic(err)
37 }
38}
39
40func initMime() {
41 for _, filename := range typeFiles {
42 loadMimeFile(filename)
43 }
44}
45
46func initMimeForTests() map[string]string {
47 typeFiles = []string{"testdata/test.types.plan9"}
48 return map[string]string{
49 ".t1": "application/test",
50 ".t2": "text/test; charset=utf-8",
Jeff R. Allenaf12dc52014-08-28 08:22:54 -070051 ".pNg": "image/png",
Anthony Martin55675922013-06-04 17:30:45 -070052 }
53}