-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathutils.go
99 lines (79 loc) · 1.69 KB
/
utils.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
package vkapi
import (
"encoding/json"
"fmt"
"net/url"
"os"
"strconv"
"strings"
)
type ResolveScreenName struct {
Type string `json:"type"`
ObjectID int `json:"object_id"`
}
type ShortLink struct {
ShortUrl string `json:"short_url"`
Accesskey string `json:"access_key"`
Key string `json:"key"`
Url string `json:"url"`
}
func (client *VKClient) ResolveScreenName(name string) (ResolveScreenName, error) {
var res ResolveScreenName
params := url.Values{}
params.Set("screen_name", name)
resp, err := client.MakeRequest("utils.resolveScreenName", params)
if err == nil {
json.Unmarshal(resp.Response, &res)
}
if res.ObjectID == 0 || res.Type == "" {
err = fmt.Errorf("%s not found", name)
}
return res, err
}
func (client *VKClient) GetShortLink(toUrl string, private int) (*ShortLink, error) {
params := url.Values{}
params.Set("url", toUrl)
params.Set("private", strconv.Itoa(private))
resp, err := client.MakeRequest("utils.getShortLink", params)
if err != nil {
return nil, err
}
var shortLink *ShortLink
json.Unmarshal(resp.Response, &shortLink)
return shortLink, nil
}
func ArrayToStr(a []int) string {
s := []string{}
for _, num := range a {
s = append(s, strconv.Itoa(num))
}
return strings.Join(s, ",")
}
func BoolToInt(a bool) int {
if a {
return 1
}
return 0
}
func IntToBool(a int) bool {
if a > 0 {
return true
}
return false
}
func GetFilesSizeMB(files []string) (int, error) {
var size int64
for _, f := range files {
file, err := os.Open(f)
if err != nil {
return 0, err
}
fi, err := file.Stat()
if err != nil {
return 0, err
}
size += fi.Size()
file.Close()
}
return int(size / 1048576), nil
}