api_statics.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. "sync"
  14. "time"
  15. "github.com/syncthing/syncthing/lib/api/auto"
  16. "github.com/syncthing/syncthing/lib/assets"
  17. "github.com/syncthing/syncthing/lib/config"
  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. theme: theme,
  33. lastThemeChange: time.Now().UTC(),
  34. }
  35. seen := make(map[string]struct{})
  36. // Load themes from compiled in assets.
  37. for file := range auto.Assets() {
  38. theme := strings.Split(file, "/")[0]
  39. if _, ok := seen[theme]; !ok {
  40. seen[theme] = struct{}{}
  41. s.availableThemes = append(s.availableThemes, theme)
  42. }
  43. }
  44. if assetDir != "" {
  45. // Load any extra themes from the asset override dir.
  46. for _, dir := range dirNames(assetDir) {
  47. if _, ok := seen[dir]; !ok {
  48. seen[dir] = struct{}{}
  49. s.availableThemes = append(s.availableThemes, dir)
  50. }
  51. }
  52. }
  53. return s
  54. }
  55. func (s *staticsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  56. switch r.URL.Path {
  57. case "/themes.json":
  58. s.serveThemes(w)
  59. default:
  60. s.serveAsset(w, r)
  61. }
  62. }
  63. func (s *staticsServer) serveAsset(w http.ResponseWriter, r *http.Request) {
  64. w.Header().Set("Cache-Control", "no-cache, must-revalidate")
  65. file := r.URL.Path
  66. if file[0] == '/' {
  67. file = file[1:]
  68. }
  69. if file == "" {
  70. file = "index.html"
  71. }
  72. s.mut.RLock()
  73. theme := s.theme
  74. modificationTime := s.lastThemeChange
  75. s.mut.RUnlock()
  76. // If path starts with special prefix, get theme and file from path
  77. if strings.HasPrefix(file, themePrefix) {
  78. path := file[len(themePrefix):]
  79. i := strings.IndexRune(path, '/')
  80. if i == -1 {
  81. http.NotFound(w, r)
  82. return
  83. }
  84. theme = path[:i]
  85. file = path[i+1:]
  86. }
  87. // Check for an override for the current theme.
  88. if s.serveFromAssetDir(file, theme, w, r) {
  89. return
  90. }
  91. // Check for a compiled in asset for the current theme.
  92. if s.serveFromAssets(file, theme, modificationTime, w, r) {
  93. return
  94. }
  95. // Check for an overridden default asset.
  96. if s.serveFromAssetDir(file, config.DefaultTheme, w, r) {
  97. return
  98. }
  99. // Check for a compiled in default asset.
  100. if s.serveFromAssets(file, config.DefaultTheme, modificationTime, w, r) {
  101. return
  102. }
  103. http.NotFound(w, r)
  104. }
  105. func (s *staticsServer) serveFromAssetDir(file, theme string, w http.ResponseWriter, r *http.Request) bool {
  106. if s.assetDir == "" {
  107. return false
  108. }
  109. p := filepath.Join(s.assetDir, theme, filepath.FromSlash(file))
  110. if _, err := os.Stat(p); err != nil {
  111. return false
  112. }
  113. mtype := assets.MimeTypeForFile(file)
  114. if mtype != "" {
  115. w.Header().Set("Content-Type", mtype)
  116. }
  117. http.ServeFile(w, r, p)
  118. return true
  119. }
  120. func (s *staticsServer) serveFromAssets(file, theme string, modificationTime time.Time, w http.ResponseWriter, r *http.Request) bool {
  121. as, ok := s.assets[theme+"/"+file]
  122. if !ok {
  123. return false
  124. }
  125. as.Modified = modificationTime
  126. assets.Serve(w, r, as)
  127. return true
  128. }
  129. func (s *staticsServer) serveThemes(w http.ResponseWriter) {
  130. sendJSON(w, map[string][]string{
  131. "themes": s.availableThemes,
  132. })
  133. }
  134. func (s *staticsServer) setTheme(theme string) {
  135. s.mut.Lock()
  136. s.theme = theme
  137. s.lastThemeChange = time.Now().UTC()
  138. s.mut.Unlock()
  139. }
  140. func (s *staticsServer) String() string {
  141. return fmt.Sprintf("staticsServer@%p", s)
  142. }