2011-12-13 00:40:51 +01: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.
|
|
|
|
|
|
|
|
package mime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
func initMime() {
|
|
|
|
var root syscall.Handle
|
|
|
|
if syscall.RegOpenKeyEx(syscall.HKEY_CLASSES_ROOT, syscall.StringToUTF16Ptr(`\`),
|
2012-01-12 02:31:45 +01:00
|
|
|
0, syscall.KEY_READ, &root) != nil {
|
2011-12-13 00:40:51 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer syscall.RegCloseKey(root)
|
|
|
|
var count uint32
|
2012-01-12 02:31:45 +01:00
|
|
|
if syscall.RegQueryInfoKey(root, nil, nil, nil, &count, nil, nil, nil, nil, nil, nil, nil) != nil {
|
2011-12-13 00:40:51 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var buf [1 << 10]uint16
|
|
|
|
for i := uint32(0); i < count; i++ {
|
|
|
|
n := uint32(len(buf))
|
2012-01-12 02:31:45 +01:00
|
|
|
if syscall.RegEnumKeyEx(root, i, &buf[0], &n, nil, nil, nil, nil) != nil {
|
2011-12-13 00:40:51 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
ext := syscall.UTF16ToString(buf[:])
|
|
|
|
if len(ext) < 2 || ext[0] != '.' { // looking for extensions only
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var h syscall.Handle
|
|
|
|
if syscall.RegOpenKeyEx(
|
|
|
|
syscall.HKEY_CLASSES_ROOT, syscall.StringToUTF16Ptr(`\`+ext),
|
2012-01-12 02:31:45 +01:00
|
|
|
0, syscall.KEY_READ, &h) != nil {
|
2011-12-13 00:40:51 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
var typ uint32
|
|
|
|
n = uint32(len(buf) * 2) // api expects array of bytes, not uint16
|
|
|
|
if syscall.RegQueryValueEx(
|
|
|
|
h, syscall.StringToUTF16Ptr("Content Type"),
|
2012-01-12 02:31:45 +01:00
|
|
|
nil, &typ, (*byte)(unsafe.Pointer(&buf[0])), &n) != nil {
|
2011-12-13 00:40:51 +01:00
|
|
|
syscall.RegCloseKey(h)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
syscall.RegCloseKey(h)
|
|
|
|
if typ != syscall.REG_SZ { // null terminated strings only
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mimeType := syscall.UTF16ToString(buf[:])
|
|
|
|
setExtensionType(ext, mimeType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initMimeForTests() map[string]string {
|
|
|
|
return map[string]string{
|
|
|
|
".bmp": "image/bmp",
|
|
|
|
".png": "image/png",
|
|
|
|
}
|
|
|
|
}
|