Implement MoveFile functionality in FileManager and corresponding tests

This commit is contained in:
2025-07-11 19:49:08 +02:00
parent 5a6895ecdc
commit 9bb95f603c
4 changed files with 158 additions and 0 deletions

View File

@@ -14,6 +14,7 @@ type FileManager interface {
FindFileByName(userID, workspaceID int, filename string) ([]string, error)
GetFileContent(userID, workspaceID int, filePath string) ([]byte, error)
SaveFile(userID, workspaceID int, filePath string, content []byte) error
MoveFile(userID, workspaceID int, srcPath string, dstPath string) error
DeleteFile(userID, workspaceID int, filePath string) error
GetFileStats(userID, workspaceID int) (*FileCountStats, error)
GetTotalFileStats() (*FileCountStats, error)
@@ -174,6 +175,34 @@ func (s *Service) SaveFile(userID, workspaceID int, filePath string, content []b
return nil
}
// MoveFile moves a file from srcPath to dstPath within the workspace directory.
// Both paths must be relative to the workspace directory given by userID and workspaceID.
// If the destination file already exists, it will be overwritten.
func (s *Service) MoveFile(userID, workspaceID int, srcPath string, dstPath string) error {
log := getLogger()
srcFullPath, err := s.ValidatePath(userID, workspaceID, srcPath)
if err != nil {
return err
}
dstFullPath, err := s.ValidatePath(userID, workspaceID, dstPath)
if err != nil {
return err
}
if err := s.fs.MoveFile(srcFullPath, dstFullPath); err != nil {
return fmt.Errorf("failed to move file: %w", err)
}
log.Debug("file moved",
"userID", userID,
"workspaceID", workspaceID,
"src", srcPath,
"dst", dstPath)
return nil
}
// DeleteFile deletes the file at the given filePath.
// Path must be a relative path within the workspace directory given by userID and workspaceID.
func (s *Service) DeleteFile(userID, workspaceID int, filePath string) error {