This repository has been archived by the owner on Aug 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 247
/
utils.go
81 lines (76 loc) · 1.7 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
package goinsta
import (
"encoding/json"
"image"
// Required for getImageDimensionFromReader in jpg and png format
_ "image/jpeg"
_ "image/png"
"io"
"strconv"
"unsafe"
)
func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func toString(i interface{}) string {
switch s := i.(type) {
case string:
return s
case bool:
return strconv.FormatBool(s)
case float64:
return strconv.FormatFloat(s, 'f', -1, 64)
case float32:
return strconv.FormatFloat(float64(s), 'f', -1, 32)
case int:
return strconv.Itoa(s)
case int64:
return strconv.FormatInt(s, 10)
case int32:
return strconv.Itoa(int(s))
case int16:
return strconv.FormatInt(int64(s), 10)
case int8:
return strconv.FormatInt(int64(s), 10)
case uint:
return strconv.FormatInt(int64(s), 10)
case uint64:
return strconv.FormatInt(int64(s), 10)
case uint32:
return strconv.FormatInt(int64(s), 10)
case uint16:
return strconv.FormatInt(int64(s), 10)
case uint8:
return strconv.FormatInt(int64(s), 10)
case []byte:
return b2s(s)
case error:
return s.Error()
}
return ""
}
func prepareRecipients(cc interface{}) (bb string, err error) {
var b []byte
ids := make([][]int64, 0)
switch c := cc.(type) {
case *Conversation:
for i := range c.Users {
ids = append(ids, []int64{c.Users[i].ID})
}
case *Item:
ids = append(ids, []int64{c.User.ID})
case int64:
ids = append(ids, []int64{c})
}
b, err = json.Marshal(ids)
bb = b2s(b)
return
}
// getImageDimensionFromReader return image dimension , types is .jpg and .png
func getImageDimensionFromReader(rdr io.Reader) (int, int, error) {
image, _, err := image.DecodeConfig(rdr)
if err != nil {
return 0, 0, err
}
return image.Width, image.Height, nil
}