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

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