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

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