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.5KB

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