Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
[#79] Refactored the filenames and fixed the copy issue bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nandagopalan committed Apr 3, 2024
1 parent a9613da commit 06a6b6f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
File renamed without changes.
51 changes: 34 additions & 17 deletions vfs/base_fs.go → vfs/basefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vfs
import (
"io"
"net/url"
"path"

"go.nandlabs.io/commons/ioutils"
)
Expand All @@ -11,34 +12,50 @@ type BaseVFS struct {
VFileSystem
}

// Copy copies a file from src to dst. If src is a directory, it will copy all files in the directory to dst.

func (b *BaseVFS) Copy(src, dst *url.URL) (err error) {
// check if src is a directory if so copy all files in the directory to dst
var srcFile VFile
var srfFileInfo VFileInfo
var srcFileInfo VFileInfo
srcFile, err = b.Open(src)
if err == nil {
defer ioutils.CloserFunc(srcFile)
srfFileInfo, err = srcFile.Info()
srcFileInfo, err = srcFile.Info()
if err == nil {
if srfFileInfo.IsDir() {
err = b.Walk(src, func(file VFile) (err error) {
var fileInfo VFileInfo
fileInfo, err = srcFile.Info()
if srcFileInfo.IsDir() {
//Prepare url for destination
var dstFile VFile
dstFile, err = b.MkdirAll(dst)
if err == nil {
defer ioutils.CloserFunc(dstFile)
var children []VFile
children, err = srcFile.ListAll()
if err == nil {
if fileInfo.IsDir() {

for _, child := range children {
var childInfo VFileInfo
childInfo, err = child.Info()
if err == nil {
var childDst *url.URL
childDst, err = url.Parse(path.Join(dstFile.Url().String(), childInfo.Name()))
if err == nil {
err = b.Copy(child.Url(), childDst)
if err != nil {
return
}
}
}
}
}
return
})
// Create directories and copy files
} else {
var destFile VFile
destFile, err = manager.Create(dst)
defer ioutils.CloserFunc(destFile)
if err == nil {
_, err = io.Copy(srcFile, destFile)
}
}
} else {
var dstFile VFile
dstFile, err = b.Create(dst)
if err == nil {
defer ioutils.CloserFunc(dstFile)
_, err = io.Copy(dstFile, srcFile)
}
}
}
return
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions vfs/local_file_test.go → vfs/localfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ func TestOsFile_ListAll(t *testing.T) {
}
}

func TestOsFsSingleFile_Copy(t *testing.T) {
src := GetRawPath("file:///test-data/raw-folder")
dst := GetRawPath("file:///test-data/raw-folder-copy")
err := testManager.CopyRaw(src, dst)
if err != nil {
t.Errorf("Copy() error = %v", err)
}
}

func TestOsFs_Delete(t *testing.T) {
u := GetRawPath("file:///test-data")
err := testManager.DeleteRaw(u)
Expand Down
File renamed without changes.

0 comments on commit 06a6b6f

Please sign in to comment.