-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathphoto.go
226 lines (183 loc) · 5.74 KB
/
photo.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
package vkapi
import (
"encoding/json"
"io/ioutil"
"net/url"
"strconv"
"strings"
)
type PhotoAttachmentSizes struct {
Type string `json:"type"`
Url string `json:"url"`
Width int `json:"width"`
Height int `json:"height"`
}
type PhotoAttachment struct {
ID int `json:"id"`
AID int `json:"album_id"`
OwnerID int `json:"owner_id"`
Photo75 string `json:"photo_75"`
Photo130 string `json:"photo_130"`
Photo604 string `json:"photo_604"`
Photo807 string `json:"photo_807"`
Photo1280 string `json:"photo_1280"`
Photo2560 string `json:"photo_2560"`
Width int `json:"width"`
Height int `json:"height"`
Text string `json:"text"`
Created int64 `json:"created"`
AccessKey string `json:"access_key"`
Sizes []PhotoAttachmentSizes `json:"sizes"`
}
type photoUploadServer struct {
UploadURL string `json:"upload_url"`
AlbumID int `json:"album_id"`
UserID int `json:"user_id"`
}
type photoWallUploadResult struct {
Server int `json:"server"`
Hash string `json:"hash"`
Photo string `json:"photo"`
}
func (client *VKClient) photoGetUploadServer(params url.Values, method string) (*photoUploadServer, error) {
resp, err := client.MakeRequest(method, params)
if err != nil {
return nil, err
}
data := new(photoUploadServer)
json.Unmarshal(resp.Response, data)
return data, nil
}
func (client *VKClient) photoUpload(params url.Values, files []string, method string) (*photoWallUploadResult, error) {
serverInfo, err := client.photoGetUploadServer(params, method)
if err != nil {
return nil, err
}
req, err := client.getPhotoMultipartReq(serverInfo.UploadURL, files)
if err != nil {
return nil, err
}
resp, err := client.Client.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
uploadData := new(photoWallUploadResult)
if err := json.Unmarshal(body, uploadData); err != nil {
return nil, err
}
return uploadData, nil
}
func (client *VKClient) photoUploadByLink(params url.Values, urlFile string, method string) (*photoWallUploadResult, error) {
serverInfo, err := client.photoGetUploadServer(params, method)
if err != nil {
return nil, err
}
req, err := client.getPhotoByLinkMultipartReq(serverInfo.UploadURL, urlFile)
if err != nil {
return nil, err
}
resp, err := client.Client.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
uploadData := new(photoWallUploadResult)
if err := json.Unmarshal(body, uploadData); err != nil {
return nil, err
}
return uploadData, nil
}
func (client *VKClient) photoWallUpload(groupID string, files []string) (*photoWallUploadResult, error) {
params := url.Values{}
params.Set("group_id", groupID)
return client.photoUpload(params, files, "photos.getWallUploadServer")
}
func (client *VKClient) photoByLinkWallUpload(groupID string, urlFile string) (*photoWallUploadResult, error) {
params := url.Values{}
params.Set("group_id", groupID)
return client.photoUploadByLink(params, urlFile, "photos.getWallUploadServer")
}
func (client *VKClient) photoMessagesUpload(peerID string, files []string) (*photoWallUploadResult, error) {
params := url.Values{}
params.Set("peer_id", peerID)
return client.photoUpload(params, files, "photos.getMessagesUploadServer")
}
func (client *VKClient) UploadGroupWallPhotos(groupID int, files []string) ([]*PhotoAttachment, error) {
gidStr := strconv.Itoa(groupID)
if gidStr[0] == '-' {
gidStr = gidStr[1:]
}
uploadData, err := client.photoWallUpload(gidStr, files)
if err != nil {
return nil, err
}
params := url.Values{}
params.Set("group_id", gidStr)
params.Set("photo", string(uploadData.Photo))
params.Set("server", strconv.Itoa(uploadData.Server))
params.Set("hash", uploadData.Hash)
resp, err := client.MakeRequest("photos.saveWallPhoto", params)
if err != nil {
return nil, err
}
var photos []*PhotoAttachment
json.Unmarshal(resp.Response, &photos)
return photos, err
}
func (client *VKClient) UploadByLinkGroupWallPhotos(groupID int, urlFile string) ([]*PhotoAttachment, error) {
gidStr := strconv.Itoa(groupID)
if gidStr[0] == '-' {
gidStr = gidStr[1:]
}
uploadData, err := client.photoByLinkWallUpload(gidStr, urlFile)
if err != nil {
return nil, err
}
params := url.Values{}
params.Set("group_id", gidStr)
params.Set("photo", string(uploadData.Photo))
params.Set("server", strconv.Itoa(uploadData.Server))
params.Set("hash", uploadData.Hash)
resp, err := client.MakeRequest("photos.saveWallPhoto", params)
if err != nil {
return nil, err
}
var photos []*PhotoAttachment
json.Unmarshal(resp.Response, &photos)
return photos, err
}
func (client *VKClient) UploadMessagesPhotos(peerID int, files []string) ([]*PhotoAttachment, error) {
pidStr := strconv.Itoa(peerID)
if pidStr[0] == '-' {
pidStr = pidStr[1:]
}
uploadData, err := client.photoMessagesUpload(pidStr, files)
if err != nil {
return nil, err
}
params := url.Values{}
params.Set("photo", string(uploadData.Photo))
params.Set("server", strconv.Itoa(uploadData.Server))
params.Set("hash", uploadData.Hash)
resp, err := client.MakeRequest("photos.saveMessagesPhoto", params)
if err != nil {
return nil, err
}
var photos []*PhotoAttachment
json.Unmarshal(resp.Response, &photos)
return photos, err
}
func (client *VKClient) GetPhotosString(photos []*PhotoAttachment) string {
s := []string{}
for _, p := range photos {
s = append(s, "photo"+strconv.Itoa(p.OwnerID)+"_"+strconv.Itoa(p.ID))
}
return strings.Join(s, ",")
}