-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyadisk.go
280 lines (235 loc) · 5.98 KB
/
yadisk.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package yadisk
import (
"encoding/json"
"errors"
"sync"
//"fmt"
//"github.com/davecgh/go-spew/spew"
"io"
"io/ioutil"
"net/http"
)
var (
disk *yaDisk
once sync.Once
)
type yaResList struct {
P_sort string `json:"sort"`
P_limit uint64 `json:"limit"`
P_offset uint64 `json:"offset"`
P_path string `json:"path"`
P_total uint64 `json:"total"`
P_items []*yaResource `json:"items"`
}
type yaResource struct {
P_name string `json:"name"`
P_sha256 string `json:"sha256"`
P_md5 string `json:"md5"`
P_created string `json:"created"`
P_revision uint64 `json:"revision"`
P_resource_id string `json:"resource_id"`
P_modified string `json:"modified"`
P_media_type string `json:"media_type"`
P_path string `json:"path"`
P_type string `json:"type"`
P_mime_type string `json:"mime_type`
P_size uint64 `json:"size"`
P_embed *yaResList `json:"_embedded"`
}
type yaLink struct {
P_href string `json:"href"`
P_method string `json:"method"`
P_templated bool `json:"templated"`
}
type yaDisk struct {
oauth string
apipath string
httpCon *http.Client
mainRes *yaResource
}
func YaDisk(oauth string) *yaDisk {
if disk == nil {
once.Do(func() {
disk = &yaDisk{
httpCon: &http.Client{},
oauth: oauth,
apipath: "https://cloud-api.yandex.net/v1/disk",
}
})
}
return disk
}
func (d *yaDisk) ReceiveMainRes() (err error) {
// Prepare http request
req, err := http.NewRequest(http.MethodGet, d.apipath+"/resources?path=app:/", nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "OAuth "+d.oauth)
req.Header.Set("Content-Type", "application/json")
resp, err := d.httpCon.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
answer, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode == http.StatusOK {
//AppResource := &YaResource{}
//fmt.Printf("answer: (%d)\n%s\n\n", resp.StatusCode, string(answer))
err := json.Unmarshal(answer, &d.mainRes)
if err != nil {
return err
}
//spew.Dump(AppResource)
return nil
//fmt.Printf("STRUCT:\n%+v\n", AppFolder)
} else {
return errors.New("Answer error: " + string(answer))
}
}
func (d *yaDisk) getUploadPath(name string) (yaresp *yaLink, err error) {
// Prepare http request
req, err := http.NewRequest(http.MethodGet, d.apipath+"/resources/upload?path=app:/"+name+"&overwrite=true", nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "OAuth "+d.oauth)
req.Header.Set("Content-Type", "application/json")
resp, err := d.httpCon.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
answer, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusOK {
yar := &yaLink{}
err := json.Unmarshal(answer, yar)
if err != nil {
return nil, err
}
return yar, nil
} else {
err = errors.New("GET upload path error: " + string(answer))
return nil, err
}
}
func (d *yaDisk) getDownloadPath(name string) (yaresp *yaLink, err error) {
// Prepare http request
req, err := http.NewRequest(http.MethodGet, d.apipath+"/resources/download?path=app:/"+name, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "OAuth "+d.oauth)
req.Header.Set("Content-Type", "application/json")
resp, err := d.httpCon.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
answer, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusOK {
yar := &yaLink{}
err := json.Unmarshal(answer, yar)
if err != nil {
return nil, err
}
return yar, nil
} else {
err = errors.New("GET download path error: " + string(answer))
return nil, err
}
}
func (d *yaDisk) PutData(name string, reader io.Reader) (err error) {
upl, err := d.getUploadPath(name)
if err != nil {
return err
}
//fmt.Printf("upl: %s\n", upl.P_href)
// Prepare PUT request
req, err := http.NewRequest(http.MethodPut, upl.P_href, reader)
if err != nil {
return err
}
resp, err := d.httpCon.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
answer, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode == http.StatusCreated {
return nil
} else {
return errors.New("PUT error: (" + resp.Status + ") " + string(answer))
}
}
func (d *yaDisk) GetCurl(name string) (curlstr string, err error) {
dwl, err := d.getDownloadPath(name)
if err != nil {
return "", err
}
str := "curl -L -X " + dwl.P_method + " \"" + dwl.P_href + "\" -o " + name
return str, nil
}
func (d *yaDisk) GetData(name string, writer io.Writer) (err error) {
dwl, err := d.getDownloadPath(name)
if err != nil {
return err
}
//fmt.Printf("upl: %s\n", upl.P_href)
// Prepare GET request
req, err := http.NewRequest(http.MethodGet, dwl.P_href, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "OAuth "+d.oauth)
resp, err := d.httpCon.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
_, err := io.Copy(writer, resp.Body)
return err
} else {
answer, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return errors.New("GET error: (" + resp.Status + ") " + string(answer))
}
}
func (d *yaDisk) DelRes(path string) (err error) {
// Prepare DELETE request
req, err := http.NewRequest(http.MethodDelete, d.apipath+"/resources?path=app:/"+path+"&permanently=true", nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "OAuth "+d.oauth)
req.Header.Set("Content-Type", "application/json")
resp, err := d.httpCon.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
answer, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode == http.StatusNoContent || resp.StatusCode == http.StatusAccepted {
return nil
} else {
return errors.New("DELETE error: (" + resp.Status + ") " + string(answer))
}
}