mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 12:24:38 +03:00
Handle index.html correctly in custom served folders (#3168)
* getStringMapString return nil if key not found * Refactor custom routes. Handle /index.html
This commit is contained in:
35
internal/api/routes_custom.go
Normal file
35
internal/api/routes_custom.go
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
"github.com/stashapp/stash/internal/manager/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
type customRoutes struct {
|
||||||
|
servedFolders config.URLMap
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rs customRoutes) Routes() chi.Router {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
|
||||||
|
r.HandleFunc("/*", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
r.URL.Path = strings.Replace(r.URL.Path, "/custom", "", 1)
|
||||||
|
|
||||||
|
// http.FileServer redirects to / if the path ends with index.html
|
||||||
|
r.URL.Path = strings.TrimSuffix(r.URL.Path, "/index.html")
|
||||||
|
|
||||||
|
// map the path to the applicable filesystem location
|
||||||
|
var dir string
|
||||||
|
r.URL.Path, dir = rs.servedFolders.GetFilesystemLocation(r.URL.Path)
|
||||||
|
if dir != "" {
|
||||||
|
http.FileServer(http.Dir(dir)).ServeHTTP(w, r)
|
||||||
|
} else {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
@@ -231,18 +231,9 @@ func Start() error {
|
|||||||
// Serve static folders
|
// Serve static folders
|
||||||
customServedFolders := c.GetCustomServedFolders()
|
customServedFolders := c.GetCustomServedFolders()
|
||||||
if customServedFolders != nil {
|
if customServedFolders != nil {
|
||||||
r.HandleFunc("/custom/*", func(w http.ResponseWriter, r *http.Request) {
|
r.Mount("/custom", customRoutes{
|
||||||
r.URL.Path = strings.Replace(r.URL.Path, "/custom", "", 1)
|
servedFolders: customServedFolders,
|
||||||
|
}.Routes())
|
||||||
// map the path to the applicable filesystem location
|
|
||||||
var dir string
|
|
||||||
r.URL.Path, dir = customServedFolders.GetFilesystemLocation(r.URL.Path)
|
|
||||||
if dir != "" {
|
|
||||||
http.FileServer(http.Dir(dir)).ServeHTTP(w, r)
|
|
||||||
} else {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
customUILocation := c.GetCustomUILocation()
|
customUILocation := c.GetCustomUILocation()
|
||||||
|
|||||||
@@ -466,7 +466,15 @@ func (i *Instance) getStringMapString(key string) map[string]string {
|
|||||||
i.RLock()
|
i.RLock()
|
||||||
defer i.RUnlock()
|
defer i.RUnlock()
|
||||||
|
|
||||||
return i.viper(key).GetStringMapString(key)
|
ret := i.viper(key).GetStringMapString(key)
|
||||||
|
|
||||||
|
// GetStringMapString returns an empty map regardless of whether the
|
||||||
|
// key exists or not.
|
||||||
|
if len(ret) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
type StashConfig struct {
|
type StashConfig struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user