resources.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (C) 2019 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. package util
  16. import (
  17. "html/template"
  18. "os"
  19. "path/filepath"
  20. "runtime"
  21. "github.com/drakkan/sftpgo/v2/internal/logger"
  22. )
  23. // FindSharedDataPath searches for the specified directory name in searchDir
  24. // and in system-wide shared data directories.
  25. // If name is an absolute path it is returned unmodified.
  26. func FindSharedDataPath(name, searchDir string) string {
  27. if !IsFileInputValid(name) {
  28. return ""
  29. }
  30. if name != "" && !filepath.IsAbs(name) {
  31. searchList := []string{searchDir}
  32. if additionalSharedDataSearchPath != "" {
  33. searchList = append(searchList, additionalSharedDataSearchPath)
  34. }
  35. if runtime.GOOS != osWindows {
  36. searchList = append(searchList, "/usr/share/sftpgo")
  37. searchList = append(searchList, "/usr/local/share/sftpgo")
  38. }
  39. searchList = RemoveDuplicates(searchList, false)
  40. for _, basePath := range searchList {
  41. res := filepath.Join(basePath, name)
  42. _, err := os.Stat(res)
  43. if err == nil {
  44. logger.Debug(logSender, "", "found share data path for name %q: %q", name, res)
  45. return res
  46. }
  47. }
  48. return filepath.Join(searchDir, name)
  49. }
  50. return name
  51. }
  52. // LoadTemplate parses the given template paths.
  53. // It behaves like template.Must but it writes a log before exiting.
  54. func LoadTemplate(base *template.Template, paths ...string) *template.Template {
  55. if base != nil {
  56. baseTmpl, err := base.Clone()
  57. if err != nil {
  58. showTemplateLoadingError(err)
  59. }
  60. t, err := baseTmpl.ParseFiles(paths...)
  61. if err != nil {
  62. showTemplateLoadingError(err)
  63. }
  64. return t
  65. }
  66. t, err := template.ParseFiles(paths...)
  67. if err != nil {
  68. showTemplateLoadingError(err)
  69. }
  70. return t
  71. }
  72. func showTemplateLoadingError(err error) {
  73. logger.ErrorToConsole("error loading required template: %v", err)
  74. logger.ErrorToConsole(templateLoadErrorHints)
  75. logger.Error(logSender, "", "error loading required template: %v", err)
  76. os.Exit(1)
  77. }