-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathhelpers.go
More file actions
214 lines (173 loc) · 4.78 KB
/
Copy pathhelpers.go
File metadata and controls
214 lines (173 loc) · 4.78 KB
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
/*
* Echotron
* Copyright (C) 2018 The Echotron Contributors
*
* Echotron is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Echotron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package echotron
import (
"fmt"
"net/url"
"os"
"path/filepath"
"strconv"
)
// content contains a file's name, its type and its data.
type content struct {
fname string
ftype string
fdata []byte
}
func check(r APIResponse) error {
if b := r.Base(); !b.Ok {
return &APIError{code: b.ErrorCode, desc: b.Description}
}
return nil
}
func processMedia(media, thumbnail InputFile) (im mediaEnvelope, cnt []content, err error) {
switch {
case media.id != "":
im = mediaEnvelope{
media: media.id,
thumbnail: "",
}
case media.url != "":
im = mediaEnvelope{
media: media.url,
thumbnail: "",
}
case media.path != "" && len(media.content) == 0:
if media.content, media.path, err = readFile(media); err != nil {
return
}
fallthrough
case media.path != "" && len(media.content) > 0:
cnt = append(cnt, content{media.path, media.path, media.content})
im = mediaEnvelope{
media: fmt.Sprintf("attach://%s", media.path),
thumbnail: "",
}
}
switch {
case thumbnail.path != "" && len(thumbnail.content) == 0:
if thumbnail.content, thumbnail.path, err = readFile(thumbnail); err != nil {
return
}
fallthrough
case thumbnail.path != "" && len(thumbnail.content) > 0:
cnt = append(cnt, content{thumbnail.path, thumbnail.path, thumbnail.content})
im.thumbnail = fmt.Sprintf("attach://%s", thumbnail.path)
}
return
}
func processSticker(sticker InputFile) (se stickerEnvelope, cnt []content, err error) {
switch {
case sticker.id != "":
se.Sticker = sticker.id
case sticker.url != "":
se.Sticker = sticker.url
case sticker.path != "" && len(sticker.content) == 0:
if sticker.content, sticker.path, err = readFile(sticker); err != nil {
return
}
fallthrough
case sticker.path != "" && len(sticker.content) > 0:
cnt = append(cnt, content{sticker.path, sticker.path, sticker.content})
se.Sticker = fmt.Sprintf("attach://%s", sticker.path)
}
return
}
func readFile(im InputFile) (content []byte, path string, err error) {
content, err = os.ReadFile(im.path)
if err != nil {
return
}
path = filepath.Base(im.path)
return
}
func toContent(ftype string, f InputFile) (content, error) {
if f.path != "" && len(f.content) == 0 {
var err error
if f.content, f.path, err = readFile(f); err != nil {
return content{}, err
}
}
return content{f.path, ftype, f.content}, nil
}
func toInputMedia(media []GroupableInputMedia) (ret []InputMedia) {
ret = make([]InputMedia, len(media))
for i, v := range media {
ret[i] = v
}
return ret
}
func joinURL(base, endpoint string, vals url.Values) (addr string, err error) {
addr, err = url.JoinPath(base, endpoint)
if err != nil {
return
}
if vals != nil {
if queries := vals.Encode(); queries != "" {
addr = fmt.Sprintf("%s?%s", addr, queries)
}
}
return
}
func processProfilePhoto(photo InputProfilePhoto) (env profilePhotoEnvelope, cnt []content, err error) {
env.content = photo
file := photo.profilePhotoFile()
switch {
case file.id != "":
env.ref = file.id
case file.url != "":
env.ref = file.url
case file.path != "" && len(file.content) == 0:
if file.content, file.path, err = readFile(file); err != nil {
return
}
fallthrough
case file.path != "" && len(file.content) > 0:
cnt = append(cnt, content{file.path, file.path, file.content})
env.ref = fmt.Sprintf("attach://%s", file.path)
}
return
}
func processStoryContent(sc InputStoryContent) (env storyContentEnvelope, cnt []content, err error) {
env.content = sc
file := sc.storyContentFile()
switch {
case file.id != "":
env.ref = file.id
case file.url != "":
env.ref = file.url
case file.path != "" && len(file.content) == 0:
if file.content, file.path, err = readFile(file); err != nil {
return
}
fallthrough
case file.path != "" && len(file.content) > 0:
cnt = append(cnt, content{file.path, file.path, file.content})
env.ref = fmt.Sprintf("attach://%s", file.path)
}
return
}
func itoa(i int64) string {
return strconv.FormatInt(i, 10)
}
func ftoa(f float64) string {
return strconv.FormatFloat(f, 'f', -1, 64)
}
func btoa(b bool) string {
return strconv.FormatBool(b)
}