2016-06-07 09:46:45 +02:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 07:52:18 +01:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-06-07 09:46:45 +02:00
|
|
|
|
2019-03-26 20:53:58 +01:00
|
|
|
package api
|
2016-06-07 09:46:45 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2018-05-10 07:53:39 +02:00
|
|
|
"time"
|
2016-06-07 09:46:45 +02:00
|
|
|
|
2020-05-10 11:44:34 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/api/auto"
|
|
|
|
"github.com/syncthing/syncthing/lib/assets"
|
2016-06-07 09:46:45 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
|
|
|
)
|
|
|
|
|
2019-11-08 22:44:37 +01:00
|
|
|
const themePrefix = "theme-assets/"
|
|
|
|
|
2016-06-07 09:46:45 +02:00
|
|
|
type staticsServer struct {
|
|
|
|
assetDir string
|
2020-05-25 08:51:27 +02:00
|
|
|
assets map[string]assets.Asset
|
2016-06-07 09:46:45 +02:00
|
|
|
availableThemes []string
|
|
|
|
|
2019-11-08 22:37:42 +01:00
|
|
|
mut sync.RWMutex
|
|
|
|
theme string
|
|
|
|
lastThemeChange time.Time
|
2016-06-07 09:46:45 +02:00
|
|
|
}
|
|
|
|
|
2021-04-13 10:12:56 +02:00
|
|
|
func newStaticsServer(theme, assetDir string) *staticsServer {
|
2016-06-07 09:46:45 +02:00
|
|
|
s := &staticsServer{
|
2019-11-08 22:37:42 +01:00
|
|
|
assetDir: assetDir,
|
|
|
|
assets: auto.Assets(),
|
|
|
|
mut: sync.NewRWMutex(),
|
|
|
|
theme: theme,
|
|
|
|
lastThemeChange: time.Now().UTC(),
|
2016-06-07 09:46:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
seen := make(map[string]struct{})
|
|
|
|
// Load themes from compiled in assets.
|
|
|
|
for file := range auto.Assets() {
|
|
|
|
theme := strings.Split(file, "/")[0]
|
|
|
|
if _, ok := seen[theme]; !ok {
|
|
|
|
seen[theme] = struct{}{}
|
|
|
|
s.availableThemes = append(s.availableThemes, theme)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if assetDir != "" {
|
|
|
|
// Load any extra themes from the asset override dir.
|
|
|
|
for _, dir := range dirNames(assetDir) {
|
|
|
|
if _, ok := seen[dir]; !ok {
|
|
|
|
seen[dir] = struct{}{}
|
|
|
|
s.availableThemes = append(s.availableThemes, dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *staticsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.URL.Path {
|
|
|
|
case "/themes.json":
|
2021-09-08 00:11:16 +02:00
|
|
|
s.serveThemes(w)
|
2016-06-07 09:46:45 +02:00
|
|
|
default:
|
|
|
|
s.serveAsset(w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *staticsServer) serveAsset(w http.ResponseWriter, r *http.Request) {
|
2018-05-10 07:53:39 +02:00
|
|
|
w.Header().Set("Cache-Control", "no-cache, must-revalidate")
|
|
|
|
|
2016-06-07 09:46:45 +02:00
|
|
|
file := r.URL.Path
|
|
|
|
|
|
|
|
if file[0] == '/' {
|
|
|
|
file = file[1:]
|
|
|
|
}
|
|
|
|
|
2022-07-28 18:49:44 +02:00
|
|
|
if file == "" {
|
2016-06-07 09:46:45 +02:00
|
|
|
file = "index.html"
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mut.RLock()
|
|
|
|
theme := s.theme
|
2019-11-08 22:37:42 +01:00
|
|
|
modificationTime := s.lastThemeChange
|
2016-06-07 09:46:45 +02:00
|
|
|
s.mut.RUnlock()
|
|
|
|
|
2019-11-08 22:44:37 +01:00
|
|
|
// If path starts with special prefix, get theme and file from path
|
|
|
|
if strings.HasPrefix(file, themePrefix) {
|
|
|
|
path := file[len(themePrefix):]
|
|
|
|
i := strings.IndexRune(path, '/')
|
|
|
|
|
|
|
|
if i == -1 {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
theme = path[:i]
|
|
|
|
file = path[i+1:]
|
|
|
|
}
|
|
|
|
|
2016-06-07 09:46:45 +02:00
|
|
|
// Check for an override for the current theme.
|
2020-11-09 15:33:32 +01:00
|
|
|
if s.serveFromAssetDir(file, theme, w, r) {
|
|
|
|
return
|
2016-06-07 09:46:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a compiled in asset for the current theme.
|
2020-11-09 15:33:32 +01:00
|
|
|
if s.serveFromAssets(file, theme, modificationTime, w, r) {
|
|
|
|
return
|
|
|
|
}
|
2016-06-07 09:46:45 +02:00
|
|
|
|
2020-11-09 15:33:32 +01:00
|
|
|
// Check for an overridden default asset.
|
|
|
|
if s.serveFromAssetDir(file, config.DefaultTheme, w, r) {
|
|
|
|
return
|
2016-06-07 09:46:45 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 15:33:32 +01:00
|
|
|
// Check for a compiled in default asset.
|
|
|
|
if s.serveFromAssets(file, config.DefaultTheme, modificationTime, w, r) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *staticsServer) serveFromAssetDir(file, theme string, w http.ResponseWriter, r *http.Request) bool {
|
|
|
|
if s.assetDir == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
p := filepath.Join(s.assetDir, theme, filepath.FromSlash(file))
|
|
|
|
if _, err := os.Stat(p); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
mtype := assets.MimeTypeForFile(file)
|
2022-07-28 18:49:44 +02:00
|
|
|
if mtype == "" {
|
2020-11-09 15:33:32 +01:00
|
|
|
w.Header().Set("Content-Type", mtype)
|
|
|
|
}
|
|
|
|
http.ServeFile(w, r, p)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *staticsServer) serveFromAssets(file, theme string, modificationTime time.Time, w http.ResponseWriter, r *http.Request) bool {
|
|
|
|
as, ok := s.assets[theme+"/"+file]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2020-05-25 08:51:27 +02:00
|
|
|
as.Modified = modificationTime
|
|
|
|
assets.Serve(w, r, as)
|
2020-11-09 15:33:32 +01:00
|
|
|
return true
|
2016-06-07 09:46:45 +02:00
|
|
|
}
|
|
|
|
|
2021-09-08 00:11:16 +02:00
|
|
|
func (s *staticsServer) serveThemes(w http.ResponseWriter) {
|
2016-06-07 09:46:45 +02:00
|
|
|
sendJSON(w, map[string][]string{
|
|
|
|
"themes": s.availableThemes,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *staticsServer) setTheme(theme string) {
|
|
|
|
s.mut.Lock()
|
|
|
|
s.theme = theme
|
2019-11-08 22:37:42 +01:00
|
|
|
s.lastThemeChange = time.Now().UTC()
|
2016-06-07 09:46:45 +02:00
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *staticsServer) String() string {
|
|
|
|
return fmt.Sprintf("staticsServer@%p", s)
|
|
|
|
}
|