-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfs.go
121 lines (102 loc) · 2.76 KB
/
rfs.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
package acclient
import (
"bytes"
"io"
"net/http"
"os"
"strings"
"github.com/subiz/log"
)
var RFSHOST = "http://db-0:2306/"
var rfs_secret = ""
func init() {
rfs_host := os.Getenv("RFS_HOST")
if rfs_host != "" {
RFSHOST = rfs_host
}
rfs_secret = os.Getenv("RFS_SECRET")
}
func convertPathToRFSUrl(path string) string {
return RFSHOST + strings.TrimPrefix(path, "/") + "?x-secret=" + rfs_secret
}
func RemoveFile(path string) error {
url := convertPathToRFSUrl(path)
req, _ := http.NewRequest("DELETE", url, nil)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return log.ERetry(err, log.M{"url": url})
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return log.ERetry(err, log.M{"url": url, "status_code": resp.StatusCode})
}
return nil
}
func TruncateFile(path string) error {
return WriteFileBytes(path, []byte(""))
}
func WriteFile(path string, stream io.Reader) error {
url := convertPathToRFSUrl(path)
// resp, err := http.Post(url, "application/octet-stream", bytes.NewBuffer(data))
resp, err := http.Post(url, "application/octet-stream", stream)
if err != nil {
return log.ERetry(err, log.M{"url": url})
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return log.ERetry(err, log.M{"url": url, "status_code": resp.StatusCode})
}
return nil
}
func WriteFileBytes(path string, data []byte) error {
return WriteFile(path, bytes.NewBuffer(data))
}
func ReadFile(path string) (io.ReadCloser, error) {
url := convertPathToRFSUrl(path)
resp, err := http.Get(url)
if err != nil {
return nil, log.ERetry(err, log.M{"url": url})
}
if resp.StatusCode == 404 {
resp.Body.Close()
return nil, os.ErrNotExist
}
return resp.Body, nil
}
func ReadFileBytes(path string) ([]byte, error) {
stream, err := ReadFile(path)
if err != nil {
return nil, err
}
defer stream.Close()
return io.ReadAll(stream)
}
func WriteFilePipe(path string, predicate func(*os.File) error) error {
tmpFile, err := os.CreateTemp("/tmp", "rfs")
if err != nil {
return log.EFS(err, "/tmp/rfs", log.M{"path": path})
}
defer os.Remove(tmpFile.Name())
if err := predicate(tmpFile); err != nil {
tmpFile.Close()
return log.EFS(err, tmpFile.Name(), log.M{"path": path})
}
tmpFile.Close()
tmpFile, err = os.OpenFile(tmpFile.Name(), os.O_RDONLY, 0600)
if err != nil {
return log.EFS(err, tmpFile.Name(), log.M{"path": path})
}
defer tmpFile.Close()
url := convertPathToRFSUrl(path)
// resp, err := http.Post(url, "application/octet-stream", bytes.NewBuffer(data))
resp, err := http.Post(url, "application/octet-stream", tmpFile)
if err != nil {
return log.ERetry(err, log.M{"url": url})
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return log.ERetry(err, log.M{"url": url, "status_code": resp.StatusCode})
}
return nil
}