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