-
Notifications
You must be signed in to change notification settings - Fork 1
/
uriget.go
137 lines (112 loc) · 2.51 KB
/
uriget.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
package main
import (
"bytes"
"errors"
"io"
"io/ioutil"
"net/http"
"os"
"regexp"
)
var regex_url *regexp.Regexp = regexp.MustCompile("http.*")
func LoadURI(uri string) ([]byte, error) {
if regex_url.MatchString(uri) {
return DownloadFileToBytes(uri)
} else {
return LoadFile(uri)
}
}
func AppendFile(filename string, content []byte) bool {
F, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.ModePerm)
F.Write(content)
F.Close()
return !PrintError(err)
}
func WriteFile(filename string, content []byte) bool {
F, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, os.ModePerm)
F.Write(content)
F.Close()
return !PrintError(err)
}
func FileExists(filename string) bool {
_, err := os.Stat(filename)
if err == nil {
return true
}
if errors.Is(err, os.ErrNotExist) {
return false
}
return true
}
func MakeDir(name string) bool {
return !PrintError(os.MkdirAll(name, os.ModePerm))
}
func DeleteFiles(name string) bool {
println("Deleting file " + name)
return !PrintError(os.RemoveAll(name))
}
func LoadURIToString(uri string) (string, error) {
B, err := LoadURI(uri)
return string(B), err
}
func LoadFile(filename string) ([]byte, error) {
file, ferr := ioutil.ReadFile(filename)
return file, ferr
}
func LoadFileToString(filename string) (string, error) {
F, err := LoadFile(filename)
return string(F), err
}
func DownloadToFile(filepath string, url string) error {
body, derr := DownloadFileToStream(url)
if derr != nil {
return derr
}
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, body)
PrintError(body.Close())
return err
}
func DownloadFileToStream(url string) (io.ReadCloser, error) {
// Get the data
resp, err := http.Get(url)
if err != nil {
return nil, err
}
// defer resp.Body.Close()
return resp.Body, nil
}
func DownloadFileToBytes(url string) ([]byte, error) {
str, err := DownloadFileToStream(url)
if err != nil {
return nil, err
}
out := StreamToByte(str)
PrintError(str.Close())
return out, nil
}
func DownloadFileToString(url string) (string, error) {
str, err := DownloadFileToStream(url)
if err != nil {
return "", err
}
out := StreamToString(str)
PrintError(str.Close())
return out, nil
}
func StreamToByte(stream io.Reader) []byte {
buf := new(bytes.Buffer)
buf.ReadFrom(stream)
return buf.Bytes()
}
func StreamToString(stream io.Reader) string {
buf := new(bytes.Buffer)
buf.ReadFrom(stream)
return buf.String()
}