LoginSignup
3
5

More than 5 years have passed since last update.

goでAssetsをgo-bindataでバイナリ含むようにするとnet/httpなどで404なファイルを参照するときでも500が出てしまう問題を修正 + ディレクトリを参照するとファイルリストが見えてしまう問題を修正

Last updated at Posted at 2016-06-05

他の人がすでにやってくれているPRが全然マージされている気配がなかったので、自分用につくりました。

diff --git a/go-bindata/main.go b/go-bindata/main.go
index 503a059..c5889b3 100644
--- a/go-bindata/main.go
+++ b/go-bindata/main.go
@@ -12,7 +12,7 @@ import (
    "regexp"
    "strings"

-   "github.com/jteeuwen/go-bindata"
+   "github.com/kazuph/go-bindata"
 )

 func main() {
diff --git a/toc.go b/toc.go
index 9ec410b..9ea6320 100644
--- a/toc.go
+++ b/toc.go
@@ -93,39 +93,9 @@ var _bintree = `)
 }

 func writeTOCTree(w io.Writer, toc []Asset) error {
-   _, err := fmt.Fprintf(w, `// AssetDir returns the file names below a certain
-// directory embedded in the file by go-bindata.
-// For example if you run go-bindata on data/... and data contains the
-// following hierarchy:
-//     data/
-//       foo.txt
-//       img/
-//         a.png
-//         b.png
-// then AssetDir("data") would return []string{"foo.txt", "img"}
-// AssetDir("data/img") would return []string{"a.png", "b.png"}
-// AssetDir("foo.txt") and AssetDir("notexist") would return an error
-// AssetDir("") will return []string{"data"}.
+   _, err := fmt.Fprintf(w, `// AssetDir returns only err
 func AssetDir(name string) ([]string, error) {
-   node := _bintree
-   if len(name) != 0 {
-       cannonicalName := strings.Replace(name, "\\", "/", -1)
-       pathList := strings.Split(cannonicalName, "/")
-       for _, p := range pathList {
-           node = node.Children[p]
-           if node == nil {
-               return nil, fmt.Errorf("Asset %%s not found", name)
-           }
-       }
-   }
-   if node.Func != nil {
-       return nil, fmt.Errorf("Asset %%s not found", name)
-   }
-   rv := make([]string, 0, len(node.Children))
-   for childName := range node.Children {
-       rv = append(rv, childName)
-   }
-   return rv, nil
+   return nil,  &os.PathError{Op: "open",  Path: name,  Err: os.ErrNotExist}
 }

 `)
@@ -171,7 +141,7 @@ func Asset(name string) ([]byte, error) {
        }
        return a.bytes, nil
    }
-   return nil, fmt.Errorf("Asset %%s not found", name)
+   return nil, &os.PathError{Op: "open", Path: name, Err: os.ErrNotExist}
 }

 // MustAsset is like Asset but panics when Asset would return an error.
@@ -197,7 +167,7 @@ func AssetInfo(name string) (os.FileInfo, error) {
        }
        return a.info, nil
    }
-   return nil, fmt.Errorf("AssetInfo %%s not found", name)
+   return nil, &os.PathError{Op: "open", Path: name, Err: os.ErrNotExist}
 }

 // AssetNames returns the names of the assets.

使う方法は以下のようにこちらのリポジトリを選んでもらえれば。

$ go get github.com/kazuph/go-bindata/...
$ go get github.com/elazarl/go-bindata-assetfs/...
$ go-bindata-assetfs templates/... public/...

として、

http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(assetFS())))
3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5