2012-03-27 23:13:14 +00:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
// +build aix darwin dragonfly freebsd linux nacl netbsd openbsd solaris
|
2012-03-27 23:13:14 +00:00
|
|
|
|
|
|
|
package mime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
func init() {
|
|
|
|
osInitMime = initMimeUnix
|
|
|
|
}
|
|
|
|
|
2012-03-27 23:13:14 +00:00
|
|
|
var typeFiles = []string{
|
|
|
|
"/etc/mime.types",
|
|
|
|
"/etc/apache2/mime.types",
|
|
|
|
"/etc/apache/mime.types",
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadMimeFile(filename string) {
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
defer f.Close()
|
2012-03-27 23:13:14 +00:00
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
for scanner.Scan() {
|
|
|
|
fields := strings.Fields(scanner.Text())
|
2012-03-27 23:13:14 +00:00
|
|
|
if len(fields) <= 1 || fields[0][0] == '#' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mimeType := fields[0]
|
|
|
|
for _, ext := range fields[1:] {
|
|
|
|
if ext[0] == '#' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
setExtensionType("."+ext, mimeType)
|
|
|
|
}
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
func initMimeUnix() {
|
2012-03-27 23:13:14 +00:00
|
|
|
for _, filename := range typeFiles {
|
|
|
|
loadMimeFile(filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initMimeForTests() map[string]string {
|
2014-09-21 17:33:12 +00:00
|
|
|
typeFiles = []string{"testdata/test.types"}
|
2012-03-27 23:13:14 +00:00
|
|
|
return map[string]string{
|
2015-08-28 15:33:40 +00:00
|
|
|
".T1": "application/test",
|
2012-03-27 23:13:14 +00:00
|
|
|
".t2": "text/test; charset=utf-8",
|
|
|
|
".png": "image/png",
|
|
|
|
}
|
|
|
|
}
|