github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/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 func init() { 14 osInitMime = initMimePlan9 15 } 16 17 func initMimePlan9() { 18 for _, filename := range typeFiles { 19 loadMimeFile(filename) 20 } 21 } 22 23 var typeFiles = []string{ 24 "/sys/lib/mimetype", 25 } 26 27 func initMimeForTests() map[string]string { 28 typeFiles = []string{"testdata/test.types.plan9"} 29 return map[string]string{ 30 ".t1": "application/test", 31 ".t2": "text/test; charset=utf-8", 32 ".pNg": "image/png", 33 } 34 } 35 36 func loadMimeFile(filename string) { 37 f, err := os.Open(filename) 38 if err != nil { 39 return 40 } 41 defer f.Close() 42 43 scanner := bufio.NewScanner(f) 44 for scanner.Scan() { 45 fields := strings.Fields(scanner.Text()) 46 if len(fields) <= 2 || fields[0][0] != '.' { 47 continue 48 } 49 if fields[1] == "-" || fields[2] == "-" { 50 continue 51 } 52 setExtensionType(fields[0], fields[1]+"/"+fields[2]) 53 } 54 if err := scanner.Err(); err != nil { 55 panic(err) 56 } 57 }