-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_file.go
160 lines (129 loc) · 3.61 KB
/
api_file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package seafile
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"path/filepath"
"time"
)
type File struct {
Id string `json:"obj_id"`
Name string `json:"obj_name"`
Type string
Size int64
Mtime time.Time
RepoId string `json:"repo_id"`
IsLocked bool `json:"is_locked"`
ParentDir string `json:"parent_dir"`
repo *Repo `json:"-"`
}
//文件完整路径
func (file *File) Path() string {
return filepath.Join(file.ParentDir, file.Name)
}
func (repo *Repo) GetFile(path string) (*File, error) {
q := url.Values{"p": {path}}
resp, err := repo.client.apiGET(repo.Uri() + "/file/?" + q.Encode())
if err != nil {
return nil, fmt.Errorf("请求文件信息失败: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("文件信息错误: %s", resp.Status)
}
var file File
err = json.NewDecoder(resp.Body).Decode(&file)
if err != nil {
return nil, fmt.Errorf("解析文件信息失败: %s, %s", resp.Status, err)
}
file.repo = repo
return &file, nil
}
//检查文件是否存在,如果不存在则创建,返回文件本身
func (repo *Repo) TouchFile(path string) (*File, error) {
file, err := repo.GetFile(path)
if err == nil {
file.repo = repo
return file, nil
}
return repo.CreateFile(path)
}
//创建文件
//如果文件已存在,则按照重命名规则创建新文件
//如果不希望创建重命名的文件,建议使用Repo.Touch方法
func (repo *Repo) CreateFile(path string) (*File, error) {
q := url.Values{"p": {path}}
d := url.Values{"operation": {"create"}}
resp, err := repo.client.apiPOSTForm(repo.Uri()+"/file/?"+q.Encode(), d)
if err != nil {
return nil, fmt.Errorf("请求资料库信息失败: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("[%s]%s", resp.Status, string(b))
}
var file File
err = json.NewDecoder(resp.Body).Decode(&file)
if err != nil {
return nil, fmt.Errorf("解析资料库信息失败: %s, %s", resp.Status, err)
}
file.repo = repo
return &file, nil
}
//更新文件内容
func (file *File) Update(content []byte) error {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
//填充文件内容
part, err := writer.CreateFormFile("file", file.Name)
if err != nil {
return fmt.Errorf("创建Multipart错误:%s", err)
}
part.Write(content)
//填充其他字段
writer.WriteField("target_file", file.Path())
err = writer.Close()
if err != nil {
return fmt.Errorf("写Multipart文件错误:%s", err)
}
//设置请求Header
header := http.Header{"Content-Type": {writer.FormDataContentType()}}
link, err := file.repo.FileUpdateLink()
if err != nil {
return fmt.Errorf("获取上传地址错误:%s", err)
}
//执行上传
resp, err := file.repo.client.request("POST", link+"?ret-json=1", header, body)
if err != nil {
return fmt.Errorf("请求错误:%s", err)
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("读取错误:%s", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("[%s] %s", resp.Status, string(b))
}
file.Id = string(b)
return nil
}
//删除文件
func (file *File) Delete() error {
q := url.Values{"p": {file.Path()}}
resp, err := file.repo.client.apiDELETE(file.repo.Uri() + "/file/?" + q.Encode())
if err != nil {
return fmt.Errorf("请求错误:%s", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return nil
}
b, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("[%s] %s", resp.Status, string(b))
}