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.

dynamic.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "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 (
  16. directories = make(directorySet)
  17. )
  18. // Dir returns all files from static 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("Unabe to check if custom directory %s is a directory. %v", customDir, 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. staticDir := path.Join(setting.StaticRootPath, "options", name)
  39. isDir, err = util.IsDir(staticDir)
  40. if err != nil {
  41. return []string{}, fmt.Errorf("Unabe to check if static directory %s is a directory. %v", staticDir, err)
  42. }
  43. if isDir {
  44. files, err := util.StatDir(staticDir, true)
  45. if err != nil {
  46. return []string{}, fmt.Errorf("Failed to read static directory. %v", err)
  47. }
  48. result = append(result, files...)
  49. }
  50. return directories.AddAndGet(name, result), nil
  51. }
  52. // Locale reads the content of a specific locale from static or custom path.
  53. func Locale(name string) ([]byte, error) {
  54. return fileFromDir(path.Join("locale", name))
  55. }
  56. // Readme reads the content of a specific readme from static or custom path.
  57. func Readme(name string) ([]byte, error) {
  58. return fileFromDir(path.Join("readme", name))
  59. }
  60. // Gitignore reads the content of a specific gitignore from static or custom path.
  61. func Gitignore(name string) ([]byte, error) {
  62. return fileFromDir(path.Join("gitignore", name))
  63. }
  64. // License reads the content of a specific license from static or custom path.
  65. func License(name string) ([]byte, error) {
  66. return fileFromDir(path.Join("license", name))
  67. }
  68. // Labels reads the content of a specific labels from static or custom path.
  69. func Labels(name string) ([]byte, error) {
  70. return fileFromDir(path.Join("label", name))
  71. }
  72. // fileFromDir is a helper to read files from static or custom path.
  73. func fileFromDir(name string) ([]byte, error) {
  74. customPath := path.Join(setting.CustomPath, "options", name)
  75. isFile, err := util.IsFile(customPath)
  76. if err != nil {
  77. log.Error("Unable to check if %s is a file. Error: %v", customPath, err)
  78. }
  79. if isFile {
  80. return os.ReadFile(customPath)
  81. }
  82. staticPath := path.Join(setting.StaticRootPath, "options", name)
  83. isFile, err = util.IsFile(staticPath)
  84. if err != nil {
  85. log.Error("Unable to check if %s is a file. Error: %v", staticPath, err)
  86. }
  87. if isFile {
  88. return os.ReadFile(staticPath)
  89. }
  90. return []byte{}, fmt.Errorf("Asset file does not exist: %s", name)
  91. }
  92. // IsDynamic will return false when using embedded data (-tags bindata)
  93. func IsDynamic() bool {
  94. return true
  95. }