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.

150 lines
3.3 KiB

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