Source file
src/mime/type_unix.go
1
2
3
4
5
6
7 package mime
8
9 import (
10 "bufio"
11 "os"
12 "strings"
13 )
14
15 func init() {
16 osInitMime = initMimeUnix
17 }
18
19
20
21 var mimeGlobs = []string{
22 "/usr/local/share/mime/globs2",
23 "/usr/share/mime/globs2",
24 }
25
26
27 var typeFiles = []string{
28 "/etc/mime.types",
29 "/etc/apache2/mime.types",
30 "/etc/apache/mime.types",
31 "/etc/httpd/conf/mime.types",
32 }
33
34 func loadMimeGlobsFile(filename string) error {
35 f, err := os.Open(filename)
36 if err != nil {
37 return err
38 }
39 defer f.Close()
40
41 scanner := bufio.NewScanner(f)
42 for scanner.Scan() {
43
44 fields := strings.Split(scanner.Text(), ":")
45 if len(fields) < 3 || len(fields[0]) < 1 || len(fields[2]) < 2 {
46 continue
47 } else if fields[0][0] == '#' || fields[2][0] != '*' {
48 continue
49 }
50
51 extension := fields[2][1:]
52 if _, ok := mimeTypes.Load(extension); ok {
53
54
55
56 continue
57 }
58
59 setExtensionType(extension, fields[1])
60 }
61 if err := scanner.Err(); err != nil {
62 panic(err)
63 }
64 return nil
65 }
66
67 func loadMimeFile(filename string) {
68 f, err := os.Open(filename)
69 if err != nil {
70 return
71 }
72 defer f.Close()
73
74 scanner := bufio.NewScanner(f)
75 for scanner.Scan() {
76 fields := strings.Fields(scanner.Text())
77 if len(fields) <= 1 || fields[0][0] == '#' {
78 continue
79 }
80 mimeType := fields[0]
81 for _, ext := range fields[1:] {
82 if ext[0] == '#' {
83 break
84 }
85 setExtensionType("."+ext, mimeType)
86 }
87 }
88 if err := scanner.Err(); err != nil {
89 panic(err)
90 }
91 }
92
93 func initMimeUnix() {
94 for _, filename := range mimeGlobs {
95 if err := loadMimeGlobsFile(filename); err == nil {
96 return
97 }
98 }
99
100
101 for _, filename := range typeFiles {
102 loadMimeFile(filename)
103 }
104 }
105
106 func initMimeForTests() map[string]string {
107 mimeGlobs = []string{""}
108 typeFiles = []string{"testdata/test.types"}
109 return map[string]string{
110 ".T1": "application/test",
111 ".t2": "text/test; charset=utf-8",
112 ".png": "image/png",
113 }
114 }
115
View as plain text