-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
60 lines (47 loc) · 1.06 KB
/
store.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
package file
import (
"io"
"net/url"
"os"
"path"
"simplex.sh/errors"
"simplex.sh/store"
)
func init() {
store.Register("file", Open)
}
func Open(u *url.URL) (store.Store, error) {
if u.Host != "" {
wd, err := os.Getwd()
if err != nil {
return nil, errors.Forward(err, "Failed to open store at `%s`", u)
}
u.Path = path.Join(wd, u.Host+u.Path)
u.Host = ""
}
return file_store_t(u.Path), nil
}
type file_store_t string
func (f file_store_t) GetBlob(name string) (io.ReadCloser, error) {
r, err := os.Open(path.Join(string(f), name))
if err != nil {
if os.IsNotExist(err) {
err = store.NotFoundError(name)
} else {
err = errors.Forward(err, "Unable to Get() `%s`", name)
}
}
return r, err
}
func (f file_store_t) SetBlob(name string) (io.WriteCloser, error) {
p := path.Join(string(f), name)
err := os.MkdirAll(path.Dir(p), 0755)
if err != nil {
return nil, errors.Forward(err, "Unable to Set() `%s`", name)
}
w, err := os.Create(p)
if err != nil {
err = errors.Forward(err, "Unable to Set() `%s`", name)
}
return w, err
}