api_statics.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package api
  7. import (
  8. "fmt"
  9. "net/http"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. "time"
  14. "github.com/syncthing/syncthing/lib/api/auto"
  15. "github.com/syncthing/syncthing/lib/assets"
  16. "github.com/syncthing/syncthing/lib/config"
  17. "github.com/syncthing/syncthing/lib/sync"
  18. )
  19. const themePrefix = "theme-assets/"
  20. type staticsServer struct {
  21. assetDir string
  22. assets map[string]assets.Asset
  23. availableThemes []string
  24. mut sync.RWMutex
  25. theme string
  26. lastThemeChange time.Time
  27. }
  28. func newStaticsServer(theme, assetDir string) *staticsServer {
  29. s := &staticsServer{
  30. assetDir: assetDir,
  31. assets: auto.Assets(),
  32. mut: sync.NewRWMutex(),
  33. theme: theme,
  34. lastThemeChange: time.Now().UTC(),
  35. }
  36. seen := make(map[string]struct{})
  37. // Load themes from compiled in assets.
  38. for file := range auto.Assets() {
  39. theme := strings.Split(file, "/")[0]
  40. if _, ok := seen[theme]; !ok {
  41. seen[theme] = struct{}{}
  42. s.availableThemes = append(s.availableThemes, theme)
  43. }
  44. }
  45. if assetDir != "" {
  46. // Load any extra themes from the asset override dir.
  47. for _, dir := range dirNames(assetDir) {
  48. if _, ok := seen[dir]; !ok {
  49. seen[dir] = struct{}{}
  50. s.availableThemes = append(s.availableThemes, dir)
  51. }
  52. }
  53. }
  54. return s
  55. }
  56. func (s *staticsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  57. switch r.URL.Path {
  58. case "/themes.json":
  59. s.serveThemes(w, r)
  60. default:
  61. s.serveAsset(w, r)
  62. }
  63. }
  64. func (s *staticsServer) serveAsset(w http.ResponseWriter, r *http.Request) {
  65. w.Header().Set("Cache-Control", "no-cache, must-revalidate")
  66. file := r.URL.Path
  67. if file[0] == '/' {
  68. file = file[1:]
  69. }
  70. if len(file) == 0 {
  71. file = "index.html"
  72. }
  73. s.mut.RLock()
  74. theme := s.theme
  75. modificationTime := s.lastThemeChange
  76. s.mut.RUnlock()
  77. // If path starts with special prefix, get theme and file from path
  78. if strings.HasPrefix(file, themePrefix) {
  79. path := file[len(themePrefix):]
  80. i := strings.IndexRune(path, '/')
  81. if i == -1 {
  82. http.NotFound(w, r)
  83. return
  84. }
  85. theme = path[:i]
  86. file = path[i+1:]
  87. }
  88. // Check for an override for the current theme.
  89. if s.assetDir != "" {
  90. p := filepath.Join(s.assetDir, theme, filepath.FromSlash(file))
  91. if _, err := os.Stat(p); err == nil {
  92. mtype := assets.MimeTypeForFile(file)
  93. if len(mtype) != 0 {
  94. w.Header().Set("Content-Type", mtype)
  95. }
  96. http.ServeFile(w, r, p)
  97. return
  98. }
  99. }
  100. // Check for a compiled in asset for the current theme.
  101. as, ok := s.assets[theme+"/"+file]
  102. if !ok {
  103. // Check for an overridden default asset.
  104. if s.assetDir != "" {
  105. p := filepath.Join(s.assetDir, config.DefaultTheme, filepath.FromSlash(file))
  106. if _, err := os.Stat(p); err == nil {
  107. mtype := assets.MimeTypeForFile(file)
  108. if len(mtype) != 0 {
  109. w.Header().Set("Content-Type", mtype)
  110. }
  111. http.ServeFile(w, r, p)
  112. return
  113. }
  114. }
  115. // Check for a compiled in default asset.
  116. as, ok = s.assets[config.DefaultTheme+"/"+file]
  117. if !ok {
  118. http.NotFound(w, r)
  119. return
  120. }
  121. }
  122. as.Modified = modificationTime
  123. assets.Serve(w, r, as)
  124. }
  125. func (s *staticsServer) serveThemes(w http.ResponseWriter, r *http.Request) {
  126. sendJSON(w, map[string][]string{
  127. "themes": s.availableThemes,
  128. })
  129. }
  130. func (s *staticsServer) setTheme(theme string) {
  131. s.mut.Lock()
  132. s.theme = theme
  133. s.lastThemeChange = time.Now().UTC()
  134. s.mut.Unlock()
  135. }
  136. func (s *staticsServer) String() string {
  137. return fmt.Sprintf("staticsServer@%p", s)
  138. }