storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/mimedb/util/gen-db.go (about)

     1  /*
     2   * mimedb: Mime Database, (C) 2015 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  // Package mimedb is a database of file extension to mime content-type.
    18  // Definitions are imported from NodeJS mime-db project under MIT license.
    19  
    20  package main
    21  
    22  import (
    23  	"encoding/json"
    24  	"fmt"
    25  	"io/ioutil"
    26  	"os"
    27  	"strings"
    28  	"text/template"
    29  )
    30  
    31  const progTempl = `// DO NOT EDIT THIS FILE. IT IS AUTO-GENERATED BY "gen-db.go". //
    32  /*
    33   * mimedb: Mime Database, (C) 2016 MinIO, Inc.
    34   *
    35   * Licensed under the Apache License, Version 2.0 (the "License");
    36   * you may not use this file except in compliance with the License.
    37   * You may obtain a copy of the License at
    38   *
    39   *     http://www.apache.org/licenses/LICENSE-2.0
    40   *
    41   * Unless required by applicable law or agreed to in writing, software
    42   * distributed under the License is distributed on an "AS IS" BASIS,
    43   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    44   * See the License for the specific language governing permissions and
    45   * limitations under the License.
    46   */
    47  
    48  // Package mimedb is a database of file extension to mime content-type.
    49  // Definitions are imported from NodeJS mime-db project under MIT license.
    50  package mimedb
    51  
    52  // DB - Mime is a collection of mime types with extension as key and content-type as value.
    53  var DB = map[string]struct {
    54  	ContentType  string
    55  	Compressible bool
    56  }{
    57  {{range $extension, $entry := . }}	"{{$extension}}": {
    58  		ContentType:  "{{$entry.ContentType}}",
    59  		Compressible: {{$entry.Compressible}},
    60  	},
    61  {{end}}}
    62  `
    63  
    64  type mimeEntry struct {
    65  	ContentType  string `json:"contentType"`
    66  	Compressible bool   `json:"compresible"`
    67  }
    68  
    69  type mimeDB map[string]mimeEntry
    70  
    71  //  JSON data from gobindata and parse them into extDB.
    72  func convertDB(jsonFile string) (mimeDB, error) {
    73  	// Structure of JSON data from mime-db project.
    74  	type dbEntry struct {
    75  		Source       string   `json:"source"`
    76  		Compressible bool     `json:"compresible"`
    77  		Extensions   []string `json:"extensions"`
    78  	}
    79  
    80  	// Access embedded "db.json" inside go-bindata.
    81  	jsonDB, err := ioutil.ReadFile(jsonFile)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	// Convert db.json into go's typed structure.
    87  	db := make(map[string]dbEntry)
    88  	if err := json.Unmarshal(jsonDB, &db); err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	mDB := make(mimeDB)
    93  
    94  	// Generate a new database from mime-db.
    95  	for key, val := range db {
    96  		if len(val.Extensions) > 0 {
    97  			/* Denormalize - each extension has its own
    98  			unique content-type now. Looks will be fast. */
    99  			for _, ext := range val.Extensions {
   100  				/* Single extension type may map to
   101  				multiple content-types. In that case,
   102  				simply prefer the longest content-type
   103  				to maintain some level of
   104  				consistency. Only guarantee is,
   105  				whatever content type is assigned, it
   106  				is appropriate and valid type. */
   107  				if strings.Compare(mDB[ext].ContentType, key) < 0 {
   108  					mDB[ext] = mimeEntry{
   109  						ContentType:  key,
   110  						Compressible: val.Compressible,
   111  					}
   112  				}
   113  			}
   114  		}
   115  	}
   116  	return mDB, nil
   117  }
   118  
   119  func main() {
   120  	// Take input json file from command-line".
   121  	if len(os.Args) != 2 {
   122  		fmt.Print("Syntax:\n\tgen-db /path/to/db.json\n")
   123  		os.Exit(1)
   124  	}
   125  
   126  	// Load and convert db.json into new database with extension
   127  	// as key.
   128  	mDB, err := convertDB(os.Args[1])
   129  	if err != nil {
   130  		panic(err)
   131  	}
   132  
   133  	// Generate db embedded go program.
   134  	tmpl := template.New("mimedb")
   135  	mimeTmpl, err := tmpl.Parse(progTempl)
   136  	if err != nil {
   137  		panic(err)
   138  	}
   139  
   140  	err = mimeTmpl.Execute(os.Stdout, mDB)
   141  	if err != nil {
   142  		panic(err)
   143  	}
   144  }