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 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // fileFromDir is a helper to read files from static or custom path.
  59. func fileFromDir(name string) ([]byte, error) {
  60. customPath := path.Join(setting.CustomPath, "options", name)
  61. if com.IsFile(customPath) {
  62. return ioutil.ReadFile(customPath)
  63. }
  64. staticPath := path.Join(setting.StaticRootPath, "options", name)
  65. if com.IsFile(staticPath) {
  66. return ioutil.ReadFile(staticPath)
  67. }
  68. return []byte{}, fmt.Errorf("Asset file does not exist: %s", name)
  69. }