Skip to content

Commit

Permalink
refactor: add comments and fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
saltbo committed Apr 25, 2020
1 parent 5e7704f commit 4f6fe68
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
12 changes: 5 additions & 7 deletions core/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ func (m mockUploader) Delete(object string) error {
}

func TestEngine(t *testing.T) {
// init
tmp := "/tmp/uptoc/"
// init test data
tmp := "/tmp/uptoc/mock"
assert.NoError(t, os.RemoveAll(tmp))
assert.NoError(t, os.Mkdir(tmp, os.FileMode(0755)))
files := map[string]string{
"abc1.txt": "abcabcabc",
"abc1.txt": "abcabc",
"abc2.txt": "112233",
"abc3.txt": "445566",
}
assert.NoError(t, os.Mkdir(tmp, os.FileMode(0755)))
for name, content := range files {
assert.NoError(t, ioutil.WriteFile(tmp+name, []byte(content), os.FileMode(0644)))
}
Expand All @@ -45,7 +46,4 @@ func TestEngine(t *testing.T) {
e := NewEngine(&mockUploader{})
assert.NoError(t, e.LoadAndCompareObjects("/tmp/uptoc"))
assert.NoError(t, e.Sync())

// clean the test files.
assert.NoError(t, os.RemoveAll(tmp))
}
5 changes: 5 additions & 0 deletions uploader/cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (
"github.com/tencentyun/cos-go-sdk-v5"
)

// COSUploader implements the Driver base on tencent's cos.
type COSUploader struct {
*cos.Client
}

// NewCOSUploader returns a new COS uploader
func NewCOSUploader(endpoint, accessKeyID, accessKeySecret, bucketName string) (Driver, error) {
u, err := url.Parse(fmt.Sprintf("https://%s.cos.%s.myqcloud.com", bucketName, endpoint))
if err != nil {
Expand All @@ -32,6 +34,7 @@ func NewCOSUploader(endpoint, accessKeyID, accessKeySecret, bucketName string) (
}, nil
}

// ListObjects returns some remote objects
func (u *COSUploader) ListObjects() ([]Object, error) {
marker := ""
objects := make([]Object, 0)
Expand All @@ -55,11 +58,13 @@ func (u *COSUploader) ListObjects() ([]Object, error) {
return objects, nil
}

// Upload uploads the local file to the object
func (u *COSUploader) Upload(object, rawPath string) (err error) {
_, err = u.Object.PutFromFile(context.Background(), object, rawPath, nil)
return
}

// Delete deletes the object
func (u *COSUploader) Delete(object string) (err error) {
_, err = u.Object.Delete(context.Background(), object)
return
Expand Down
4 changes: 2 additions & 2 deletions uploader/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ type Driver interface {
type Constructor func(endpoint, accessKeyID, accessKeySecret, bucketName string) (Driver, error)

var supportDrivers = map[string]Constructor{
"oss": AliOSSUploader,
"qiniu": QiniuUploader,
"oss": NewOSSUploader,
"cos": NewCOSUploader,
"qiniu": NewQiniuUploader,
}

// New is a instantiation function to find and init a upload driver.
Expand Down
6 changes: 2 additions & 4 deletions uploader/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ var driverConfigs = map[string]map[string]string{

func TestUploader(t *testing.T) {
tmp := "/tmp/uptoc/"
assert.NoError(t, os.RemoveAll(tmp))
assert.NoError(t, os.Mkdir(tmp, os.FileMode(0755)))
files := map[string]string{
"abc1.txt": "abcabcabc",
"abc2.txt": "112233",
"abc3.txt": "445566",
}
assert.NoError(t, os.Mkdir(tmp, os.FileMode(0755)))
for name, content := range files {
assert.NoError(t, ioutil.WriteFile(tmp+name, []byte(content), os.FileMode(0644)))
}
Expand All @@ -61,9 +62,6 @@ func TestUploader(t *testing.T) {
assert.NoError(t, uploader.Delete(object))
}
}

// clean the test files.
assert.NoError(t, os.RemoveAll(tmp))
}

func TestNotSupportDriver(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions uploader/oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ type OSSUploader struct {
bucket *oss.Bucket
}

// AliOSSUploader returns a new oss uploader
func AliOSSUploader(endpoint, accessKeyID, accessKeySecret, bucketName string) (Driver, error) {
// NewOSSUploader returns a new oss uploader
func NewOSSUploader(endpoint, accessKeyID, accessKeySecret, bucketName string) (Driver, error) {
ossCli, err := oss.New(endpoint, accessKeyID, accessKeySecret)
if err != nil {
return nil, err
Expand Down
7 changes: 6 additions & 1 deletion uploader/qiniu.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ var zones = map[string]storage.Zone{
"xinjiapo": storage.ZoneXinjiapo,
}

// Qiniu implements the Driver base on qiuniu.
type Qiniu struct {
mac *qbox.Mac
cfg *storage.Config
bucketName string
}

func QiniuUploader(endpoint, accessKey, accessSecret, bucketName string) (Driver, error) {
// NewQiniuUploader returns a new Qiniu uploader
func NewQiniuUploader(endpoint, accessKey, accessSecret, bucketName string) (Driver, error) {
zone, ok := zones[endpoint]
if !ok {
return nil, fmt.Errorf("endpoint %s not support", endpoint)
Expand All @@ -37,6 +39,7 @@ func QiniuUploader(endpoint, accessKey, accessSecret, bucketName string) (Driver
}, nil
}

// ListObjects returns some remote objects
func (u *Qiniu) ListObjects() ([]Object, error) {
limit := 1000
prefix := ""
Expand Down Expand Up @@ -69,6 +72,7 @@ func (u *Qiniu) ListObjects() ([]Object, error) {
return objects, nil
}

// Upload uploads the local file to the object
func (u *Qiniu) Upload(object, rawPath string) error {
putPolicy := storage.PutPolicy{
Scope: u.bucketName,
Expand All @@ -78,6 +82,7 @@ func (u *Qiniu) Upload(object, rawPath string) error {
return storage.NewFormUploader(u.cfg).PutFile(ctx, &storage.PutRet{}, upToken, object, rawPath, nil)
}

// Delete deletes the object
func (u *Qiniu) Delete(object string) error {
return storage.NewBucketManager(u.mac, u.cfg).Delete(u.bucketName, object)
}

0 comments on commit 4f6fe68

Please sign in to comment.