file.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package bindata
  2. import (
  3. "fmt"
  4. "io"
  5. )
  6. func writeAssetFS(w io.Writer, c *Config) error {
  7. if !c.HttpFileSystem {
  8. return nil
  9. }
  10. _, err := fmt.Fprintf(w, `
  11. type assetFile struct {
  12. *bytes.Reader
  13. name string
  14. childInfos []os.FileInfo
  15. childInfoOffset int
  16. }
  17. type assetOperator struct{}
  18. // Open implement http.FileSystem interface
  19. func (f *assetOperator) Open(name string) (http.File, error) {
  20. var err error
  21. if len(name) > 0 && name[0] == '/' {
  22. name = name[1:]
  23. }
  24. content, err := Asset(name)
  25. if err == nil {
  26. return &assetFile{name: name, Reader: bytes.NewReader(content)}, nil
  27. }
  28. children, err := AssetDir(name)
  29. if err == nil {
  30. childInfos := make([]os.FileInfo, 0, len(children))
  31. for _, child := range children {
  32. childPath := filepath.Join(name, child)
  33. info, errInfo := AssetInfo(filepath.Join(name, child))
  34. if errInfo == nil {
  35. childInfos = append(childInfos, info)
  36. } else {
  37. childInfos = append(childInfos, newDirFileInfo(childPath))
  38. }
  39. }
  40. return &assetFile{name: name, childInfos: childInfos}, nil
  41. } else {
  42. // If the error is not found, return an error that will
  43. // result in a 404 error. Otherwise the server returns
  44. // a 500 error for files not found.
  45. if strings.Contains(err.Error(), "not found") {
  46. return nil, os.ErrNotExist
  47. }
  48. return nil, err
  49. }
  50. }
  51. // Close no need do anything
  52. func (f *assetFile) Close() error {
  53. return nil
  54. }
  55. // Readdir read dir's children file info
  56. func (f *assetFile) Readdir(count int) ([]os.FileInfo, error) {
  57. if len(f.childInfos) == 0 {
  58. return nil, os.ErrNotExist
  59. }
  60. if count <= 0 {
  61. return f.childInfos, nil
  62. }
  63. if f.childInfoOffset+count > len(f.childInfos) {
  64. count = len(f.childInfos) - f.childInfoOffset
  65. }
  66. offset := f.childInfoOffset
  67. f.childInfoOffset += count
  68. return f.childInfos[offset : offset+count], nil
  69. }
  70. // Stat read file info from asset item
  71. func (f *assetFile) Stat() (os.FileInfo, error) {
  72. if len(f.childInfos) != 0 {
  73. return newDirFileInfo(f.name), nil
  74. }
  75. return AssetInfo(f.name)
  76. }
  77. // newDirFileInfo return default dir file info
  78. func newDirFileInfo(name string) os.FileInfo {
  79. return &bindataFileInfo{
  80. name: name,
  81. size: 0,
  82. mode: os.FileMode(2147484068), // equal os.FileMode(0644)|os.ModeDir
  83. modTime: time.Time{}}
  84. }
  85. // AssetFile return a http.FileSystem instance that data backend by asset
  86. func AssetFile() http.FileSystem {
  87. return &assetOperator{}
  88. }
  89. `)
  90. return err
  91. }