[server] Allow filesystems without specified root

This commit is contained in:
Abhishek Shroff
2025-05-09 10:53:01 +05:30
parent e327152fa8
commit 05da73175f
4 changed files with 12 additions and 32 deletions
+3 -21
View File
@@ -1,7 +1,7 @@
package fs
import (
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/shroff/phylum/server/internal/core/db"
"github.com/shroff/phylum/server/internal/core/storage"
)
@@ -9,29 +9,11 @@ import (
type filesystem struct {
db db.Handler
cs storage.Storage
rootID uuid.UUID
pathRoot pgtype.UUID
username string
fullAccess bool
}
func (f filesystem) RootID() uuid.UUID {
return f.rootID
}
func (f filesystem) WithRoot(id uuid.UUID) FileSystem {
return f.withRoot(id)
}
func (f filesystem) withRoot(id uuid.UUID) filesystem {
return filesystem{
db: f.db,
cs: f.cs,
rootID: id,
username: f.username,
fullAccess: f.fullAccess,
}
}
func (f filesystem) WithDb(db db.Handler) FileSystem {
return f.withDb(db)
}
@@ -40,7 +22,7 @@ func (f filesystem) withDb(db db.Handler) filesystem {
return filesystem{
db: db,
cs: f.cs,
rootID: f.rootID,
pathRoot: f.pathRoot,
username: f.username,
fullAccess: f.fullAccess,
}
+4 -1
View File
@@ -6,6 +6,9 @@ import (
)
func (f filesystem) ResourceByPath(path string) (Resource, error) {
if !f.pathRoot.Valid {
return Resource{}, ErrResourceNotFound
}
nodes := goqu.T("nodes").As("n")
resources := goqu.T("resources").As("r")
sub := pg.
@@ -20,7 +23,7 @@ func (f filesystem) ResourceByPath(path string) (Resource, error) {
rec := pg.
Select(resources.Col("id"), resources.Col("parent"), goqu.L("array_remove(string_to_array(?::TEXT, '/', NULL), '')", path), goqu.L("0")).
From(resources).
Where(resources.Col("id").Eq(goqu.V(f.rootID))).
Where(resources.Col("id").Eq(goqu.V(f.pathRoot))).
UnionAll(sub)
l := goqu.T("publinks").As("l")
+4 -6
View File
@@ -36,8 +36,6 @@ func init() {
type FileSystem interface {
// filesystem.go
RootID() uuid.UUID
WithRoot(uuid.UUID) FileSystem
WithDb(db db.Handler) FileSystem
RunInTx(fn func(FileSystem) error) error
@@ -62,12 +60,12 @@ type FileSystem interface {
TrashEmpty() (int, error)
}
func Open(ctx context.Context, username string, root uuid.UUID, fullAccess bool) FileSystem {
func Open(ctx context.Context, username string, root pgtype.UUID, fullAccess bool) FileSystem {
return filesystem{
db: db.Get(ctx),
cs: storage.Get(),
username: username,
rootID: root,
pathRoot: root,
fullAccess: fullAccess,
}
}
@@ -77,7 +75,7 @@ func OpenOmniscient(ctx context.Context) FileSystem {
if err != nil {
logrus.Fatal("could not read root id: " + err.Error())
}
return Open(ctx, "", id, true)
return Open(ctx, "", pgtype.UUID{Bytes: id, Valid: true}, true)
}
func _readRootID(ctx context.Context) (uuid.UUID, error) {
@@ -135,7 +133,7 @@ func OpenFromPublink(ctx context.Context, id string, password string, path strin
f := filesystem{
db: d,
cs: storage.Get(),
rootID: link.Root,
pathRoot: pgtype.UUID{Bytes: link.Root, Valid: true},
fullAccess: true, // TODO: #permissions Replace with permissions int
}
+1 -4
View File
@@ -44,10 +44,7 @@ func scanUser(row pgx.CollectableRow) (User, error) {
}
func (u User) OpenFileSystem(ctx context.Context, id pgtype.UUID) fs.FileSystem {
if !id.Valid {
return nil
}
return fs.Open(ctx, u.Username, id.Bytes, u.Permissions&PermissionAllFiles != 0)
return fs.Open(ctx, u.Username, id, u.Permissions&PermissionAllFiles != 0)
}
func (u User) OpenHomeFileSystem(ctx context.Context) fs.FileSystem {