Initial commit
commit
53d6b98865
@ -0,0 +1,20 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2018 Nick Thomas <go-mimedb@ur.gs>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@ -0,0 +1,12 @@
|
||||
# MIME DB
|
||||
|
||||
This Go package uses generators to convert [this database](https://github.com/jshttp/mime-db)
|
||||
into additions to the stdlib `mime` package.
|
||||
|
||||
Since all the work is done at compile time, the MIME types end up embedded in
|
||||
the binary, loading them on startup is fast, and you still get sensible results
|
||||
when `/etc/mime.types` is unavailable on your platform!
|
||||
|
||||
This work is somewhat inspired by [mime-ext-go](https://github.com/mytrile/mime-ext-go),
|
||||
which lacks the automatic generation (and so easy update) to be found in this
|
||||
package.
|
@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type MimeType struct {
|
||||
Source string `json:"source"`
|
||||
Extensions []string `json:"extensions"`
|
||||
Compressible bool `json:"compressible"`
|
||||
Charset string `json:"charset"`
|
||||
}
|
||||
|
||||
type Data map[string]MimeType
|
||||
|
||||
func main() {
|
||||
out, err := os.OpenFile("generated_mime_types.go", os.O_RDWR|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
defer out.Close()
|
||||
|
||||
rsp, err := http.Get("https://cdn.rawgit.com/jshttp/mime-db/v1.33.0/db.json")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if rsp.StatusCode != http.StatusOK {
|
||||
panic(rsp)
|
||||
}
|
||||
|
||||
var result Data
|
||||
|
||||
if err := json.NewDecoder(rsp.Body).Decode(&result); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Fprintln(out, "package mimedb")
|
||||
fmt.Fprintln(out, "")
|
||||
fmt.Fprintln(out, "var (")
|
||||
fmt.Fprintln(out, " mimeTypeToExts = map[string][]string{")
|
||||
|
||||
for mimeType, entry := range result {
|
||||
if len(entry.Extensions) > 0 {
|
||||
fmt.Fprintf(out, " %q: %#v,\n", mimeType, entry.Extensions)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintln(out, " }")
|
||||
fmt.Fprintln(out, ")")
|
||||
|
||||
if err := out.Close(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// run `go fmt` on the output
|
||||
if err := exec.Command("go", "fmt").Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package mimedb
|
||||
|
||||
import (
|
||||
"mime"
|
||||
)
|
||||
|
||||
//go:generate go run cmd/generate/main.go
|
||||
|
||||
func LoadTypes() error {
|
||||
// mimeTypeToExts is declared in the generated_mime_types.go file
|
||||
for mimeType, extensions := range mimeTypeToExts {
|
||||
for _, extension := range extensions {
|
||||
if err := mime.AddExtensionType("."+extension, mimeType); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package mimedb
|
||||
|
||||
import (
|
||||
"mime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoadTypes(t *testing.T) {
|
||||
if mime.TypeByExtension(".webmanifest") != "" {
|
||||
t.Log("We have .webmanifest BEFORE loading the DB. Passing would be false negative!")
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
if err := LoadTypes(); err != nil {
|
||||
t.Logf("Unexpected error loading types: %s", err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
if mime.TypeByExtension(".webmanifest") != "application/manifest+json" {
|
||||
t.Logf("A MIME type for .webmanifest was not found")
|
||||
t.Fail()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue