[server] Specify path with optional root for info and get routes

This commit is contained in:
Abhishek Shroff
2025-05-09 23:53:27 +05:30
parent 71f7c9565f
commit 5a7a359c98
6 changed files with 31 additions and 14 deletions

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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,

View File

@@ -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:])
}

View File

@@ -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)