1
0

resources.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (C) 2019-2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. //go:build !bundle
  15. // +build !bundle
  16. package util
  17. import (
  18. "html/template"
  19. "os"
  20. "path/filepath"
  21. "runtime"
  22. "github.com/drakkan/sftpgo/v2/internal/logger"
  23. )
  24. // FindSharedDataPath searches for the specified directory name in searchDir
  25. // and in system-wide shared data directories.
  26. // If name is an absolute path it is returned unmodified.
  27. func FindSharedDataPath(name, searchDir string) string {
  28. if !IsFileInputValid(name) {
  29. return ""
  30. }
  31. if name != "" && !filepath.IsAbs(name) {
  32. searchList := []string{searchDir}
  33. if additionalSharedDataSearchPath != "" {
  34. searchList = append(searchList, additionalSharedDataSearchPath)
  35. }
  36. if runtime.GOOS != osWindows {
  37. searchList = append(searchList, "/usr/share/sftpgo")
  38. searchList = append(searchList, "/usr/local/share/sftpgo")
  39. }
  40. searchList = RemoveDuplicates(searchList, false)
  41. for _, basePath := range searchList {
  42. res := filepath.Join(basePath, name)
  43. _, err := os.Stat(res)
  44. if err == nil {
  45. logger.Debug(logSender, "", "found share data path for name %q: %q", name, res)
  46. return res
  47. }
  48. }
  49. return filepath.Join(searchDir, name)
  50. }
  51. return name
  52. }
  53. // LoadTemplate parses the given template paths.
  54. // It behaves like template.Must but it writes a log before exiting.
  55. // You can optionally provide a base template (e.g. to define some custom functions)
  56. func LoadTemplate(base *template.Template, paths ...string) *template.Template {
  57. var t *template.Template
  58. var err error
  59. if base != nil {
  60. base, err = base.Clone()
  61. if err == nil {
  62. t, err = base.ParseFiles(paths...)
  63. }
  64. } else {
  65. t, err = template.ParseFiles(paths...)
  66. }
  67. if err != nil {
  68. logger.ErrorToConsole("error loading required template: %v", err)
  69. logger.ErrorToConsole(templateLoadErrorHints)
  70. logger.Error(logSender, "", "error loading required template: %v", err)
  71. os.Exit(1)
  72. }
  73. return t
  74. }