Skip to content

Commit

Permalink
Merge pull request #23 from qor/fix-same
Browse files Browse the repository at this point in the history
Fix same
  • Loading branch information
zhangshanwen authored Jul 29, 2024
2 parents c04686f + 8256e3a commit 88484a7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions filesystem/filesystem.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package filesystem

import (
"bytes"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ 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 := 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 {
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)
Expand Down

0 comments on commit 88484a7

Please sign in to comment.