Black Lives Matter. Support the Equal Justice Initiative.

Source file src/mime/type_unix.go

Documentation: mime

     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  	"/etc/httpd/conf/mime.types",
    24  }
    25  
    26  func loadMimeFile(filename string) {
    27  	f, err := os.Open(filename)
    28  	if err != nil {
    29  		return
    30  	}
    31  	defer f.Close()
    32  
    33  	scanner := bufio.NewScanner(f)
    34  	for scanner.Scan() {
    35  		fields := strings.Fields(scanner.Text())
    36  		if len(fields) <= 1 || fields[0][0] == '#' {
    37  			continue
    38  		}
    39  		mimeType := fields[0]
    40  		for _, ext := range fields[1:] {
    41  			if ext[0] == '#' {
    42  				break
    43  			}
    44  			setExtensionType("."+ext, mimeType)
    45  		}
    46  	}
    47  	if err := scanner.Err(); err != nil {
    48  		panic(err)
    49  	}
    50  }
    51  
    52  func initMimeUnix() {
    53  	for _, filename := range typeFiles {
    54  		loadMimeFile(filename)
    55  	}
    56  }
    57  
    58  func initMimeForTests() map[string]string {
    59  	typeFiles = []string{"testdata/test.types"}
    60  	return map[string]string{
    61  		".T1":  "application/test",
    62  		".t2":  "text/test; charset=utf-8",
    63  		".png": "image/png",
    64  	}
    65  }
    66  

View as plain text