mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-07 00:14:25 +00:00
Refactor filesystem to make it testable
This commit is contained in:
@@ -2,28 +2,65 @@ package filesystem
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"novamd/internal/gitutils"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// FileSystem represents the file system structure.
|
||||
type FileSystem struct {
|
||||
// fileSystem defines the interface for filesystem operations
|
||||
type fileSystem interface {
|
||||
ReadFile(path string) ([]byte, error)
|
||||
WriteFile(path string, data []byte, perm fs.FileMode) error
|
||||
Remove(path string) error
|
||||
MkdirAll(path string, perm fs.FileMode) error
|
||||
RemoveAll(path string) error
|
||||
ReadDir(path string) ([]fs.DirEntry, error)
|
||||
Stat(path string) (fs.FileInfo, error)
|
||||
IsNotExist(err error) bool
|
||||
}
|
||||
|
||||
// Storage represents the file system structure.
|
||||
type Storage struct {
|
||||
fs fileSystem
|
||||
RootDir string
|
||||
GitRepos map[int]map[int]*gitutils.GitRepo // map[userID]map[workspaceID]*gitutils.GitRepo
|
||||
}
|
||||
|
||||
// New creates a new FileSystem instance.
|
||||
func New(rootDir string) *FileSystem {
|
||||
return &FileSystem{
|
||||
// New creates a new Storage instance.
|
||||
// Parameters:
|
||||
// - rootDir: the root directory for the storage
|
||||
// Returns:
|
||||
// - result: the new Storage instance
|
||||
func New(rootDir string) *Storage {
|
||||
return NewWithFS(rootDir, &osFS{})
|
||||
}
|
||||
|
||||
// NewWithFS creates a new Storage instance with the given filesystem.
|
||||
// Parameters:
|
||||
// - rootDir: the root directory for the storage
|
||||
// - fs: the filesystem implementation to use
|
||||
// Returns:
|
||||
// - result: the new Storage instance
|
||||
func NewWithFS(rootDir string, fs fileSystem) *Storage {
|
||||
return &Storage{
|
||||
fs: fs,
|
||||
RootDir: rootDir,
|
||||
GitRepos: make(map[int]map[int]*gitutils.GitRepo),
|
||||
}
|
||||
}
|
||||
|
||||
// ValidatePath validates the given path and returns the cleaned path if it is valid.
|
||||
func (fs *FileSystem) ValidatePath(userID, workspaceID int, path string) (string, error) {
|
||||
workspacePath := fs.GetWorkspacePath(userID, workspaceID)
|
||||
// Parameters:
|
||||
// - userID: the ID of the user who owns the workspace
|
||||
// - workspaceID: the ID of the workspace to validate the path for
|
||||
// - path: the path to validate
|
||||
// Returns:
|
||||
// - result: the cleaned path if it is valid
|
||||
// - error: any error that occurred during validation
|
||||
func (s *Storage) ValidatePath(userID, workspaceID int, path string) (string, error) {
|
||||
workspacePath := s.GetWorkspacePath(userID, workspaceID)
|
||||
fullPath := filepath.Join(workspacePath, path)
|
||||
cleanPath := filepath.Clean(fullPath)
|
||||
|
||||
@@ -34,7 +71,31 @@ func (fs *FileSystem) ValidatePath(userID, workspaceID int, path string) (string
|
||||
return cleanPath, nil
|
||||
}
|
||||
|
||||
// GetTotalFileStats returns the total file statistics for the file system.
|
||||
func (fs *FileSystem) GetTotalFileStats() (*FileCountStats, error) {
|
||||
return fs.countFilesInPath(fs.RootDir)
|
||||
// osFS implements the FileSystem interface using the real filesystem.
|
||||
type osFS struct{}
|
||||
|
||||
// ReadFile reads the file at the given path.
|
||||
func (f *osFS) ReadFile(path string) ([]byte, error) { return os.ReadFile(path) }
|
||||
|
||||
// WriteFile writes the given data to the file at the given path.
|
||||
func (f *osFS) WriteFile(path string, data []byte, perm fs.FileMode) error {
|
||||
return os.WriteFile(path, data, perm)
|
||||
}
|
||||
|
||||
// Remove deletes the file at the given path.
|
||||
func (f *osFS) Remove(path string) error { return os.Remove(path) }
|
||||
|
||||
// MkdirAll creates the directory at the given path and any necessary parents.
|
||||
func (f *osFS) MkdirAll(path string, perm fs.FileMode) error { return os.MkdirAll(path, perm) }
|
||||
|
||||
// RemoveAll removes the file or directory at the given path.
|
||||
func (f *osFS) RemoveAll(path string) error { return os.RemoveAll(path) }
|
||||
|
||||
// ReadDir reads the directory at the given path.
|
||||
func (f *osFS) ReadDir(path string) ([]fs.DirEntry, error) { return os.ReadDir(path) }
|
||||
|
||||
// Stat returns the FileInfo for the file at the given path.
|
||||
func (f *osFS) Stat(path string) (fs.FileInfo, error) { return os.Stat(path) }
|
||||
|
||||
// IsNotExist returns true if the error is a "file does not exist" error.
|
||||
func (f *osFS) IsNotExist(err error) bool { return os.IsNotExist(err) }
|
||||
|
||||
Reference in New Issue
Block a user