diff options
author | Alexey Sharov <www.pismeco@gmail.com> | 2018-10-01 19:39:39 +0800 |
---|---|---|
committer | Anton Evangelatov <anton.evangelatov@gmail.com> | 2018-10-01 19:39:39 +0800 |
commit | dc5d643bb59812cda578fac941c2f1da316bc9d7 (patch) | |
tree | 7405f387672f0548eb4734a93581780c96cee7a9 /cmd/swarm/mimegen/generator.go | |
parent | b69942befeb9f1af55cad0f91953bdaea2ea3efb (diff) | |
download | dexon-dc5d643bb59812cda578fac941c2f1da316bc9d7.tar dexon-dc5d643bb59812cda578fac941c2f1da316bc9d7.tar.gz dexon-dc5d643bb59812cda578fac941c2f1da316bc9d7.tar.bz2 dexon-dc5d643bb59812cda578fac941c2f1da316bc9d7.tar.lz dexon-dc5d643bb59812cda578fac941c2f1da316bc9d7.tar.xz dexon-dc5d643bb59812cda578fac941c2f1da316bc9d7.tar.zst dexon-dc5d643bb59812cda578fac941c2f1da316bc9d7.zip |
cmd/swarm, swarm: cross-platform Content-Type detection (#17782)
- Mime types generator (Standard "mime" package rely on system-settings, see mime.osInitMime)
- Changed swarm/api.Upload:
- simplify I/O throttling by semaphore primitive and use file name where possible
- f.Close() must be called in Defer - otherwise panic or future added early return will cause leak of file descriptors
- one error was suppressed
Diffstat (limited to 'cmd/swarm/mimegen/generator.go')
-rw-r--r-- | cmd/swarm/mimegen/generator.go | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/cmd/swarm/mimegen/generator.go b/cmd/swarm/mimegen/generator.go new file mode 100644 index 000000000..68f9e306e --- /dev/null +++ b/cmd/swarm/mimegen/generator.go @@ -0,0 +1,124 @@ +// Copyright 2018 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +package main + +// Standard "mime" package rely on system-settings, see mime.osInitMime +// Swarm will run on many OS/Platform/Docker and must behave similar +// This command generates code to add common mime types based on mime.types file +// +// mime.types file provided by mailcap, which follow https://www.iana.org/assignments/media-types/media-types.xhtml +// +// Get last version of mime.types file by: +// docker run --rm -v $(pwd):/tmp alpine:edge /bin/sh -c "apk add -U mailcap; mv /etc/mime.types /tmp" + +import ( + "bufio" + "bytes" + "flag" + "html/template" + "io/ioutil" + "strings" + + "log" +) + +var ( + typesFlag = flag.String("types", "", "Input mime.types file") + packageFlag = flag.String("package", "", "Golang package in output file") + outFlag = flag.String("out", "", "Output file name for the generated mime types") +) + +type mime struct { + Name string + Exts []string +} + +type templateParams struct { + PackageName string + Mimes []mime +} + +func main() { + // Parse and ensure all needed inputs are specified + flag.Parse() + if *typesFlag == "" { + log.Fatalf("--types is required") + } + if *packageFlag == "" { + log.Fatalf("--types is required") + } + if *outFlag == "" { + log.Fatalf("--out is required") + } + + params := templateParams{ + PackageName: *packageFlag, + } + + types, err := ioutil.ReadFile(*typesFlag) + if err != nil { + log.Fatal(err) + } + + scanner := bufio.NewScanner(bytes.NewReader(types)) + for scanner.Scan() { + txt := scanner.Text() + if strings.HasPrefix(txt, "#") || len(txt) == 0 { + continue + } + parts := strings.Fields(txt) + if len(parts) == 1 { + continue + } + params.Mimes = append(params.Mimes, mime{parts[0], parts[1:]}) + } + + if err = scanner.Err(); err != nil { + log.Fatal(err) + } + + result := bytes.NewBuffer([]byte{}) + + if err := template.Must(template.New("_").Parse(tpl)).Execute(result, params); err != nil { + log.Fatal(err) + } + + if err := ioutil.WriteFile(*outFlag, result.Bytes(), 0600); err != nil { + log.Fatal(err) + } +} + +var tpl = `// Code generated by github.com/ethereum/go-ethereum/cmd/swarm/mimegen. DO NOT EDIT. + +package {{ .PackageName }} + +import "mime" +func init() { + var mimeTypes = map[string]string{ +{{- range .Mimes -}} + {{ $name := .Name -}} + {{- range .Exts }} + ".{{ . }}": "{{ $name | html }}", + {{- end }} +{{- end }} + } + for ext, name := range mimeTypes { + if err := mime.AddExtensionType(ext, name); err != nil { + panic(err) + } + } +} +` |