stash/pkg/session/local.go
1509x 9a1b1fb718
[Feature] Reveal file in system file manager from file info panel (#6587)
* Add reveal in file manager button to file info panel

Adds a folder icon button next to the path field in the Scene, Image,
and Gallery file info panels. Clicking it calls a new GraphQL mutation
that opens the file's enclosing directory in the system file manager
(Finder on macOS, Explorer on Windows, xdg-open on Linux).

Also fixes the existing revealInFileManager implementations which were
constructing exec.Command but never calling Run(), making them no-ops:
- darwin: add Run() to open -R
- windows: add Run() and fix flag from \select to /select,<path>
- linux: implement with xdg-open on the parent directory
- desktop.go: use os.Stat instead of FileExists so folders work too

* Disallow reveal operation if request not from loopback
---------
Co-authored-by: 1509x <1509x@users.noreply.github.com>
Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com>
2026-02-23 12:51:35 +11:00

44 lines
1.1 KiB
Go

package session
import (
"context"
"net"
"net/http"
"github.com/stashapp/stash/pkg/logger"
)
// SetLocalRequest checks if the request is from localhost and sets the context value accordingly.
// It returns the modified request with the updated context, or the original request if it did
// not come from localhost or if there was an error parsing the remote address.
func SetLocalRequest(r *http.Request) *http.Request {
// determine if request is from localhost
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
logger.Errorf("Error parsing remote address: %v", err)
return r
}
ip := net.ParseIP(host)
if ip == nil {
logger.Errorf("Error parsing IP address: %s", host)
return r
}
if ip.IsLoopback() {
ctx := context.WithValue(r.Context(), contextLocalRequest, true)
r = r.WithContext(ctx)
}
return r
}
// IsLocalRequest returns true if the request is from localhost, as determined by the context value set by SetLocalRequest.
// If the context value is not set, it returns false.
func IsLocalRequest(ctx context.Context) bool {
val := ctx.Value(contextLocalRequest)
if val == nil {
return false
}
return val.(bool)
}