mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-04 18:50:42 -06:00
[server][core] Remove App.RootFS
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/shroff/phylum/server/internal/core/db"
|
||||
"github.com/shroff/phylum/server/internal/core/fs"
|
||||
"github.com/shroff/phylum/server/internal/core/storage"
|
||||
"github.com/shroff/phylum/server/internal/core/user"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@@ -57,6 +58,18 @@ func App() *app.App {
|
||||
return a
|
||||
}
|
||||
|
||||
func RootFileSystem() fs.FileSystem {
|
||||
var rootUser user.User
|
||||
if user, err := App().OpenUserManager(context.Background()).UserByEmail(user.DefaultUserUsername); err != nil {
|
||||
fmt.Println("unable to find root user: " + err.Error())
|
||||
os.Exit(1)
|
||||
} else {
|
||||
rootUser = user
|
||||
}
|
||||
|
||||
return App().OpenFileSystem(context.Background(), rootUser)
|
||||
}
|
||||
|
||||
func UserFileSystem(cmd *cobra.Command) fs.FileSystem {
|
||||
if f == nil {
|
||||
if value, err := cmd.Flags().GetString("user"); err != nil {
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"syscall"
|
||||
|
||||
"github.com/shroff/phylum/server/internal/command/common"
|
||||
"github.com/shroff/phylum/server/internal/core/app"
|
||||
"github.com/shroff/phylum/server/internal/core/db"
|
||||
"github.com/shroff/phylum/server/internal/core/fs"
|
||||
"github.com/shroff/phylum/server/internal/core/user"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -59,12 +59,14 @@ func setupUserAddCommand() *cobra.Command {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
root := common.App().Rootfs.RootID()
|
||||
|
||||
rootFS := common.RootFileSystem()
|
||||
root := rootFS.RootID()
|
||||
if rootPathOrUUID, err := cmd.Flags().GetString("chroot"); err != nil {
|
||||
fmt.Println("invalid value for flag 'chroot': " + err.Error())
|
||||
os.Exit(2)
|
||||
} else if rootPathOrUUID != "" {
|
||||
if r, err := common.App().Rootfs.ResourceByPathOrUUID(rootPathOrUUID); err != nil {
|
||||
if r, err := rootFS.ResourceByPathOrUUID(rootPathOrUUID); err != nil {
|
||||
fmt.Println("invalid value for flag 'chroot': " + err.Error())
|
||||
os.Exit(1)
|
||||
} else {
|
||||
@@ -93,8 +95,8 @@ func setupUserAddCommand() *cobra.Command {
|
||||
fmt.Println(homePath)
|
||||
|
||||
ctx := context.Background()
|
||||
err = common.App().RunInTx(ctx, func(a app.App) error {
|
||||
userManager := a.OpenUserManager(ctx)
|
||||
err = common.App().RunInTx(ctx, func(db *db.DbHandler) error {
|
||||
userManager := common.App().OpenUserManager(ctx).WithDb(db)
|
||||
var u user.User
|
||||
if user, err := userManager.CreateUser(username, displayName, password, root); err != nil {
|
||||
return err
|
||||
@@ -103,12 +105,12 @@ func setupUserAddCommand() *cobra.Command {
|
||||
}
|
||||
|
||||
if homePath != "" {
|
||||
f := a.Rootfs.WithRoot(root)
|
||||
home, err := f.CreateResourceByPath(homePath, true, true)
|
||||
rootFS = rootFS.WithDb(db)
|
||||
home, err := rootFS.CreateResourceByPath(homePath, true, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := f.UpdatePermissions(home, u.Username, fs.PermissionReadWriteShare); err != nil {
|
||||
if _, err := rootFS.UpdatePermissions(home, u.Username, fs.PermissionReadWriteShare); err != nil {
|
||||
return err
|
||||
}
|
||||
return userManager.UpdateUserHome(u, home.ID)
|
||||
|
||||
@@ -39,7 +39,7 @@ func setupUserChrootCommand() *cobra.Command {
|
||||
if user, err := userManager.UserByEmail(email); err != nil {
|
||||
logrus.Fatal(err)
|
||||
} else {
|
||||
if res, err := common.App().Rootfs.ResourceByPathOrUUID(idOrPath); err != nil {
|
||||
if res, err := common.RootFileSystem().ResourceByPathOrUUID(idOrPath); err != nil {
|
||||
logrus.Fatal(err)
|
||||
} else {
|
||||
userManager.UpdateUserRoot(user, res.ID)
|
||||
|
||||
@@ -11,13 +11,9 @@ import (
|
||||
"github.com/shroff/phylum/server/internal/core/user"
|
||||
)
|
||||
|
||||
const defaultUserUsername = "phylum"
|
||||
const defaultUserDisplayName = "Phylum"
|
||||
|
||||
type App struct {
|
||||
Rootfs fs.FileSystem
|
||||
db *db.DbHandler
|
||||
cs storage.Storage
|
||||
db *db.DbHandler
|
||||
cs storage.Storage
|
||||
}
|
||||
|
||||
func Create(ctx context.Context, db *db.DbHandler, cs storage.Storage) (*App, error) {
|
||||
@@ -26,30 +22,28 @@ func Create(ctx context.Context, db *db.DbHandler, cs storage.Storage) (*App, er
|
||||
cs: cs,
|
||||
}
|
||||
userManager := user.CreateManager(ctx, db)
|
||||
u, err := userManager.UserByEmail(defaultUserUsername)
|
||||
if _, err := userManager.UserByEmail(user.DefaultUserUsername); err != nil {
|
||||
if errors.Is(err, user.ErrNotFound) {
|
||||
_, err = app.populateData(ctx)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if errors.Is(err, user.ErrNotFound) {
|
||||
u, err = app.populateData(ctx)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
app.Rootfs = fs.OpenFileSystem(db, ctx, cs, u.Username, u.Root)
|
||||
return app, nil
|
||||
}
|
||||
|
||||
func (a App) WithDb(db *db.DbHandler) App {
|
||||
return App{
|
||||
db: db,
|
||||
Rootfs: a.Rootfs.WithDb(db),
|
||||
cs: a.cs.WithDb(db),
|
||||
}
|
||||
func (a App) RunInTx(ctx context.Context, fn func(*db.DbHandler) error) error {
|
||||
return a.db.WithTx(ctx, fn)
|
||||
}
|
||||
|
||||
func (a App) RunInTx(ctx context.Context, fn func(App) error) error {
|
||||
return a.db.WithTx(ctx, func(db *db.DbHandler) error {
|
||||
return fn(a.WithDb(db))
|
||||
})
|
||||
func (a App) OpenFileSystem(ctx context.Context, u user.User) fs.FileSystem {
|
||||
return fs.OpenFileSystem(a.db, ctx, a.cs, u.Username, u.Root)
|
||||
}
|
||||
|
||||
func (a App) OpenUserManager(ctx context.Context) user.Manager {
|
||||
return user.CreateManager(ctx, a.db)
|
||||
}
|
||||
|
||||
func (a *App) populateData(ctx context.Context) (u user.User, e error) {
|
||||
@@ -81,7 +75,7 @@ func (a *App) populateData(ctx context.Context) (u user.User, e error) {
|
||||
userHome, err := dbh.CreateResource(ctx, db.CreateResourceParams{
|
||||
ID: uuid.New(),
|
||||
Parent: &home.ID,
|
||||
Name: defaultUserUsername,
|
||||
Name: user.DefaultUserUsername,
|
||||
Dir: true,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -90,8 +84,8 @@ func (a *App) populateData(ctx context.Context) (u user.User, e error) {
|
||||
|
||||
// Create phylum user
|
||||
u, err := dbh.CreateUser(ctx, db.CreateUserParams{
|
||||
Username: defaultUserUsername,
|
||||
DisplayName: defaultUserDisplayName,
|
||||
Username: user.DefaultUserUsername,
|
||||
DisplayName: user.DefaultUserDisplayName,
|
||||
PasswordHash: "CANNOT LOG IN",
|
||||
Root: root.ID,
|
||||
Home: userHome.ID,
|
||||
@@ -115,11 +109,3 @@ func (a *App) populateData(ctx context.Context) (u user.User, e error) {
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (a App) OpenFileSystem(ctx context.Context, u user.User) fs.FileSystem {
|
||||
return fs.OpenFileSystem(a.db, ctx, a.cs, u.Username, u.Root)
|
||||
}
|
||||
|
||||
func (a App) OpenUserManager(ctx context.Context) user.Manager {
|
||||
return user.CreateManager(ctx, a.db)
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
)
|
||||
|
||||
type Storage interface {
|
||||
WithDb(db *db.DbHandler) Storage
|
||||
CreateBackend(ctx context.Context, name string, driver string, params map[string]string) error
|
||||
ListBackends() map[string]Backend
|
||||
OpenRead(id uuid.UUID, start, length int64) (io.ReadCloser, error)
|
||||
@@ -42,18 +41,6 @@ func Create(ctx context.Context, db *db.DbHandler, defaultStorageDir string) (St
|
||||
}
|
||||
}
|
||||
|
||||
func (s storage) WithDb(db *db.DbHandler) Storage {
|
||||
return s.withDb(db)
|
||||
}
|
||||
|
||||
func (s storage) withDb(db *db.DbHandler) storage {
|
||||
return storage{
|
||||
db: db,
|
||||
backends: s.backends,
|
||||
defaultBackend: s.defaultBackend,
|
||||
}
|
||||
}
|
||||
|
||||
func (s storage) OpenRead(id uuid.UUID, start, length int64) (io.ReadCloser, error) {
|
||||
if backend, err := s.findStorageBackend(id); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -8,6 +8,9 @@ import (
|
||||
"github.com/shroff/phylum/server/internal/core/fs"
|
||||
)
|
||||
|
||||
const DefaultUserUsername = "phylum"
|
||||
const DefaultUserDisplayName = "Phylum"
|
||||
|
||||
type User struct {
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display"`
|
||||
|
||||
Reference in New Issue
Block a user