mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-07 20:20:58 -06:00
[server] Specify path with optional root for info and get routes
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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:])
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user