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.

105 lines
2.5 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 static 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. staticDir := path.Join(setting.StaticRootPath, "options", name)
  33. if com.IsDir(staticDir) {
  34. files, err := com.StatDir(staticDir, true)
  35. if err != nil {
  36. return []string{}, fmt.Errorf("Failed to read static directory. %v", err)
  37. }
  38. result = append(result, files...)
  39. }
  40. return directories.AddAndGet(name, result), nil
  41. }
  42. // Locale reads the content of a specific locale from static or custom path.
  43. func Locale(name string) ([]byte, error) {
  44. return fileFromDir(path.Join("locale", name))
  45. }
  46. // Readme reads the content of a specific readme from static or custom path.
  47. func Readme(name string) ([]byte, error) {
  48. return fileFromDir(path.Join("readme", name))
  49. }
  50. // Gitignore reads the content of a specific gitignore from static or custom path.
  51. func Gitignore(name string) ([]byte, error) {
  52. return fileFromDir(path.Join("gitignore", name))
  53. }
  54. // License reads the content of a specific license from static or custom path.
  55. func License(name string) ([]byte, error) {
  56. return fileFromDir(path.Join("license", name))
  57. }
  58. // Labels reads the content of a specific labels from static or custom path.
  59. func Labels(name string) ([]byte, error) {
  60. return fileFromDir(path.Join("label", name))
  61. }
  62. // fileFromDir is a helper to read files from static or custom path.
  63. func fileFromDir(name string) ([]byte, error) {
  64. customPath := path.Join(setting.CustomPath, "options", name)
  65. if com.IsFile(customPath) {
  66. return ioutil.ReadFile(customPath)
  67. }
  68. staticPath := path.Join(setting.StaticRootPath, "options", name)
  69. if com.IsFile(staticPath) {
  70. return ioutil.ReadFile(staticPath)
  71. }
  72. return []byte{}, fmt.Errorf("Asset file does not exist: %s", name)
  73. }
  74. // IsDynamic will return false when using embedded data (-tags bindata)
  75. func IsDynamic() bool {
  76. return true
  77. }