Skip to content

Commit e1ebd9a

Browse files
committed
增加v2.1版本File相关接口的支持
1 parent 374d1ef commit e1ebd9a

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Golang版本的Seafile Web API库
3434
# 当前支持的v2.1接口
3535
- [x] 资料库
3636
- [x] 获取资料库信息
37+
- [ ] 文件
38+
- [x] 获取文件信息
3739

3840
# TBD
3941
由于目前Seafile官方的文档并不完善,尤其是错误处理方面。有时候用HTTP状态吗、有时候用字符串、有时候用非固定的JSON字符串。

api_file.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package seafile
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
"time"
9+
)
10+
11+
type File struct {
12+
Id string `json:"obj_id"`
13+
Name string `json:"obj_name"`
14+
Type string
15+
Size int64
16+
Mtime time.Time
17+
RepoId string `json:"repo_id"`
18+
IsLocked bool `json:"is_locked"`
19+
ParentDir string `json:"parent_dir"`
20+
21+
repo *Repo `json:"-"`
22+
}
23+
24+
func (repo *Repo) GetFile(path string) (*File, error) {
25+
q := url.Values{"p": {path}}
26+
resp, err := repo.client.apiGET(repo.Uri() + "/file/?" + q.Encode())
27+
if err != nil {
28+
return nil, fmt.Errorf("请求文件信息失败: %s", err)
29+
}
30+
defer resp.Body.Close()
31+
32+
if resp.StatusCode != http.StatusOK {
33+
return nil, fmt.Errorf("文件信息错误: %s", resp.Status)
34+
}
35+
36+
var file File
37+
err = json.NewDecoder(resp.Body).Decode(&file)
38+
if err != nil {
39+
return nil, fmt.Errorf("解析文件信息失败: %s, %s", resp.Status, err)
40+
}
41+
42+
file.repo = repo
43+
44+
return &file, nil
45+
}

file_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package seafile
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestGetFile(t *testing.T) {
9+
client := New(os.Getenv("SEAFILE_HOST"), os.Getenv("SEAFILE_TOKEN"))
10+
11+
repo, err := client.GetRepoByName(os.Getenv("SEAFILE_REPO"))
12+
if err != nil {
13+
t.Fatal(err)
14+
}
15+
16+
file, err := repo.GetFile(os.Getenv("SEAFILE_FILE"))
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
21+
t.Log("文件信息:")
22+
t.Logf("%+v", file)
23+
}

0 commit comments

Comments
 (0)