-
Notifications
You must be signed in to change notification settings - Fork 7
/
files.go
179 lines (144 loc) · 4.18 KB
/
files.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
package vkapi
import (
"bytes"
"encoding/json"
"github.com/technoweenie/multipartstreamer"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
)
// FileBytes contains information about a set of bytes to upload
// as a File.
type FileBytes struct {
Name string
Bytes []byte
}
// FileReader contains information about a reader to upload as a File.
// If Size is -1, it will read the entire Reader into memory to
// calculate a Size.
type FileReader struct {
Name string
Reader io.Reader
Size int64
}
type ServerResponse struct {
Response
Server int64 `json:"server"`
Photo string `json:"photo"`
File string `json:"file"`
Hash string `json:"hash"`
}
func (client *Client) UploadFile(url string, fieldName string, file interface{}) (ServerResponse, *Error) {
ms := multipartstreamer.New()
switch f := file.(type) {
case string:
fileHandle, err := os.Open(f)
if err != nil {
return ServerResponse{}, NewError(ErrBadCode, err.Error())
}
defer fileHandle.Close()
fi, err := os.Stat(f)
if err != nil {
return ServerResponse{}, NewError(ErrBadCode, err.Error())
}
ms.WriteReader(fieldName, fileHandle.Name(), fi.Size(), fileHandle)
case FileBytes:
buf := bytes.NewBuffer(f.Bytes)
ms.WriteReader(fieldName, f.Name, int64(len(f.Bytes)), buf)
case FileReader:
if f.Size != -1 {
ms.WriteReader(fieldName, f.Name, f.Size, f.Reader)
break
}
data, err := ioutil.ReadAll(f.Reader)
if err != nil {
return ServerResponse{}, NewError(ErrBadCode, err.Error())
}
buf := bytes.NewBuffer(data)
ms.WriteReader(fieldName, f.Name, int64(len(data)), buf)
default:
return ServerResponse{}, NewError(ErrBadCode, "Bad input")
}
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return ServerResponse{}, NewError(ErrBadCode, err.Error())
}
ms.SetupRequest(req)
res, err := client.apiClient.httpClient.Do(req)
if err != nil {
return ServerResponse{}, NewError(ErrBadCode, err.Error())
}
defer res.Body.Close()
bytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return ServerResponse{}, NewError(ErrBadCode, err.Error())
}
client.apiClient.logPrintf("upload %s: %s", fieldName, string(bytes))
response := ServerResponse{}
if err := json.Unmarshal(bytes, &response); err != nil {
return ServerResponse{}, NewError(ErrBadCode, err.Error())
}
return response, nil
}
type UploadServer struct {
UploadURL string `json:"upload_url"`
}
type UploadServerPhoto struct {
UploadServer
UserID int64 `json:"user_id"`
AlbumID int64 `json:"album_id"`
}
func (client *Client) GetMessagesUploadServerForPhoto() (server UploadServerPhoto, err *Error) {
res, err := client.Do(NewRequest("photos.getMessagesUploadServer", "", nil))
if err != nil {
return UploadServerPhoto{}, err
}
if err := res.To(&server); err != nil {
return UploadServerPhoto{}, NewError(ErrBadCode, err.Error())
}
return
}
func (client *Client) GetMessagesUploadServerForDoc(fiendName string, peerID int64) (server UploadServer, err *Error) {
values := url.Values{}
values.Add("peer_id", ConcatInt64ToString(peerID))
values.Add("type", fiendName)
res, err := client.Do(NewRequest("docs.getMessagesUploadServer", "", values))
if err != nil {
return UploadServer{}, err
}
if err := res.To(&server); err != nil {
return UploadServer{}, NewError(ErrBadCode, err.Error())
}
return
}
func (client *Client) SaveMessagesDoc(file string, title string) (Document, *Error) {
values := url.Values{}
values.Set("file", file)
values.Set("title", title)
res, err := client.Do(NewRequest("docs.save", "", values))
if err != nil {
return Document{}, err
}
var doc []Document
if err := res.To(&doc); err != nil {
return Document{}, NewError(ErrBadCode, err.Error())
}
return doc[0], nil
}
func (client *Client) SaveMessagesPhoto(response ServerResponse) (Photo, *Error) {
values := url.Values{}
values.Add("photo", response.Photo)
values.Add("server", ConcatInt64ToString(response.Server))
values.Add("hash", response.Hash)
res, err := client.Do(NewRequest("photos.saveMessagesPhoto", "", values))
if err != nil {
return Photo{}, err
}
var photo []Photo
if err := res.To(&photo); err != nil {
return Photo{}, NewError(ErrBadCode, err.Error())
}
return photo[0], nil
}