parent
3a55d05eff
commit
d8abb36653
@ -1,26 +0,0 @@ |
|||||||
# Generate db.go from db.json downloaded nodejs mime-db project.
|
|
||||||
# NOTE: Autogenerated db.go needs to be vet proofed. \
|
|
||||||
Manually edit json -> JSON for all variable names
|
|
||||||
all: checkdeps download build |
|
||||||
|
|
||||||
# go-bindata generates go source from any data file.
|
|
||||||
checkdeps: |
|
||||||
@go get -u github.com/jteeuwen/go-bindata/...
|
|
||||||
|
|
||||||
# Download db.json from NodeJS's mime-db project. It is under MIT license.
|
|
||||||
download: checkdeps |
|
||||||
@mkdir db
|
|
||||||
@wget -nv https://cdn.rawgit.com/jshttp/mime-db/master/db.json -O db/db.json
|
|
||||||
|
|
||||||
|
|
||||||
# After generating db.go, clean up downloaded db.json.
|
|
||||||
build: download |
|
||||||
@go-bindata -pkg contentdb -o db.go db
|
|
||||||
@rm -fv db/db.json
|
|
||||||
@rm -rfv db
|
|
||||||
|
|
||||||
# Clean auto-generated files and backups.
|
|
||||||
clean: |
|
||||||
@rm -rf db.go
|
|
||||||
@rm -f *~
|
|
||||||
|
|
@ -1,118 +0,0 @@ |
|||||||
/* |
|
||||||
* mime-db: Mime Database, (C) 2015 Minio, Inc. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
|
|
||||||
// Package contentdb is a database of file extension to mime content-type.
|
|
||||||
// Definitions are imported from NodeJS mime-db project under MIT license.
|
|
||||||
package contentdb |
|
||||||
|
|
||||||
import ( |
|
||||||
"errors" |
|
||||||
"fmt" |
|
||||||
"strings" |
|
||||||
"sync" |
|
||||||
|
|
||||||
"encoding/json" |
|
||||||
) |
|
||||||
|
|
||||||
var ( |
|
||||||
// Internal lock.
|
|
||||||
mutex = &sync.Mutex{} |
|
||||||
|
|
||||||
// Make note of initialization.
|
|
||||||
isInitialized = false |
|
||||||
|
|
||||||
// Database of extension:content-type.
|
|
||||||
extDB map[string]string |
|
||||||
) |
|
||||||
|
|
||||||
// Load JSON data from gobindata and parse them into extDB.
|
|
||||||
func loadDB() error { |
|
||||||
// Structure of JSON data from mime-db project.
|
|
||||||
type dbEntry struct { |
|
||||||
Source string `json:"source"` |
|
||||||
Compressible bool `json:"compresible"` |
|
||||||
Extensions []string `json:"extensions"` |
|
||||||
} |
|
||||||
|
|
||||||
// Access embedded "db.json" inside go-bindata.
|
|
||||||
jsonDB, e := Asset("db/db.json") |
|
||||||
if e != nil { |
|
||||||
return e |
|
||||||
} |
|
||||||
|
|
||||||
// Convert db.json into go's typed structure.
|
|
||||||
db := make(map[string]dbEntry) |
|
||||||
if e := json.Unmarshal(jsonDB, &db); e != nil { |
|
||||||
return e |
|
||||||
} |
|
||||||
|
|
||||||
// Generate a new database from mime-db.
|
|
||||||
for key, val := range db { |
|
||||||
if len(val.Extensions) > 0 { |
|
||||||
/* Denormalize - each extension has its own |
|
||||||
unique content-type now. Looks will be fast. */ |
|
||||||
for _, ext := range val.Extensions { |
|
||||||
/* Single extension type may map to |
|
||||||
multiple content-types. In that case, |
|
||||||
simply prefer the longest content-type |
|
||||||
to maintain some level of |
|
||||||
consistency. Only guarantee is, |
|
||||||
whatever content type is assigned, it |
|
||||||
is appropriate and valid type. */ |
|
||||||
if strings.Compare(extDB[ext], key) < 0 { |
|
||||||
extDB[ext] = key |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
return nil |
|
||||||
} |
|
||||||
|
|
||||||
// Init initializes contentdb for lookups. JSON structure is parsed into a simple map of extension and content-type.
|
|
||||||
func Init() error { |
|
||||||
mutex.Lock() |
|
||||||
defer mutex.Unlock() |
|
||||||
|
|
||||||
if !isInitialized { |
|
||||||
var e error |
|
||||||
extDB = make(map[string]string) |
|
||||||
|
|
||||||
if !isInitialized { |
|
||||||
e = loadDB() |
|
||||||
} |
|
||||||
isInitialized = true |
|
||||||
return e |
|
||||||
} |
|
||||||
return nil |
|
||||||
} |
|
||||||
|
|
||||||
// Lookup returns matching content-type for known types of file extensions.
|
|
||||||
func Lookup(extension string) (contentType string, e error) { |
|
||||||
if !isInitialized { |
|
||||||
return "", errors.New("contentdb is not initialized") |
|
||||||
} |
|
||||||
|
|
||||||
return extDB[extension], e |
|
||||||
} |
|
||||||
|
|
||||||
// MustLookup returns matching content-type for known types of file extensions. In case of error, it panics.
|
|
||||||
func MustLookup(extension string) (contentType string) { |
|
||||||
var e error |
|
||||||
if contentType, e = Lookup(extension); e != nil { |
|
||||||
panic(fmt.Sprintf("Lookup failed: %s\n", e)) |
|
||||||
} |
|
||||||
return contentType |
|
||||||
} |
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,17 @@ |
|||||||
|
# Generate db.go from db.json downloaded nodejs mime-db project.
|
||||||
|
# NOTE: Autogenerated db.go needs to be vet proofed. \
|
||||||
|
Manually edit json -> JSON for all variable names
|
||||||
|
all: download build |
||||||
|
|
||||||
|
# Download db.json from NodeJS's mime-db project. It is under MIT license.
|
||||||
|
download: |
||||||
|
@mkdir db
|
||||||
|
@wget -nv -q https://cdn.rawgit.com/jshttp/mime-db/master/db.json -O db/db.json
|
||||||
|
|
||||||
|
|
||||||
|
# After generating db.go, clean up downloaded db.json.
|
||||||
|
build: download |
||||||
|
@go run util/gen-db.go db/db.json > db.go
|
||||||
|
@rm -f db/db.json
|
||||||
|
@rm -rf db
|
||||||
|
@echo Generated \"db.go\".
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,146 @@ |
|||||||
|
/* |
||||||
|
* mimedb: Mime Database, (C) 2015 Minio, Inc. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
// Package mimedb is a database of file extension to mime content-type.
|
||||||
|
// Definitions are imported from NodeJS mime-db project under MIT license.
|
||||||
|
|
||||||
|
package main |
||||||
|
|
||||||
|
import ( |
||||||
|
"encoding/json" |
||||||
|
"fmt" |
||||||
|
"io/ioutil" |
||||||
|
"os" |
||||||
|
"strings" |
||||||
|
"text/template" |
||||||
|
) |
||||||
|
|
||||||
|
const progTempl = `// DO NOT EDIT THIS FILE. IT IS AUTO-GENERATED BY "gen-db.go". //
|
||||||
|
/* |
||||||
|
* mimedb: Mime Database, (C) 2016 Minio, Inc. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
// Package mimedb is a database of file extension to mime content-type.
|
||||||
|
// Definitions are imported from NodeJS mime-db project under MIT license.
|
||||||
|
package mimedb |
||||||
|
|
||||||
|
// Mime is a collection of mime types with extension as key and content-type as value.
|
||||||
|
var DB = map[string]struct { |
||||||
|
ContentType string |
||||||
|
Compressible bool |
||||||
|
}{ |
||||||
|
{{range $extension, $entry := . }} "{{$extension}}": { |
||||||
|
ContentType: "{{$entry.ContentType}}", |
||||||
|
Compressible: {{$entry.Compressible}}, |
||||||
|
}, |
||||||
|
{{end}}} |
||||||
|
` |
||||||
|
|
||||||
|
type mimeEntry struct { |
||||||
|
ContentType string `json:"contentType"` |
||||||
|
Compressible bool `json:"compresible"` |
||||||
|
} |
||||||
|
|
||||||
|
type mimeDB map[string]mimeEntry |
||||||
|
|
||||||
|
var () |
||||||
|
|
||||||
|
// JSON data from gobindata and parse them into extDB.
|
||||||
|
func convertDB(jsonFile string) (mimeDB, error) { |
||||||
|
// Structure of JSON data from mime-db project.
|
||||||
|
type dbEntry struct { |
||||||
|
Source string `json:"source"` |
||||||
|
Compressible bool `json:"compresible"` |
||||||
|
Extensions []string `json:"extensions"` |
||||||
|
} |
||||||
|
|
||||||
|
// Access embedded "db.json" inside go-bindata.
|
||||||
|
jsonDB, err := ioutil.ReadFile(jsonFile) |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
|
||||||
|
// Convert db.json into go's typed structure.
|
||||||
|
db := make(map[string]dbEntry) |
||||||
|
if err := json.Unmarshal(jsonDB, &db); err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
|
||||||
|
mDB := make(mimeDB) |
||||||
|
|
||||||
|
// Generate a new database from mime-db.
|
||||||
|
for key, val := range db { |
||||||
|
if len(val.Extensions) > 0 { |
||||||
|
/* Denormalize - each extension has its own |
||||||
|
unique content-type now. Looks will be fast. */ |
||||||
|
for _, ext := range val.Extensions { |
||||||
|
/* Single extension type may map to |
||||||
|
multiple content-types. In that case, |
||||||
|
simply prefer the longest content-type |
||||||
|
to maintain some level of |
||||||
|
consistency. Only guarantee is, |
||||||
|
whatever content type is assigned, it |
||||||
|
is appropriate and valid type. */ |
||||||
|
if strings.Compare(mDB[ext].ContentType, key) < 0 { |
||||||
|
mDB[ext] = mimeEntry{ |
||||||
|
ContentType: key, |
||||||
|
Compressible: val.Compressible, |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return mDB, nil |
||||||
|
} |
||||||
|
|
||||||
|
func main() { |
||||||
|
// Take input json file from command-line".
|
||||||
|
if len(os.Args) != 2 { |
||||||
|
fmt.Print("Syntax:\n\tgen-db /path/to/db.json\n") |
||||||
|
os.Exit(1) |
||||||
|
} |
||||||
|
|
||||||
|
// Load and convert db.json into new database with extension
|
||||||
|
// as key.
|
||||||
|
mDB, err := convertDB(os.Args[1]) |
||||||
|
if err != nil { |
||||||
|
panic(err) |
||||||
|
} |
||||||
|
|
||||||
|
// Generate db embedded go program.
|
||||||
|
tmpl := template.New("mimedb") |
||||||
|
mimeTmpl, err := tmpl.Parse(progTempl) |
||||||
|
if err != nil { |
||||||
|
panic(err) |
||||||
|
} |
||||||
|
|
||||||
|
err = mimeTmpl.Execute(os.Stdout, mDB) |
||||||
|
if err != nil { |
||||||
|
panic(err) |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue