Feat: list oss files

pull/348/head
HFO4 5 years ago
parent 1afc750dae
commit 52c2422be9

@ -18,6 +18,7 @@ import (
"io"
"net/url"
"path"
"path/filepath"
"strings"
"time"
)
@ -100,39 +101,72 @@ func (handler *Driver) InitOSSClient() error {
return nil
}
func (handler Driver) List(ctx context.Context, path string, recursive bool) ([]response.Object, error) {
// List 列出OSS上的文件
func (handler Driver) List(ctx context.Context, base string, recursive bool) ([]response.Object, error) {
// 初始化客户端
if err := handler.InitOSSClient(); err != nil {
return nil, err
}
// 列取文件
options := []oss.Option{
oss.Prefix(strings.TrimPrefix(path, "/")),
}
if recursive {
options = append(options, oss.Delimiter("/"))
base = strings.TrimPrefix(base, "/")
delimiter := ""
if !recursive {
delimiter = "/"
}
listRes, err := handler.bucket.ListObjects(options...)
marker := ""
var (
objects []oss.ObjectProperties
commons []string
)
for {
subRes, err := handler.bucket.ListObjects(oss.Marker(marker), oss.Prefix(base),
oss.MaxKeys(1000), oss.Delimiter(delimiter))
if err != nil {
return nil, err
}
objects = append(objects, subRes.Objects...)
commons = append(commons, subRes.CommonPrefixes...)
marker = subRes.NextMarker
if marker == "" {
break
}
}
// 处理列取结果
res := make([]response.Object, len(listRes.CommonPrefixes)+len(listRes.Objects))
res := make([]response.Object, 0, len(objects)+len(commons))
// 处理目录
//for _,object := range listRes.CommonPrefixes{
// res = append(res,response.Object{
// Name: "",
// RelativePath: "",
// Source: "",
// Size: 0,
// IsDir: false,
// LastModify: time.Time{},
// })
//}
return res, err
for _, object := range commons {
rel, err := filepath.Rel(base, object)
if err != nil {
continue
}
res = append(res, response.Object{
Name: path.Base(object),
RelativePath: filepath.ToSlash(rel),
Size: 0,
IsDir: true,
LastModify: time.Now(),
})
}
// 处理文件
for _, object := range objects {
rel, err := filepath.Rel(base, object.Key)
if err != nil {
continue
}
res = append(res, response.Object{
Name: path.Base(object.Key),
Source: object.Key,
RelativePath: filepath.ToSlash(rel),
Size: uint64(object.Size),
IsDir: false,
LastModify: object.LastModified,
})
}
return res, nil
}
// Get 获取文件

@ -145,6 +145,10 @@ func (job *ImportTask) Do() {
parentFolder := &model.Folder{}
if parent, ok := pathCache[virtualPath]; ok {
parentFolder = parent
} else {
exist, folder := fs.IsPathExist(virtualPath)
if exist {
parentFolder = folder
} else {
folder, err := fs.CreateDirectory(context.Background(), virtualPath)
if err != nil {
@ -154,6 +158,7 @@ func (job *ImportTask) Do() {
}
parentFolder = folder
}
}
// 插入文件记录
_, err := fs.AddFile(addFileCtx, parentFolder)

@ -142,6 +142,8 @@ func TestImportTask_Do(t *testing.T) {
mock.ExpectCommit()
// 查找父目录,但是不存在
mock.ExpectQuery("SELECT(.+)folders").WillReturnRows(sqlmock.NewRows([]string{"id"}))
// 仍然不存在
mock.ExpectQuery("SELECT(.+)folders").WillReturnRows(sqlmock.NewRows([]string{"id"}))
// 创建文件时查找父目录,仍然不存在
mock.ExpectQuery("SELECT(.+)folders").WillReturnRows(sqlmock.NewRows([]string{"id"}))

Loading…
Cancel
Save