80 lines
2.1 KiB
Go
80 lines
2.1 KiB
Go
package service
|
|
|
|
import (
|
|
"codespace/internal/fs"
|
|
"codespace/internal/workspace"
|
|
)
|
|
|
|
// FileService handles file operations within workspaces.
|
|
type FileService struct {
|
|
workspaces workspace.Manager
|
|
}
|
|
|
|
// NewFileService creates a FileService.
|
|
func NewFileService(workspaces workspace.Manager) *FileService {
|
|
return &FileService{workspaces: workspaces}
|
|
}
|
|
|
|
// List lists files at the given workspace-relative path.
|
|
func (s *FileService) List(workspaceID, path string) ([]fs.FileInfo, error) {
|
|
ws, err := s.workspaces.Get(workspaceID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return fs.NewLocal(ws.Root).List(path)
|
|
}
|
|
|
|
// Read reads a file at the given workspace-relative path.
|
|
func (s *FileService) Read(workspaceID, path string) ([]byte, error) {
|
|
ws, err := s.workspaces.Get(workspaceID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return fs.NewLocal(ws.Root).Read(path)
|
|
}
|
|
|
|
// Write writes data to a file at the given workspace-relative path.
|
|
func (s *FileService) Write(workspaceID, path string, data []byte) error {
|
|
ws, err := s.workspaces.Get(workspaceID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return fs.NewLocal(ws.Root).Write(path, data)
|
|
}
|
|
|
|
// Mkdir creates a directory at the given workspace-relative path.
|
|
func (s *FileService) Mkdir(workspaceID, path string) error {
|
|
ws, err := s.workspaces.Get(workspaceID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return fs.NewLocal(ws.Root).Mkdir(path)
|
|
}
|
|
|
|
// Remove removes a file or directory at the given workspace-relative path.
|
|
func (s *FileService) Remove(workspaceID, path string) error {
|
|
ws, err := s.workspaces.Get(workspaceID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return fs.NewLocal(ws.Root).Remove(path)
|
|
}
|
|
|
|
// Rename renames a file or directory from oldPath to newPath.
|
|
func (s *FileService) Rename(workspaceID, oldPath, newPath string) error {
|
|
ws, err := s.workspaces.Get(workspaceID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return fs.NewLocal(ws.Root).Rename(oldPath, newPath)
|
|
}
|
|
|
|
// Stat returns file info for the given workspace-relative path.
|
|
func (s *FileService) Stat(workspaceID, path string) (*fs.FileInfo, error) {
|
|
ws, err := s.workspaces.Get(workspaceID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return fs.NewLocal(ws.Root).Stat(path)
|
|
}
|