Files
filebrowser/backend/internal/database/activity/paths.go
T
+10 3c02eedca6 Beta/v2.0.0 (#2746)
Co-authored-by: Kevin B. <kurami32_kbm@proton.me>
Co-authored-by: Gabriel Górski <me@glaeqen.eu>
Co-authored-by: Joel Godfrey <82655889+BaccanoMob@users.noreply.github.com>
Co-authored-by: Shane Chiu <71911511+pwn2ooown@users.noreply.github.com>
Co-authored-by: carloda <35763669+carloda@users.noreply.github.com>
Co-authored-by: Taufik Hidayat <taufikdev88@gmail.com>
Co-authored-by: Stephan Paternotte <Stephan-P@users.noreply.github.com>
Co-authored-by: SIDDHU SINGH <siddhu3116@gmail.com>
Co-authored-by: Gaurav Kalal <117557391+Gorupa@users.noreply.github.com>
Co-authored-by: Nebula <40148908+nebula-it@users.noreply.github.com>
Co-authored-by: Juan Soler <juansoler@gmail.com>
Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com>
Co-authored-by: Anatolii Prylutskyi <anpryl@gmail.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Graham Steffaniak <graham@debian-lake.ghome.net>
Co-authored-by: Neil <kylelzzz02@icloud.com>
Co-authored-by: Graham <info@quantumx-apps.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-07 16:24:38 -05:00

104 lines
2.4 KiB
Go

package activity
import (
"strings"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
)
// TrimPathForUserScope removes a user's index scope prefix from a stored path.
// If the path is outside the scope prefix it is returned unchanged.
func TrimPathForUserScope(path, userScope string) string {
if userScope == "/" || userScope == "" {
return path
}
path = strings.TrimSpace(path)
scope := strings.TrimRight(strings.TrimSpace(userScope), "/")
if path == "" || scope == "" || scope == "/" {
return path
}
if !strings.HasPrefix(scope, "/") {
scope = "/" + scope
}
rest, ok := strings.CutPrefix(path, scope)
if !ok {
return path
}
if rest == "" {
return "/"
}
return "/" + strings.TrimPrefix(rest, "/")
}
func scopeForUserSource(user *users.User, source string) (string, bool) {
if user == nil || source == "" {
return "", false
}
if scope, err := user.GetScopeForSourceName(source); err == nil {
return scope, true
}
if scope, err := user.GetScopeForSourcePath(source); err == nil {
return scope, true
}
return "", false
}
func trimPathForUser(user *users.User, source, path string) string {
if path == "" {
return path
}
if source == "" {
return path
}
scope, ok := scopeForUserSource(user, source)
if !ok {
return path
}
return TrimPathForUserScope(path, scope)
}
// TrimPathsForUser strips the user's per-source scope prefix from file paths in the API entry.
// User scope settings in Details.Scopes are left unchanged.
func (fe *FrontendEntry) TrimPathsForUser(user *users.User) {
if user == nil || user.ID == 0 {
return
}
source := fe.Source
if source == "" {
source = fe.Details.Source
}
if fe.Path != "" {
fe.Path = trimPathForUser(user, source, fe.Path)
}
if fe.TargetPath != "" {
fe.TargetPath = trimPathForUser(user, source, fe.TargetPath)
}
if fe.Details.Path != "" {
detailsSource := fe.Details.Source
if detailsSource == "" {
detailsSource = source
}
fe.Details.Path = trimPathForUser(user, detailsSource, fe.Details.Path)
}
if fe.Details.TargetPath != "" {
detailsSource := fe.Details.Source
if detailsSource == "" {
detailsSource = source
}
fe.Details.TargetPath = trimPathForUser(user, detailsSource, fe.Details.TargetPath)
}
if len(fe.Details.Paths) > 0 {
detailsSource := fe.Details.Source
if detailsSource == "" {
detailsSource = source
}
for i, p := range fe.Details.Paths {
fe.Details.Paths[i] = trimPathForUser(user, detailsSource, p)
}
}
}