add move func, fix test for copy

This commit is contained in:
Shane C. 2024-11-19 09:20:53 -05:00
parent 9301eb3727
commit 222b66c2f2
Signed by: Shane C.
GPG key ID: E46B5FEA35B22FF9
2 changed files with 42 additions and 2 deletions

View file

@ -273,3 +273,23 @@ func (s *Storage) Copy(name string, dest string) error {
return nil return nil
} }
} }
func (s *Storage) Move(name string, dest string) error {
if s.s3Client != nil {
if _, err := s.s3Client.CopyObject(s.ctx, minio.CopyDestOptions{
Bucket: s.config.S3BucketName,
Object: dest,
}, minio.CopySrcOptions{
Bucket: s.config.S3BucketName,
Object: name,
}); err != nil {
return err
}
if err := s.s3Client.RemoveObject(s.ctx, s.config.S3BucketName, name, minio.RemoveObjectOptions{}); err != nil {
return err
}
return nil
} else {
return os.Rename(filepath.Join(s.config.Path, name), filepath.Join(s.config.Path, dest))
}
}

View file

@ -39,7 +39,17 @@ func TestStorageLocal(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, fs.FileMode(0600), copyFileInfo.Mode) assert.Equal(t, fs.FileMode(0600), copyFileInfo.Mode)
assert.Equal(t, "test2.txt", copyFileInfo.Name) assert.Equal(t, "test2.txt", copyFileInfo.Name)
assert.Equal(t, int64(len("hi")), fileInfo.Size) assert.Equal(t, int64(len("hi")), copyFileInfo.Size)
t.Log("== Move ==")
err = stor.Move("test2.txt", "test3.txt")
assert.NoError(t, err)
moveFileInfo, err := stor.Stat("test3.txt")
assert.NoError(t, err)
assert.Equal(t, fs.FileMode(0600), moveFileInfo.Mode)
assert.Equal(t, "test3.txt", moveFileInfo.Name)
assert.Equal(t, int64(len("hi")), moveFileInfo.Size)
t.Log("== Open ==") t.Log("== Open ==")
file, err := stor.Open("test.txt") file, err := stor.Open("test.txt")
@ -94,7 +104,17 @@ func TestStorageS3(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, fs.FileMode(0777), copyFileInfo.Mode) assert.Equal(t, fs.FileMode(0777), copyFileInfo.Mode)
assert.Equal(t, "test2.txt", copyFileInfo.Name) assert.Equal(t, "test2.txt", copyFileInfo.Name)
assert.Equal(t, int64(len("hi")), fileInfo.Size) assert.Equal(t, int64(len("hi")), copyFileInfo.Size)
t.Log("== Move ==")
err = stor.Move("test2.txt", "test3.txt")
assert.NoError(t, err)
moveFileInfo, err := stor.Stat("test3.txt")
assert.NoError(t, err)
assert.Equal(t, fs.FileMode(0777), moveFileInfo.Mode)
assert.Equal(t, "test3.txt", moveFileInfo.Name)
assert.Equal(t, int64(len("hi")), moveFileInfo.Size)
t.Log("== Open ==") t.Log("== Open ==")
file, err := stor.Open("test.txt") file, err := stor.Open("test.txt")