Fix filesystem paths

This commit is contained in:
2024-10-14 22:36:37 +02:00
parent cbdf51db05
commit 4953138154
2 changed files with 46 additions and 52 deletions

View File

@@ -10,8 +10,8 @@ import (
) )
type FileSystem struct { type FileSystem struct {
RootDir string RootDir string
GitRepos map[int]*gitutils.GitRepo GitRepos map[int]map[int]*gitutils.GitRepo // map[userID]map[workspaceID]*gitutils.GitRepo
} }
type FileNode struct { type FileNode struct {
@@ -24,16 +24,16 @@ type FileNode struct {
func New(rootDir string) *FileSystem { func New(rootDir string) *FileSystem {
return &FileSystem{ return &FileSystem{
RootDir: rootDir, RootDir: rootDir,
GitRepos: make(map[int]*gitutils.GitRepo), GitRepos: make(map[int]map[int]*gitutils.GitRepo),
} }
} }
func (fs *FileSystem) GetWorkspacePath(workspaceID int) string { func (fs *FileSystem) GetWorkspacePath(userID, workspaceID int) string {
return filepath.Join(fs.RootDir, fmt.Sprintf("%d", workspaceID)) return filepath.Join(fs.RootDir, fmt.Sprintf("%d", userID), fmt.Sprintf("%d", workspaceID))
} }
func (fs *FileSystem) ValidatePath(workspaceID int, path string) (string, error) { func (fs *FileSystem) ValidatePath(userID, workspaceID int, path string) (string, error) {
workspacePath := fs.GetWorkspacePath(workspaceID) workspacePath := fs.GetWorkspacePath(userID, workspaceID)
fullPath := filepath.Join(workspacePath, path) fullPath := filepath.Join(workspacePath, path)
cleanPath := filepath.Clean(fullPath) cleanPath := filepath.Clean(fullPath)
@@ -44,8 +44,8 @@ func (fs *FileSystem) ValidatePath(workspaceID int, path string) (string, error)
return cleanPath, nil return cleanPath, nil
} }
func (fs *FileSystem) ListFilesRecursively(workspaceID int) ([]FileNode, error) { func (fs *FileSystem) ListFilesRecursively(userID, workspaceID int) ([]FileNode, error) {
workspacePath := fs.GetWorkspacePath(workspaceID) workspacePath := fs.GetWorkspacePath(userID, workspaceID)
return fs.walkDirectory(workspacePath, "") return fs.walkDirectory(workspacePath, "")
} }
@@ -81,9 +81,9 @@ func (fs *FileSystem) walkDirectory(dir, prefix string) ([]FileNode, error) {
return nodes, nil 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 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 { err := filepath.Walk(workspacePath, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
@@ -112,16 +112,16 @@ func (fs *FileSystem) FindFileByName(workspaceID int, filename string) ([]string
return foundPaths, nil return foundPaths, nil
} }
func (fs *FileSystem) GetFileContent(workspaceID int, filePath string) ([]byte, error) { func (fs *FileSystem) GetFileContent(userID, workspaceID int, filePath string) ([]byte, error) {
fullPath, err := fs.ValidatePath(workspaceID, filePath) fullPath, err := fs.ValidatePath(userID, workspaceID, filePath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return os.ReadFile(fullPath) return os.ReadFile(fullPath)
} }
func (fs *FileSystem) SaveFile(workspaceID int, filePath string, content []byte) error { func (fs *FileSystem) SaveFile(userID, workspaceID int, filePath string, content []byte) error {
fullPath, err := fs.ValidatePath(workspaceID, filePath) fullPath, err := fs.ValidatePath(userID, workspaceID, filePath)
if err != nil { if err != nil {
return err return err
} }
@@ -134,31 +134,39 @@ func (fs *FileSystem) SaveFile(workspaceID int, filePath string, content []byte)
return os.WriteFile(fullPath, content, 0644) return os.WriteFile(fullPath, content, 0644)
} }
func (fs *FileSystem) DeleteFile(workspaceID int, filePath string) error { func (fs *FileSystem) DeleteFile(userID, workspaceID int, filePath string) error {
fullPath, err := fs.ValidatePath(workspaceID, filePath) fullPath, err := fs.ValidatePath(userID, workspaceID, filePath)
if err != nil { if err != nil {
return err return err
} }
return os.Remove(fullPath) return os.Remove(fullPath)
} }
func (fs *FileSystem) CreateWorkspaceDirectory(workspaceID int) error { func (fs *FileSystem) CreateWorkspaceDirectory(userID, workspaceID int) error {
dir := fs.GetWorkspacePath(workspaceID) dir := fs.GetWorkspacePath(userID, workspaceID)
return os.MkdirAll(dir, 0755) return os.MkdirAll(dir, 0755)
} }
func (fs *FileSystem) SetupGitRepo(workspaceID int, gitURL, gitUser, gitToken string) error { func (fs *FileSystem) SetupGitRepo(userID, workspaceID int, gitURL, gitUser, gitToken string) error {
workspacePath := fs.GetWorkspacePath(workspaceID) workspacePath := fs.GetWorkspacePath(userID, workspaceID)
fs.GitRepos[workspaceID] = gitutils.New(gitURL, gitUser, gitToken, workspacePath) if _, ok := fs.GitRepos[userID]; !ok {
return fs.GitRepos[workspaceID].EnsureRepo() 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) { func (fs *FileSystem) DisableGitRepo(userID, workspaceID int) {
delete(fs.GitRepos, workspaceID) 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 { func (fs *FileSystem) StageCommitAndPush(userID, workspaceID int, message string) error {
repo, ok := fs.GitRepos[workspaceID] repo, ok := fs.getGitRepo(userID, workspaceID)
if !ok { if !ok {
return errors.New("git settings not configured for this workspace") 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() return repo.Push()
} }
func (fs *FileSystem) Pull(workspaceID int) error { func (fs *FileSystem) Pull(userID, workspaceID int) error {
repo, ok := fs.GitRepos[workspaceID] repo, ok := fs.getGitRepo(userID, workspaceID)
if !ok { if !ok {
return errors.New("git settings not configured for this workspace") return errors.New("git settings not configured for this workspace")
} }
return repo.Pull() 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
}

View File

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