-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
185 lines (149 loc) · 3.44 KB
/
util.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/h2non/filetype"
"github.com/labstack/gommon/log"
)
func GetEnv(key string, defaultVal string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return defaultVal
}
func GetEnvAsInt(name string, defaultVal int) int {
valueStr := GetEnv(name, "")
if value, err := strconv.Atoi(valueStr); err == nil {
return value
}
return defaultVal
}
func FileExists(name string) bool {
_, err := os.Stat(name)
if os.IsNotExist(err) {
return false
}
//sometimes there can be permission or other errors
//here we use a simple logic that if file exists and we can use it then true otherwise false
return err == nil
}
type FileType int
const (
PHOTO FileType = iota
AUDIO
VIDEO
OTHER
)
func GetFileType(path string) FileType {
file, err := os.Open(path)
if err != nil {
log.Error("Failed to determine file type of "+path, err)
return OTHER
}
defer file.Close()
head := make([]byte, 261)
if _, err = file.Read(head); err != nil {
log.Error("Failed to read file header of "+path, err)
return OTHER
}
if filetype.IsImage(head) {
return PHOTO
} else if filetype.IsVideo(head) {
return VIDEO
} else if filetype.IsAudio(head) {
return AUDIO
}
return OTHER
}
func ParseFileType(fileType string) FileType {
fileType = strings.TrimSpace(strings.ToLower(fileType))
switch fileType {
case "video":
return VIDEO
case "audio":
return AUDIO
case "photo":
return PHOTO
default:
return OTHER
}
}
func isValidUrl(toTest string) bool {
_, err := url.ParseRequestURI(toTest)
if err != nil {
return false
}
u, err := url.Parse(toTest)
if err != nil || u.Scheme == "" || u.Host == "" {
return false
}
return true
}
func doPOST(aURL string, payload map[string]interface{}, headers map[string]interface{}, timeoutSec int) (string, error) {
bytesRepresentation, err := json.Marshal(payload)
if err != nil {
return "", err
}
client := &http.Client{Timeout: time.Second * time.Duration(timeoutSec)}
req, err := http.NewRequest("POST", aURL, bytes.NewBuffer(bytesRepresentation))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
for key, val := range headers {
req.Header.Set(key, fmt.Sprintf("%v", val))
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(data), nil
}
func doGET(aURL string, params map[string]interface{}, headers map[string]interface{}, timeoutSec int) (string, error) {
urlParams := url.Values{}
for key, val := range params {
urlParams.Add(key, fmt.Sprintf("%v", val))
}
req, err := http.NewRequest("GET", aURL, nil)
if err != nil {
return "", err
}
for key, val := range headers {
req.Header.Set(key, fmt.Sprintf("%v", val))
}
req.URL.RawQuery = urlParams.Encode()
client := &http.Client{Timeout: time.Second * time.Duration(timeoutSec)}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(data), nil
}
func ReadFile(path string) (string, error) {
if !FileExists(path) {
return "", fmt.Errorf("File %s not found", path)
}
fileContent, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return string(fileContent), nil
}