You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

static.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2016 The Gitea 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. //go:build bindata
  5. // +build bindata
  6. package options
  7. import (
  8. "fmt"
  9. "io"
  10. "os"
  11. "path"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/modules/util"
  15. )
  16. var (
  17. directories = make(directorySet)
  18. )
  19. // Dir returns all files from bindata or custom directory.
  20. func Dir(name string) ([]string, error) {
  21. if directories.Filled(name) {
  22. return directories.Get(name), nil
  23. }
  24. var (
  25. result []string
  26. )
  27. customDir := path.Join(setting.CustomPath, "options", name)
  28. isDir, err := util.IsDir(customDir)
  29. if err != nil {
  30. return []string{}, fmt.Errorf("Failed to check if custom directory %s is a directory. %v", err)
  31. }
  32. if isDir {
  33. files, err := util.StatDir(customDir, true)
  34. if err != nil {
  35. return []string{}, fmt.Errorf("Failed to read custom directory. %v", err)
  36. }
  37. result = append(result, files...)
  38. }
  39. files, err := AssetDir(name)
  40. if err != nil {
  41. return []string{}, fmt.Errorf("Failed to read embedded directory. %v", err)
  42. }
  43. result = append(result, files...)
  44. return directories.AddAndGet(name, result), nil
  45. }
  46. func AssetDir(dirName string) ([]string, error) {
  47. d, err := Assets.Open(dirName)
  48. if err != nil {
  49. return nil, err
  50. }
  51. defer d.Close()
  52. files, err := d.Readdir(-1)
  53. if err != nil {
  54. return nil, err
  55. }
  56. var results = make([]string, 0, len(files))
  57. for _, file := range files {
  58. results = append(results, file.Name())
  59. }
  60. return results, nil
  61. }
  62. // Locale reads the content of a specific locale from bindata or custom path.
  63. func Locale(name string) ([]byte, error) {
  64. return fileFromDir(path.Join("locale", name))
  65. }
  66. // Readme reads the content of a specific readme from bindata or custom path.
  67. func Readme(name string) ([]byte, error) {
  68. return fileFromDir(path.Join("readme", name))
  69. }
  70. // Gitignore reads the content of a gitignore locale from bindata or custom path.
  71. func Gitignore(name string) ([]byte, error) {
  72. return fileFromDir(path.Join("gitignore", name))
  73. }
  74. // License reads the content of a specific license from bindata or custom path.
  75. func License(name string) ([]byte, error) {
  76. return fileFromDir(path.Join("license", name))
  77. }
  78. // Labels reads the content of a specific labels from static or custom path.
  79. func Labels(name string) ([]byte, error) {
  80. return fileFromDir(path.Join("label", name))
  81. }
  82. // fileFromDir is a helper to read files from bindata or custom path.
  83. func fileFromDir(name string) ([]byte, error) {
  84. customPath := path.Join(setting.CustomPath, "options", name)
  85. isFile, err := util.IsFile(customPath)
  86. if err != nil {
  87. log.Error("Unable to check if %s is a file. Error: %v", customPath, err)
  88. }
  89. if isFile {
  90. return os.ReadFile(customPath)
  91. }
  92. f, err := Assets.Open(name)
  93. if err != nil {
  94. return nil, err
  95. }
  96. defer f.Close()
  97. return io.ReadAll(f)
  98. }
  99. func Asset(name string) ([]byte, error) {
  100. f, err := Assets.Open("/" + name)
  101. if err != nil {
  102. return nil, err
  103. }
  104. defer f.Close()
  105. return io.ReadAll(f)
  106. }
  107. func AssetNames() []string {
  108. realFS := Assets.(vfsgen۰FS)
  109. var results = make([]string, 0, len(realFS))
  110. for k := range realFS {
  111. results = append(results, k[1:])
  112. }
  113. return results
  114. }
  115. func AssetIsDir(name string) (bool, error) {
  116. if f, err := Assets.Open("/" + name); err != nil {
  117. return false, err
  118. } else {
  119. defer f.Close()
  120. if fi, err := f.Stat(); err != nil {
  121. return false, err
  122. } else {
  123. return fi.IsDir(), nil
  124. }
  125. }
  126. }
  127. // IsDynamic will return false when using embedded data (-tags bindata)
  128. func IsDynamic() bool {
  129. return false
  130. }