From 495313815482990f15b3cd582d6dcbfb5e0d73ff Mon Sep 17 00:00:00 2001 From: LordMathis Date: Mon, 14 Oct 2024 22:36:37 +0200 Subject: [PATCH] Fix filesystem paths --- backend/internal/filesystem/filesystem.go | 75 ++++++++++++++--------- backend/internal/filesystem/paths.go | 23 ------- 2 files changed, 46 insertions(+), 52 deletions(-) delete mode 100644 backend/internal/filesystem/paths.go diff --git a/backend/internal/filesystem/filesystem.go b/backend/internal/filesystem/filesystem.go index 8fe6985..3007f9d 100644 --- a/backend/internal/filesystem/filesystem.go +++ b/backend/internal/filesystem/filesystem.go @@ -10,8 +10,8 @@ import ( ) type FileSystem struct { - RootDir string - GitRepos map[int]*gitutils.GitRepo + RootDir string + GitRepos map[int]map[int]*gitutils.GitRepo // map[userID]map[workspaceID]*gitutils.GitRepo } type FileNode struct { @@ -24,16 +24,16 @@ type FileNode struct { func New(rootDir string) *FileSystem { return &FileSystem{ RootDir: rootDir, - GitRepos: make(map[int]*gitutils.GitRepo), + GitRepos: make(map[int]map[int]*gitutils.GitRepo), } } -func (fs *FileSystem) GetWorkspacePath(workspaceID int) string { - return filepath.Join(fs.RootDir, fmt.Sprintf("%d", workspaceID)) +func (fs *FileSystem) GetWorkspacePath(userID, workspaceID int) string { + return filepath.Join(fs.RootDir, fmt.Sprintf("%d", userID), fmt.Sprintf("%d", workspaceID)) } -func (fs *FileSystem) ValidatePath(workspaceID int, path string) (string, error) { - workspacePath := fs.GetWorkspacePath(workspaceID) +func (fs *FileSystem) ValidatePath(userID, workspaceID int, path string) (string, error) { + workspacePath := fs.GetWorkspacePath(userID, workspaceID) fullPath := filepath.Join(workspacePath, path) cleanPath := filepath.Clean(fullPath) @@ -44,8 +44,8 @@ func (fs *FileSystem) ValidatePath(workspaceID int, path string) (string, error) return cleanPath, nil } -func (fs *FileSystem) ListFilesRecursively(workspaceID int) ([]FileNode, error) { - workspacePath := fs.GetWorkspacePath(workspaceID) +func (fs *FileSystem) ListFilesRecursively(userID, workspaceID int) ([]FileNode, error) { + workspacePath := fs.GetWorkspacePath(userID, workspaceID) return fs.walkDirectory(workspacePath, "") } @@ -81,9 +81,9 @@ func (fs *FileSystem) walkDirectory(dir, prefix string) ([]FileNode, error) { return nodes, nil } -func (fs *FileSystem) FindFileByName(workspaceID int, filename string) ([]string, error) { +func (fs *FileSystem) FindFileByName(userID, workspaceID int, filename string) ([]string, error) { var foundPaths []string - workspacePath := fs.GetWorkspacePath(workspaceID) + workspacePath := fs.GetWorkspacePath(userID, workspaceID) err := filepath.Walk(workspacePath, func(path string, info os.FileInfo, err error) error { if err != nil { @@ -112,16 +112,16 @@ func (fs *FileSystem) FindFileByName(workspaceID int, filename string) ([]string return foundPaths, nil } -func (fs *FileSystem) GetFileContent(workspaceID int, filePath string) ([]byte, error) { - fullPath, err := fs.ValidatePath(workspaceID, filePath) +func (fs *FileSystem) GetFileContent(userID, workspaceID int, filePath string) ([]byte, error) { + fullPath, err := fs.ValidatePath(userID, workspaceID, filePath) if err != nil { return nil, err } return os.ReadFile(fullPath) } -func (fs *FileSystem) SaveFile(workspaceID int, filePath string, content []byte) error { - fullPath, err := fs.ValidatePath(workspaceID, filePath) +func (fs *FileSystem) SaveFile(userID, workspaceID int, filePath string, content []byte) error { + fullPath, err := fs.ValidatePath(userID, workspaceID, filePath) if err != nil { return err } @@ -134,31 +134,39 @@ func (fs *FileSystem) SaveFile(workspaceID int, filePath string, content []byte) return os.WriteFile(fullPath, content, 0644) } -func (fs *FileSystem) DeleteFile(workspaceID int, filePath string) error { - fullPath, err := fs.ValidatePath(workspaceID, filePath) +func (fs *FileSystem) DeleteFile(userID, workspaceID int, filePath string) error { + fullPath, err := fs.ValidatePath(userID, workspaceID, filePath) if err != nil { return err } return os.Remove(fullPath) } -func (fs *FileSystem) CreateWorkspaceDirectory(workspaceID int) error { - dir := fs.GetWorkspacePath(workspaceID) +func (fs *FileSystem) CreateWorkspaceDirectory(userID, workspaceID int) error { + dir := fs.GetWorkspacePath(userID, workspaceID) return os.MkdirAll(dir, 0755) } -func (fs *FileSystem) SetupGitRepo(workspaceID int, gitURL, gitUser, gitToken string) error { - workspacePath := fs.GetWorkspacePath(workspaceID) - fs.GitRepos[workspaceID] = gitutils.New(gitURL, gitUser, gitToken, workspacePath) - return fs.GitRepos[workspaceID].EnsureRepo() +func (fs *FileSystem) SetupGitRepo(userID, workspaceID int, gitURL, gitUser, gitToken string) error { + workspacePath := fs.GetWorkspacePath(userID, workspaceID) + if _, ok := fs.GitRepos[userID]; !ok { + fs.GitRepos[userID] = make(map[int]*gitutils.GitRepo) + } + fs.GitRepos[userID][workspaceID] = gitutils.New(gitURL, gitUser, gitToken, workspacePath) + return fs.GitRepos[userID][workspaceID].EnsureRepo() } -func (fs *FileSystem) DisableGitRepo(workspaceID int) { - delete(fs.GitRepos, workspaceID) +func (fs *FileSystem) DisableGitRepo(userID, workspaceID int) { + if userRepos, ok := fs.GitRepos[userID]; ok { + delete(userRepos, workspaceID) + if len(userRepos) == 0 { + delete(fs.GitRepos, userID) + } + } } -func (fs *FileSystem) StageCommitAndPush(workspaceID int, message string) error { - repo, ok := fs.GitRepos[workspaceID] +func (fs *FileSystem) StageCommitAndPush(userID, workspaceID int, message string) error { + repo, ok := fs.getGitRepo(userID, workspaceID) if !ok { return errors.New("git settings not configured for this workspace") } @@ -170,11 +178,20 @@ func (fs *FileSystem) StageCommitAndPush(workspaceID int, message string) error return repo.Push() } -func (fs *FileSystem) Pull(workspaceID int) error { - repo, ok := fs.GitRepos[workspaceID] +func (fs *FileSystem) Pull(userID, workspaceID int) error { + repo, ok := fs.getGitRepo(userID, workspaceID) if !ok { return errors.New("git settings not configured for this workspace") } return repo.Pull() +} + +func (fs *FileSystem) getGitRepo(userID, workspaceID int) (*gitutils.GitRepo, bool) { + userRepos, ok := fs.GitRepos[userID] + if !ok { + return nil, false + } + repo, ok := userRepos[workspaceID] + return repo, ok } \ No newline at end of file diff --git a/backend/internal/filesystem/paths.go b/backend/internal/filesystem/paths.go deleted file mode 100644 index 91cb6fd..0000000 --- a/backend/internal/filesystem/paths.go +++ /dev/null @@ -1,23 +0,0 @@ -package filesystem - -import ( - "fmt" - "os" - "path/filepath" - - "novamd/internal/models" -) - -// GetWorkspacePath returns the file system path for a given workspace -func GetWorkspacePath(workspace *models.Workspace) string { - baseDir := os.Getenv("NOVAMD_WORKDIR") - if baseDir == "" { - baseDir = "./data" // Default if not set - } - return filepath.Join(baseDir, fmt.Sprintf("%d", workspace.UserID), workspace.Name) -} - -// GetFilePath returns the file system path for a given file within a workspace -func GetFilePath(workspace *models.Workspace, relativeFilePath string) string { - return filepath.Join(GetWorkspacePath(workspace), relativeFilePath) -}