github.com/ccccaoqing/test@v0.0.0-20220510085219-3985d23445c0/src/mime/type_plan9.go (about) 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", 51 ".pNg": "image/png", 52 } 53 }