package bindata import ( "fmt" "io" ) func writeAssetFS(w io.Writer, c *Config) error { if !c.HttpFileSystem { return nil } _, err := fmt.Fprintf(w, ` type assetFile struct { *bytes.Reader name string childInfos []os.FileInfo childInfoOffset int } type assetOperator struct{} // Open implement http.FileSystem interface func (f *assetOperator) Open(name string) (http.File, error) { var err error if len(name) > 0 && name[0] == '/' { name = name[1:] } content, err := Asset(name) if err == nil { return &assetFile{name: name, Reader: bytes.NewReader(content)}, nil } children, err := AssetDir(name) if err == nil { childInfos := make([]os.FileInfo, 0, len(children)) for _, child := range children { childPath := filepath.Join(name, child) info, errInfo := AssetInfo(filepath.Join(name, child)) if errInfo == nil { childInfos = append(childInfos, info) } else { childInfos = append(childInfos, newDirFileInfo(childPath)) } } return &assetFile{name: name, childInfos: childInfos}, nil } else { // If the error is not found, return an error that will // result in a 404 error. Otherwise the server returns // a 500 error for files not found. if strings.Contains(err.Error(), "not found") { return nil, os.ErrNotExist } return nil, err } } // Close no need do anything func (f *assetFile) Close() error { return nil } // Readdir read dir's children file info func (f *assetFile) Readdir(count int) ([]os.FileInfo, error) { if len(f.childInfos) == 0 { return nil, os.ErrNotExist } if count <= 0 { return f.childInfos, nil } if f.childInfoOffset+count > len(f.childInfos) { count = len(f.childInfos) - f.childInfoOffset } offset := f.childInfoOffset f.childInfoOffset += count return f.childInfos[offset : offset+count], nil } // Stat read file info from asset item func (f *assetFile) Stat() (os.FileInfo, error) { if len(f.childInfos) != 0 { return newDirFileInfo(f.name), nil } return AssetInfo(f.name) } // newDirFileInfo return default dir file info func newDirFileInfo(name string) os.FileInfo { return &bindataFileInfo{ name: name, size: 0, mode: os.FileMode(2147484068), // equal os.FileMode(0644)|os.ModeDir modTime: time.Time{}} } // AssetFile return a http.FileSystem instance that data backend by asset func AssetFile() http.FileSystem { return &assetOperator{} } `) return err }