From 4fda859911b3e5960dbc8ca6487660d6e3f08fdf Mon Sep 17 00:00:00 2001 From: wen Date: Mon, 29 Jul 2024 16:33:17 +0800 Subject: [PATCH 1/2] fix put same file; new file cover old file --- .idea/.gitignore | 8 ++++++++ filesystem/filesystem.go | 14 ++++++++++---- tests/tests.go | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 .idea/.gitignore diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/filesystem/filesystem.go b/filesystem/filesystem.go index 56934aa..88fbaa1 100644 --- a/filesystem/filesystem.go +++ b/filesystem/filesystem.go @@ -1,6 +1,7 @@ package filesystem import ( + "bytes" "fmt" "io" "os" @@ -54,13 +55,18 @@ func (fileSystem FileSystem) Put(path string, reader io.Reader) (*oss.Object, er return nil, err } + if seeker, ok := reader.(io.ReadSeeker); ok { + seeker.Seek(0, 0) + } + buf := bytes.NewBuffer([]byte{}) + if _, err = io.Copy(buf, reader); err != nil { + return nil, err + } + dst, err := os.Create(fullpath) if err == nil { - if seeker, ok := reader.(io.ReadSeeker); ok { - seeker.Seek(0, 0) - } - _, err = io.Copy(dst, reader) + _, err = io.Copy(dst, buf) } return &oss.Object{Path: path, Name: filepath.Base(path), StorageInterface: fileSystem}, err diff --git a/tests/tests.go b/tests/tests.go index fb5d128..078018d 100644 --- a/tests/tests.go +++ b/tests/tests.go @@ -18,6 +18,7 @@ func TestAll(storage oss.StorageInterface, t *testing.T) { fmt.Printf("testing file in %v\n", filepath.Join(storage.GetEndpoint(), randomPath)) fileName := "/" + filepath.Join(randomPath, "sample.txt") + sampleFileName, _ := storage.GetURL("/tmp" + fileName) fileName2 := "/" + filepath.Join(randomPath, "sample2", "sample.txt") exceptObjects := 2 sampleFile, _ := filepath.Abs("../tests/sample.txt") @@ -33,6 +34,22 @@ func TestAll(storage oss.StorageInterface, t *testing.T) { t.Errorf("No error should happen when opem sample file, but got %v", err) } + // Put file again + if file, err := os.Open(sampleFileName); err == nil { + if object, err := storage.Put(fileName, file); err != nil { + t.Errorf("No error should happen when save sample file, but got %v", err) + } else if object.Path == "" || object.StorageInterface == nil { + t.Errorf("returned object should necessary information") + } else { + stat, _ := os.Stat(sampleFileName) + if stat.Size() == 0 { + t.Errorf("put same file error") + } + } + } else { + t.Errorf("No error should happen when opem sample file, but got %v", err) + } + if file, err := os.Open(sampleFile); err == nil { if object, err := storage.Put(fileName2, file); err != nil { t.Errorf("No error should happen when save sample file, but got %v", err) From 8256e3a7c68977d8363419bf51d160fc8768e8c4 Mon Sep 17 00:00:00 2001 From: wen Date: Mon, 29 Jul 2024 18:11:33 +0800 Subject: [PATCH 2/2] remove temp --- tests/tests.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/tests.go b/tests/tests.go index 078018d..d2899f6 100644 --- a/tests/tests.go +++ b/tests/tests.go @@ -18,7 +18,6 @@ func TestAll(storage oss.StorageInterface, t *testing.T) { fmt.Printf("testing file in %v\n", filepath.Join(storage.GetEndpoint(), randomPath)) fileName := "/" + filepath.Join(randomPath, "sample.txt") - sampleFileName, _ := storage.GetURL("/tmp" + fileName) fileName2 := "/" + filepath.Join(randomPath, "sample2", "sample.txt") exceptObjects := 2 sampleFile, _ := filepath.Abs("../tests/sample.txt") @@ -35,16 +34,11 @@ func TestAll(storage oss.StorageInterface, t *testing.T) { } // Put file again - if file, err := os.Open(sampleFileName); err == nil { + if file, err := storage.GetStream(fileName); err == nil { if object, err := storage.Put(fileName, file); err != nil { t.Errorf("No error should happen when save sample file, but got %v", err) } else if object.Path == "" || object.StorageInterface == nil { t.Errorf("returned object should necessary information") - } else { - stat, _ := os.Stat(sampleFileName) - if stat.Size() == 0 { - t.Errorf("put same file error") - } } } else { t.Errorf("No error should happen when opem sample file, but got %v", err)