-
Notifications
You must be signed in to change notification settings - Fork 204
/
file.go
57 lines (44 loc) · 1.1 KB
/
file.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
package configuration
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
"net/http"
log "github.com/sirupsen/logrus"
)
func WriteFile(filePath string, data []byte) error {
basePath := filepath.Dir(filePath)
fileName := filepath.Base(filePath)
log.Debug(basePath)
err := os.MkdirAll(basePath, 0744)
if err != nil {
return err
}
return ioutil.WriteFile(basePath+"/"+fileName, data, 0644)
}
func ReadFile(filePath string) ([]byte, error) {
if strings.HasPrefix(filePath, "http://") || strings.HasPrefix(filePath, "https://") {
return DownloadFile(filePath)
}
data, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, errors.New("File not found: " + filePath)
}
return data, nil
}
func DownloadFile(filePath string) ([]byte, error) {
response, err := http.Get(filePath)
if err != nil {
log.Info(err.Error())
return nil, errors.New("Could not download simulation")
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Info(err.Error())
return nil, errors.New("Could not download simulation")
}
return body, nil
}