114 lines
3.2 KiB
Go
114 lines
3.2 KiB
Go
package server
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"math/big"
|
|
"mime"
|
|
"net/http"
|
|
pathpkg "path"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
const defaultHomeWallpaperOpacity = 28
|
|
|
|
type homeWallpaperView struct {
|
|
Path string
|
|
URL string
|
|
Opacity string
|
|
Key string
|
|
}
|
|
|
|
// Legacy route retained as a deliberate 404. Home wallpapers are never
|
|
// published to unauthenticated clients through the sign-in screen.
|
|
func (s *Server) handleAuthWallpaper(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
|
w.Header().Set("Allow", "GET, HEAD")
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
w.Header().Set("Cache-Control", "private, no-store")
|
|
http.NotFound(w, r)
|
|
}
|
|
|
|
func normalizePickerPath(raw string) (string, bool) {
|
|
raw = strings.TrimSpace(raw)
|
|
if raw == "" {
|
|
return "", true
|
|
}
|
|
if len(raw) > 4096 || !strings.HasPrefix(raw, "/") || strings.ContainsAny(raw, "\x00\r\n\\") {
|
|
return "", false
|
|
}
|
|
for _, part := range strings.Split(raw, "/") {
|
|
if part == "." || part == ".." {
|
|
return "", false
|
|
}
|
|
}
|
|
clean := pathpkg.Clean(raw)
|
|
if clean == "." || !strings.HasPrefix(clean, "/") {
|
|
return "", false
|
|
}
|
|
return clean, true
|
|
}
|
|
|
|
func imageContentType(raw string) bool {
|
|
mediaType, _, err := mime.ParseMediaType(raw)
|
|
return err == nil && strings.HasPrefix(strings.ToLower(mediaType), "image/")
|
|
}
|
|
|
|
func (s *Server) homeWallpaperConfig(domainUUID string) (string, int) {
|
|
path, ok := normalizePickerPath(s.getDomainConfig("home_wallpaper_path", domainUUID))
|
|
if !ok {
|
|
path = ""
|
|
}
|
|
opacity := defaultHomeWallpaperOpacity
|
|
if raw := strings.TrimSpace(s.getDomainConfig("home_wallpaper_opacity", domainUUID)); raw != "" {
|
|
if parsed, err := strconv.Atoi(raw); err == nil && parsed >= 0 && parsed <= 100 {
|
|
opacity = parsed
|
|
}
|
|
}
|
|
return path, opacity
|
|
}
|
|
|
|
func (s *Server) homeWallpaperForRequest(r *http.Request) homeWallpaperView {
|
|
domainUUID := s.domainUUIDForRequest(r)
|
|
p := s.appearanceForRequest(r)
|
|
configured, opacity := p.Path, p.Opacity
|
|
view := homeWallpaperView{Path: configured, Opacity: strconv.FormatFloat(float64(opacity)/100, 'f', 2, 64)}
|
|
if configured == "" {
|
|
return view
|
|
}
|
|
|
|
httpRequest := r
|
|
if objectUUID, err := s.store.ResolvePathInDomain(domainUUID, configured); err == nil && s.requestCanReadPath(httpRequest, configured) {
|
|
if revision, err := s.latestRevisionMetadataForRequest(httpRequest, objectUUID); err == nil && imageContentType(revision.ContentType) {
|
|
view.URL = rawProjectionURL("rev:" + revision.UUID)
|
|
view.Key = revision.DocumentHash
|
|
return view
|
|
}
|
|
}
|
|
|
|
entries, err := s.store.ListBrowserEntriesInDomain(domainUUID, configured)
|
|
if err != nil {
|
|
return view
|
|
}
|
|
candidates := make([]store.BrowserEntry, 0, len(entries))
|
|
for _, entry := range entries {
|
|
if !entry.IsDir && imageContentType(entry.ContentType) && s.requestCanReadPath(httpRequest, entry.Path) {
|
|
candidates = append(candidates, entry)
|
|
}
|
|
}
|
|
if len(candidates) == 0 {
|
|
return view
|
|
}
|
|
index := 0
|
|
if value, err := rand.Int(rand.Reader, big.NewInt(int64(len(candidates)))); err == nil {
|
|
index = int(value.Int64())
|
|
}
|
|
view.URL = rawProjectionURL("rev:" + candidates[index].RevisionUUID)
|
|
view.Key = candidates[index].DocumentHash
|
|
return view
|
|
}
|