Anthony Martin | 5567592 | 2013-06-04 17:30:45 -0700 | [diff] [blame] | 1 | // 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 | |
| 5 | package mime |
| 6 | |
| 7 | import ( |
| 8 | "bufio" |
| 9 | "os" |
| 10 | "strings" |
| 11 | ) |
| 12 | |
| 13 | var typeFiles = []string{ |
| 14 | "/sys/lib/mimetypes", |
| 15 | } |
| 16 | |
| 17 | func 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 | |
| 40 | func initMime() { |
| 41 | for _, filename := range typeFiles { |
| 42 | loadMimeFile(filename) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func 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. Allen | af12dc5 | 2014-08-28 08:22:54 -0700 | [diff] [blame] | 51 | ".pNg": "image/png", |
Anthony Martin | 5567592 | 2013-06-04 17:30:45 -0700 | [diff] [blame] | 52 | } |
| 53 | } |