github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/cmd/swarm/mimegen/generator.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-ethereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 package main 17 18 // Standard "mime" package rely on system-settings, see mime.osInitMime 19 // Swarm will run on many OS/Platform/Docker and must behave similar 20 // This command generates code to add common mime types based on mime.types file 21 // 22 // mime.types file provided by mailcap, which follow https://www.iana.org/assignments/media-types/media-types.xhtml 23 // 24 // Get last version of mime.types file by: 25 // docker run --rm -v $(pwd):/tmp alpine:edge /bin/sh -c "apk add -U mailcap; mv /etc/mime.types /tmp" 26 27 import ( 28 "bufio" 29 "bytes" 30 "flag" 31 "html/template" 32 "io/ioutil" 33 "log" 34 "strings" 35 ) 36 37 var ( 38 typesFlag = flag.String("types", "", "Input mime.types file") 39 packageFlag = flag.String("package", "", "Golang package in output file") 40 outFlag = flag.String("out", "", "Output file name for the generated mime types") 41 ) 42 43 type mime struct { 44 Name string 45 Exts []string 46 } 47 48 type templateParams struct { 49 PackageName string 50 Mimes []mime 51 } 52 53 func main() { 54 // Parse and ensure all needed inputs are specified 55 flag.Parse() 56 if *typesFlag == "" { 57 log.Fatalf("--types is required") 58 } 59 if *packageFlag == "" { 60 log.Fatalf("--types is required") 61 } 62 if *outFlag == "" { 63 log.Fatalf("--out is required") 64 } 65 66 params := templateParams{ 67 PackageName: *packageFlag, 68 } 69 70 types, err := ioutil.ReadFile(*typesFlag) 71 if err != nil { 72 log.Fatal(err) 73 } 74 75 scanner := bufio.NewScanner(bytes.NewReader(types)) 76 for scanner.Scan() { 77 txt := scanner.Text() 78 if strings.HasPrefix(txt, "#") || len(txt) == 0 { 79 continue 80 } 81 parts := strings.Fields(txt) 82 if len(parts) == 1 { 83 continue 84 } 85 params.Mimes = append(params.Mimes, mime{parts[0], parts[1:]}) 86 } 87 88 if err = scanner.Err(); err != nil { 89 log.Fatal(err) 90 } 91 92 result := bytes.NewBuffer([]byte{}) 93 94 if err := template.Must(template.New("_").Parse(tpl)).Execute(result, params); err != nil { 95 log.Fatal(err) 96 } 97 98 if err := ioutil.WriteFile(*outFlag, result.Bytes(), 0600); err != nil { 99 log.Fatal(err) 100 } 101 } 102 103 var tpl = `// Code generated by github.com/ethereum/go-ethereum/cmd/swarm/mimegen. DO NOT EDIT. 104 105 package {{ .PackageName }} 106 107 import "mime" 108 func init() { 109 var mimeTypes = map[string]string{ 110 {{- range .Mimes -}} 111 {{ $name := .Name -}} 112 {{- range .Exts }} 113 ".{{ . }}": "{{ $name | html }}", 114 {{- end }} 115 {{- end }} 116 } 117 for ext, name := range mimeTypes { 118 if err := mime.AddExtensionType(ext, name); err != nil { 119 panic(err) 120 } 121 } 122 } 123 `