github.com/c12o16h1/go/src@v0.0.0-20200114212001-5a151c0f00ed/mime/type_unix.go (about)

     1  // Copyright 2010 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  // +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris
     6  
     7  package mime
     8  
     9  import (
    10  	"bufio"
    11  	"os"
    12  	"strings"
    13  )
    14  
    15  func init() {
    16  	osInitMime = initMimeUnix
    17  }
    18  
    19  var typeFiles = []string{
    20  	"/etc/mime.types",
    21  	"/etc/apache2/mime.types",
    22  	"/etc/apache/mime.types",
    23  }
    24  
    25  func loadMimeFile(filename string) {
    26  	f, err := os.Open(filename)
    27  	if err != nil {
    28  		return
    29  	}
    30  	defer f.Close()
    31  
    32  	scanner := bufio.NewScanner(f)
    33  	for scanner.Scan() {
    34  		fields := strings.Fields(scanner.Text())
    35  		if len(fields) <= 1 || fields[0][0] == '#' {
    36  			continue
    37  		}
    38  		mimeType := fields[0]
    39  		for _, ext := range fields[1:] {
    40  			if ext[0] == '#' {
    41  				break
    42  			}
    43  			setExtensionType("."+ext, mimeType)
    44  		}
    45  	}
    46  	if err := scanner.Err(); err != nil {
    47  		panic(err)
    48  	}
    49  }
    50  
    51  func initMimeUnix() {
    52  	for _, filename := range typeFiles {
    53  		loadMimeFile(filename)
    54  	}
    55  }
    56  
    57  func initMimeForTests() map[string]string {
    58  	typeFiles = []string{"testdata/test.types"}
    59  	return map[string]string{
    60  		".T1":  "application/test",
    61  		".t2":  "text/test; charset=utf-8",
    62  		".png": "image/png",
    63  	}
    64  }