api_statics.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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)
  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 file == "" {
  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.serveFromAssetDir(file, theme, w, r) {
  90. return
  91. }
  92. // Check for a compiled in asset for the current theme.
  93. if s.serveFromAssets(file, theme, modificationTime, w, r) {
  94. return
  95. }
  96. // Check for an overridden default asset.
  97. if s.serveFromAssetDir(file, config.DefaultTheme, w, r) {
  98. return
  99. }
  100. // Check for a compiled in default asset.
  101. if s.serveFromAssets(file, config.DefaultTheme, modificationTime, w, r) {
  102. return
  103. }
  104. http.NotFound(w, r)
  105. }
  106. func (s *staticsServer) serveFromAssetDir(file, theme string, w http.ResponseWriter, r *http.Request) bool {
  107. if s.assetDir == "" {
  108. return false
  109. }
  110. p := filepath.Join(s.assetDir, theme, filepath.FromSlash(file))
  111. if _, err := os.Stat(p); err != nil {
  112. return false
  113. }
  114. mtype := assets.MimeTypeForFile(file)
  115. if mtype != "" {
  116. w.Header().Set("Content-Type", mtype)
  117. }
  118. http.ServeFile(w, r, p)
  119. return true
  120. }
  121. func (s *staticsServer) serveFromAssets(file, theme string, modificationTime time.Time, w http.ResponseWriter, r *http.Request) bool {
  122. as, ok := s.assets[theme+"/"+file]
  123. if !ok {
  124. return false
  125. }
  126. as.Modified = modificationTime
  127. assets.Serve(w, r, as)
  128. return true
  129. }
  130. func (s *staticsServer) serveThemes(w http.ResponseWriter) {
  131. sendJSON(w, map[string][]string{
  132. "themes": s.availableThemes,
  133. })
  134. }
  135. func (s *staticsServer) setTheme(theme string) {
  136. s.mut.Lock()
  137. s.theme = theme
  138. s.lastThemeChange = time.Now().UTC()
  139. s.mut.Unlock()
  140. }
  141. func (s *staticsServer) String() string {
  142. return fmt.Sprintf("staticsServer@%p", s)
  143. }