public.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package public
  5. import (
  6. "bytes"
  7. "net/http"
  8. "os"
  9. "path/filepath"
  10. "time"
  11. "gogs.io/gogs/internal/assets"
  12. )
  13. //go:generate go-bindata -nomemcopy -pkg=public -ignore="\\.DS_Store|less" -prefix=../../../public -debug=false -o=public_gen.go ../../../public/...
  14. /*
  15. This file is a modified version of https://github.com/go-bindata/go-bindata/pull/18.
  16. */
  17. type fileInfo struct {
  18. name string
  19. size int64
  20. }
  21. func (d fileInfo) Name() string {
  22. return d.name
  23. }
  24. func (d fileInfo) Size() int64 {
  25. return d.size
  26. }
  27. func (d fileInfo) Mode() os.FileMode {
  28. return os.FileMode(0644) | os.ModeDir
  29. }
  30. func (d fileInfo) ModTime() time.Time {
  31. return time.Time{}
  32. }
  33. // IsDir return file whether a directory
  34. func (d *fileInfo) IsDir() bool {
  35. return true
  36. }
  37. func (d fileInfo) Sys() interface{} {
  38. return nil
  39. }
  40. // file implements the http.File interface.
  41. type file struct {
  42. name string
  43. *bytes.Reader
  44. children []os.FileInfo
  45. childrenOffset int
  46. }
  47. func (f *file) Close() error {
  48. return nil
  49. }
  50. // ⚠️ WARNING: This method is not concurrent-safe.
  51. func (f *file) Readdir(count int) ([]os.FileInfo, error) {
  52. if len(f.children) == 0 {
  53. return nil, os.ErrNotExist
  54. }
  55. if count <= 0 {
  56. return f.children, nil
  57. }
  58. if f.childrenOffset+count > len(f.children) {
  59. count = len(f.children) - f.childrenOffset
  60. }
  61. offset := f.childrenOffset
  62. f.childrenOffset += count
  63. return f.children[offset : offset+count], nil
  64. }
  65. func (f *file) Stat() (os.FileInfo, error) {
  66. childCount := len(f.children)
  67. if childCount != 0 {
  68. return &fileInfo{
  69. name: f.name,
  70. size: int64(childCount),
  71. }, nil
  72. }
  73. return AssetInfo(f.name)
  74. }
  75. // fileSystem implements the http.FileSystem interface.
  76. type fileSystem struct{}
  77. func (f *fileSystem) Open(name string) (http.File, error) {
  78. if len(name) > 0 && name[0] == '/' {
  79. name = name[1:]
  80. }
  81. // Attempt to get it as a file
  82. p, err := Asset(name)
  83. if err != nil && !assets.IsErrNotFound(err) {
  84. return nil, err
  85. } else if err == nil {
  86. return &file{
  87. name: name,
  88. Reader: bytes.NewReader(p),
  89. }, nil
  90. }
  91. // Attempt to get it as a directory
  92. paths, err := AssetDir(name)
  93. if err != nil && !assets.IsErrNotFound(err) {
  94. return nil, err
  95. }
  96. infos := make([]os.FileInfo, len(paths))
  97. for i, path := range paths {
  98. path = filepath.Join(name, path)
  99. info, err := AssetInfo(path)
  100. if err != nil {
  101. if !assets.IsErrNotFound(err) {
  102. return nil, err
  103. }
  104. // Not found as a file, assume it's a directory.
  105. infos[i] = &fileInfo{name: path}
  106. } else {
  107. infos[i] = info
  108. }
  109. }
  110. return &file{
  111. name: name,
  112. children: infos,
  113. }, nil
  114. }
  115. // NewFileSystem returns an http.FileSystem instance backed by embedded assets.
  116. func NewFileSystem() http.FileSystem {
  117. return &fileSystem{}
  118. }