/
localStorage.go
91 lines (81 loc) 路 1.96 KB
/
localStorage.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
package drivers
import (
"context"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"strings"
)
type LocalStorage struct {
root string
domain string
}
func NewLocalStorage(root string, domain string) *LocalStorage {
return &LocalStorage{root, domain}
}
func (s *LocalStorage) Write(ctx context.Context, path string, contents string, config map[string]interface{}) error {
fullPath := s.root + "/" + path
paths, _ := filepath.Split(fullPath)
err := os.MkdirAll(paths, 0777)
if err != nil {
return err
}
f, err := os.Create(fullPath)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(contents)
if err != nil {
return err
}
return nil
}
func (s *LocalStorage) WriteStream(ctx context.Context, path string, stream io.Reader, config map[string]interface{}) error {
fullPath := s.root + "/" + path
paths, _ := filepath.Split(fullPath)
err := os.MkdirAll(paths, 0777)
if err != nil {
return err
}
f, err := os.Create(fullPath)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, stream)
if err != nil {
return err
}
return nil
}
func (s *LocalStorage) Read(ctx context.Context, path string) (string, error) {
fullPath := s.root + "/" + path
contents, err := os.ReadFile(fullPath)
if err != nil {
return "", err
}
return string(contents), nil
}
func (s *LocalStorage) ReadStream(ctx context.Context, path string) (io.Reader, error) {
fullPath := s.root + "/" + path
f, err := os.Open(fullPath)
if err != nil {
return nil, err
}
return f, nil
}
func (s *LocalStorage) Delete(ctx context.Context, path string) error {
fullPath := s.root + "/" + path
return os.Remove(fullPath)
}
func (s *LocalStorage) PublicUrl(ctx context.Context, path string) (string, error) {
srcUrl := fmt.Sprintf("%s/%s", strings.TrimRight(s.domain, "/"), path)
srcUri, _ := url.Parse(srcUrl)
return srcUri.String(), nil
}
func (s *LocalStorage) PrivateUrl(ctx context.Context, path string) (string, error) {
return s.domain + "/" + path, nil
}