From 5a7a359c9809946f5d0c84635e357c03acbf833d Mon Sep 17 00:00:00 2001 From: Abhishek Shroff Date: Fri, 9 May 2025 23:53:27 +0530 Subject: [PATCH] [server] Specify path with optional root for info and get routes --- server/internal/api/v1/fs/contents.go | 8 ++------ server/internal/api/v1/fs/info.go | 8 ++------ server/internal/api/v1/fs/routes.go | 4 ++-- server/internal/core/fs/filesystem.go | 4 ++++ server/internal/core/fs/find.go | 20 ++++++++++++++++++++ server/internal/core/fs/fs.go | 1 + 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/server/internal/api/v1/fs/contents.go b/server/internal/api/v1/fs/contents.go index 61f05af5..bf7f6253 100644 --- a/server/internal/api/v1/fs/contents.go +++ b/server/internal/api/v1/fs/contents.go @@ -6,7 +6,6 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/google/uuid" "github.com/shroff/phylum/server/internal/api/authenticator" "github.com/shroff/phylum/server/internal/api/serve" "github.com/shroff/phylum/server/internal/core/errors" @@ -16,13 +15,10 @@ import ( var errGetCollectionContents = errors.NewError(http.StatusBadRequest, "resource_is_collection", "Cannot get contents of collection") func handleContentsRequest(c *gin.Context) { - resourceID, err := uuid.Parse(c.Param("id")) - if err != nil { - panic(errResourceIDInvalid) - } + path := c.Param("path") f := authenticator.GetFileSystem(c) - r, err := f.ResourceByID(resourceID) + r, err := f.ResourceByPathWithMaybeRoot(path) if err != nil { panic(err) } diff --git a/server/internal/api/v1/fs/info.go b/server/internal/api/v1/fs/info.go index deb9d330..0a47e7eb 100644 --- a/server/internal/api/v1/fs/info.go +++ b/server/internal/api/v1/fs/info.go @@ -2,19 +2,15 @@ package fs import ( "github.com/gin-gonic/gin" - "github.com/google/uuid" "github.com/shroff/phylum/server/internal/api/authenticator" "github.com/shroff/phylum/server/internal/api/v1/responses" ) func handleInfoRequest(c *gin.Context) { - resourceID, err := uuid.Parse(c.Param("id")) - if err != nil { - panic(errResourceIDInvalid) - } + path := c.Param("path") fs := authenticator.GetFileSystem(c) - r, err := fs.ResourceByID(resourceID) + r, err := fs.ResourceByPathWithMaybeRoot(path) if err != nil { panic(err) } diff --git a/server/internal/api/v1/fs/routes.go b/server/internal/api/v1/fs/routes.go index c4e72e57..1bc1bb04 100644 --- a/server/internal/api/v1/fs/routes.go +++ b/server/internal/api/v1/fs/routes.go @@ -15,8 +15,8 @@ var ( func SetupRoutes(r *gin.RouterGroup) { group := r.Group("/fs") - group.GET("/contents/:id", handleContentsRequest) - group.GET("/info/:id", handleInfoRequest) + group.GET("/info/*path", handleInfoRequest) + group.GET("/contents/*path", handleContentsRequest) group.GET("/disk_usage/:id", handleDiskUsageRequest) group.POST("/share/:id", handleShareRequest) group.POST("/move/:id", handleMoveRequest) diff --git a/server/internal/core/fs/filesystem.go b/server/internal/core/fs/filesystem.go index b42fe754..70bf26db 100644 --- a/server/internal/core/fs/filesystem.go +++ b/server/internal/core/fs/filesystem.go @@ -29,6 +29,10 @@ func (f filesystem) withDb(db db.Handler) filesystem { } func (f filesystem) WithPathRoot(pathRoot pgtype.UUID) FileSystem { + return f.withPathRoot(pathRoot) +} + +func (f filesystem) withPathRoot(pathRoot pgtype.UUID) filesystem { return filesystem{ db: f.db, cs: f.cs, diff --git a/server/internal/core/fs/find.go b/server/internal/core/fs/find.go index 75d1e60e..cd1779de 100644 --- a/server/internal/core/fs/find.go +++ b/server/internal/core/fs/find.go @@ -1,8 +1,11 @@ package fs import ( + "strings" + "github.com/doug-martin/goqu/v9" "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgtype" ) func (f filesystem) ResourceByPath(path string) (Resource, error) { @@ -73,3 +76,20 @@ func (f filesystem) ResourceByPathOrUUID(pathOrUUID string) (Resource, error) { return f.ResourceByID(id) } } + +func (f filesystem) ResourceByPathWithMaybeRoot(ref string) (Resource, error) { + i := strings.Index(ref, ":") + + if i >= 0 { + if ref[0] == '/' { + ref = ref[1:] + i -= 1 + } + id, err := uuid.Parse(ref[:i]) + if err != nil { + return Resource{}, ErrResourceNotFound + } + f = f.withPathRoot(pgtype.UUID{Bytes: id, Valid: true}) + } + return f.ResourceByPath(ref[i+1:]) +} diff --git a/server/internal/core/fs/fs.go b/server/internal/core/fs/fs.go index 7681f4e3..f47abf42 100644 --- a/server/internal/core/fs/fs.go +++ b/server/internal/core/fs/fs.go @@ -49,6 +49,7 @@ type FileSystem interface { ResourceByID(id uuid.UUID) (Resource, error) ResourceByPath(path string) (Resource, error) ResourceByPathOrUUID(pathOrUUID string) (Resource, error) + ResourceByPathWithMaybeRoot(ref string) (Resource, error) // search.go Search(query string, includeDeleted bool) ([]Resource, error)