|
|
@ -19,13 +19,18 @@ |
|
|
|
package contentdb |
|
|
|
package contentdb |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
|
|
|
|
"sync" |
|
|
|
|
|
|
|
|
|
|
|
"encoding/json" |
|
|
|
"encoding/json" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
var ( |
|
|
|
|
|
|
|
// Internal lock.
|
|
|
|
|
|
|
|
mutex = &sync.Mutex{} |
|
|
|
|
|
|
|
|
|
|
|
// Make note of initialization.
|
|
|
|
// Make note of initialization.
|
|
|
|
isInitialized = false |
|
|
|
isInitialized = false |
|
|
|
|
|
|
|
|
|
|
@ -78,6 +83,10 @@ func loadDB() error { |
|
|
|
|
|
|
|
|
|
|
|
// Init initializes contentdb for lookups. JSON structure is parsed into a simple map of extension and content-type.
|
|
|
|
// Init initializes contentdb for lookups. JSON structure is parsed into a simple map of extension and content-type.
|
|
|
|
func Init() error { |
|
|
|
func Init() error { |
|
|
|
|
|
|
|
mutex.Lock() |
|
|
|
|
|
|
|
defer mutex.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !isInitialized { |
|
|
|
var e error |
|
|
|
var e error |
|
|
|
extDB = make(map[string]string) |
|
|
|
extDB = make(map[string]string) |
|
|
|
|
|
|
|
|
|
|
@ -87,11 +96,13 @@ func Init() error { |
|
|
|
isInitialized = true |
|
|
|
isInitialized = true |
|
|
|
return e |
|
|
|
return e |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Lookup returns matching content-type for known types of file extensions.
|
|
|
|
// Lookup returns matching content-type for known types of file extensions.
|
|
|
|
func Lookup(extension string) (contentType string, e error) { |
|
|
|
func Lookup(extension string) (contentType string, e error) { |
|
|
|
if !isInitialized { |
|
|
|
if !isInitialized { |
|
|
|
e = Init() |
|
|
|
return "", errors.New("contentdb is not initialized.") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return extDB[extension], e |
|
|
|
return extDB[extension], e |
|
|
@ -99,11 +110,9 @@ func Lookup(extension string) (contentType string, e error) { |
|
|
|
|
|
|
|
|
|
|
|
// MustLookup returns matching content-type for known types of file extensions. In case of error, it panics.
|
|
|
|
// MustLookup returns matching content-type for known types of file extensions. In case of error, it panics.
|
|
|
|
func MustLookup(extension string) (contentType string) { |
|
|
|
func MustLookup(extension string) (contentType string) { |
|
|
|
if !isInitialized { |
|
|
|
var e error |
|
|
|
if e := Init(); e != nil { |
|
|
|
if contentType, e = Lookup(extension); e != nil { |
|
|
|
panic(fmt.Sprintf("Error loading contentdb: %s\n", e)) |
|
|
|
panic(fmt.Sprintf("Lookup failed: %s\n", e)) |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return contentType |
|
|
|
return extDB[extension] |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|