-
Notifications
You must be signed in to change notification settings - Fork 1
/
local.go
92 lines (81 loc) · 1.66 KB
/
local.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
package fstorage
import (
"bytes"
"context"
"crypto/sha1"
"encoding/hex"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
type localoss struct {
path string
lm map[string]struct{}
}
func NewLocalOSS(path string) *localoss {
return &localoss{
path: path,
lm: map[string]struct{}{},
}
}
func (m *localoss) Put(
ctx context.Context,
ext string,
data []byte,
) (string, error) {
return m.PutReader(ctx, ext, bytes.NewReader(data), int64(len(data)))
}
func (m *localoss) PutReader(
ctx context.Context,
ext string,
r io.Reader,
l int64,
) (string, error) {
name := sha(strconv.Itoa(int(time.Now().UnixNano())))
if strings.HasPrefix(ext, ".") {
name += ext
} else {
name += "." + ext
}
if _, ok := m.lm[name[:4]]; !ok {
if err := os.MkdirAll(filepath.Join(m.path, name[:2], name[2:4]), 0700); err != nil {
return "", err
}
m.lm[name[:4]] = struct{}{}
}
n := filepath.Join(name[:2], name[2:4], name)
file, err := os.Create(filepath.Join(m.path, n))
if err != nil {
return "", err
}
defer file.Close()
if _, err := io.Copy(file, r); err != nil {
return "", err
}
return n, nil
}
func (m *localoss) Get(ctx context.Context, name string) ([]byte, error) {
r, err := m.GetReader(ctx, name)
if err != nil {
return nil, err
}
defer r.Close()
return io.ReadAll(r)
}
func (m *localoss) GetReader(
ctx context.Context,
name string,
) (io.ReadCloser, error) {
return os.Open(filepath.Join(m.path, name))
}
func (m *localoss) Del(ctx context.Context, name string) error {
return os.Remove(filepath.Join(m.path, name))
}
func sha(s string) string {
m := sha1.New()
m.Write([]byte(s))
return hex.EncodeToString(m.Sum(nil))
}