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