diff --git a/siopao/api_move.go b/siopao/api_move.go index 81afd7a..cb8c32e 100644 --- a/siopao/api_move.go +++ b/siopao/api_move.go @@ -9,6 +9,9 @@ import ( // move the file to another folder. If you want to simply rename the file's name, use Rename instead, otherwise, // if you want to keep the name, but move the folder, use MoveTo instead. func (file *File) Move(dest string) error { + if err := mkparent(dest); err != nil { + return err + } return os.Rename(file.path, dest) } @@ -28,6 +31,10 @@ func (file *File) Rename(name string) error { // If you want to move the file into an entirely new folder, use Move instead. // You can also use Rename if you want to rename the file's name. func (file *File) MoveTo(dir string) error { - base := filepath.Base(dir) - return os.Rename(file.path, filepath.Join(dir, base)) + base := filepath.Base(file.path) + dest := filepath.Join(dir, base) + if err := mkparent(dest); err != nil { + return err + } + return os.Rename(file.path, dest) } diff --git a/siopao/core_open.go b/siopao/core_open.go index 1b7da56..636acc9 100644 --- a/siopao/core_open.go +++ b/siopao/core_open.go @@ -55,8 +55,12 @@ func (file *File) clear() error { // MkdirParent creates the parent folders of the path. func (file *File) mkparent() error { - if strings.Contains(file.path, "\\") || strings.Contains(file.path, "/") { - if err := os.MkdirAll(filepath.Dir(file.path), os.ModePerm); err != nil { + return mkparent(file.path) +} + +func mkparent(path string) error { + if strings.Contains(path, "\\") || strings.Contains(path, "/") { + if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil { return err } } diff --git a/siopao/siopao_test.go b/siopao/siopao_test.go index 3ed7195..da8b64a 100644 --- a/siopao/siopao_test.go +++ b/siopao/siopao_test.go @@ -47,6 +47,49 @@ func TestFile_Checksum(t *testing.T) { } } +func TestFile_Copy(t *testing.T) { + file := Open(".tests/write-01.txt") + err := file.Copy(".tests/copy-01.txt") + if err != nil { + t.Fatal("failed to copy to test text file: ", err) + } +} + +func TestFile_CopyWithHash(t *testing.T) { + file := Open(".tests/write-01.txt") + checksum, err := file.CopyWithHash(Md5Checksum, ".tests/copy-02.txt") + if err != nil { + t.Fatal("failed to copy to test text file: ", err) + } + t.Log("checksum of copy: ", checksum) +} + +func TestFile_Rename(t *testing.T) { + file := Open(".tests/write-01.txt") + err := file.Copy(".tests/copy-03.txt") + if err != nil { + t.Fatal("failed to copy to test text file: ", err) + } + file = Open(".tests/copy-03.txt") + if err := file.Rename("rename-01.txt"); err != nil { + t.Fatal("failed to rename test text file: ", err) + } +} + +func TestFile_MoveTo(t *testing.T) { + file := Open(".tests/rename-01.txt") + if err := file.MoveTo(".tests/renamed"); err != nil { + t.Fatal("failed to move to new directory: ", err) + } +} + +func TestFile_Move(t *testing.T) { + file := Open(".tests/renamed/rename-01.txt") + if err := file.Move(".tests/renamed/rename-02.txt"); err != nil { + t.Fatal("failed to force move to new directory: ", err) + } +} + type Hello struct { World string `json:"world"` }