|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 Moriyoshi Koizumi |
| 3 | + * Copyright (c) 2012 Elazar Leibovich. |
| 4 | + * Copyright (c) 2012 The Go Authors. |
| 5 | + * |
| 6 | + * All rights reserved. |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions are |
| 10 | + * met: |
| 11 | + * |
| 12 | + * * Redistributions of source code must retain the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer. |
| 14 | + * * Redistributions in binary form must reproduce the above |
| 15 | + * copyright notice, this list of conditions and the following disclaimer |
| 16 | + * in the documentation and/or other materials provided with the |
| 17 | + * distribution. |
| 18 | + * * Neither the name of Elazar Leibovich. nor the names of its |
| 19 | + * contributors may be used to endorse or promote products derived from |
| 20 | + * this software without specific prior written permission. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | + */ |
| 34 | +package main |
| 35 | + |
| 36 | +import ( |
| 37 | + "fmt" |
| 38 | + "mime" |
| 39 | + "net/http" |
| 40 | + "os" |
| 41 | + "path/filepath" |
| 42 | + |
| 43 | + "github.com/moriyoshi/mimetypes" |
| 44 | + _ "github.com/moriyoshi/mimetypes/loaders" |
| 45 | + "github.com/moriyoshi/simplefiletx" |
| 46 | +) |
| 47 | + |
| 48 | +var httpMetaKeys = []string{"Content-Type"} |
| 49 | + |
| 50 | +const defaultMimeType = "application/octet-stream" |
| 51 | + |
| 52 | +type MyOpener struct { |
| 53 | + MimeTypes mimetypes.MediaTypeRegistry |
| 54 | +} |
| 55 | + |
| 56 | +type MyReaderWithStat struct { |
| 57 | + inner simplefiletx.ReaderWithStat |
| 58 | + name string |
| 59 | + opener *MyOpener |
| 60 | +} |
| 61 | + |
| 62 | +func (opener *MyOpener) Open(name string) (simplefiletx.ReaderWithStat, error) { |
| 63 | + f, err := os.Open(name) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + return &MyReaderWithStat{f, name, opener}, nil |
| 69 | +} |
| 70 | + |
| 71 | +func (rws *MyReaderWithStat) Read(b []byte) (int, error) { |
| 72 | + return rws.inner.Read(b) |
| 73 | +} |
| 74 | + |
| 75 | +func (rws *MyReaderWithStat) Close() error { |
| 76 | + return rws.inner.Close() |
| 77 | +} |
| 78 | + |
| 79 | +func (rws *MyReaderWithStat) Stat() (os.FileInfo, error) { |
| 80 | + return rws.inner.Stat() |
| 81 | +} |
| 82 | + |
| 83 | +func (rws *MyReaderWithStat) GetHTTPMetadataKeys() ([]string, error) { |
| 84 | + return httpMetaKeys, nil |
| 85 | +} |
| 86 | + |
| 87 | +func (rws *MyReaderWithStat) GetHTTPMetadata(k string) ([]string, error) { |
| 88 | + if k == "Content-Type" { |
| 89 | + ext := filepath.Ext(rws.name) |
| 90 | + var mimeType string |
| 91 | + if rws.opener.MimeTypes != nil { |
| 92 | + mimeType = rws.opener.MimeTypes.TypeByExtension(ext) |
| 93 | + } else { |
| 94 | + mimeType = mime.TypeByExtension(ext) |
| 95 | + } |
| 96 | + if mimeType == "" { |
| 97 | + mimeType = defaultMimeType |
| 98 | + } |
| 99 | + return []string{mimeType}, nil |
| 100 | + } |
| 101 | + return nil, fmt.Errorf("no such key: %s", k) |
| 102 | +} |
| 103 | + |
| 104 | +func NewFileTransport(config FileTransportConfig) http.RoundTripper { |
| 105 | + return &simplefiletx.SimpleFileTransport{ |
| 106 | + BaseDir: config.RootDirectory, |
| 107 | + Opener: &MyOpener{config.MimeTypes}, |
| 108 | + } |
| 109 | +} |
0 commit comments