/
store.go
66 lines (51 loc) · 1.45 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
61
62
63
64
65
66
package jetcapture
import (
"context"
"io"
"os"
"path"
"time"
)
type BlockStore[K DestKey] interface {
// Write takes a block and writes it out to a "final" destination as specified by the deskKey, dir and file name.
// returns a stringified version of the destination, number of bytes written, and the duration
// TODO(jonathan): should the block be an io.ReadCloser?
Write(ctx context.Context, block io.Reader, destKey K, dir, fileName string) (string, int64, time.Duration, error)
}
var (
_ BlockStore[string] = &LocalFSStore[string]{}
)
type LocalFSStore[K DestKey] struct {
Resolver func(destKey K) (string, error)
}
func (f *LocalFSStore[K]) Write(_ context.Context, block io.Reader, destKey K, dir, fileName string) (string, int64, time.Duration, error) {
start := time.Now()
p, err := f.Resolver(destKey)
if err != nil {
return "", 0, 0, err
}
p = path.Join(p, dir)
if err := os.MkdirAll(p, 0755); err != nil {
return "", 0, 0, err
}
p = path.Join(p, fileName)
log.Debugf("writing block to %s", p)
fout, err := os.Create(p)
if err != nil {
return p, 0, 0, err
}
defer fout.Close()
n, err := io.Copy(fout, block)
if err != nil {
return p, n, time.Since(start), err
}
return p, n, time.Since(start), fout.Close()
}
func SingleDirStore[K DestKey](path string) BlockStore[K] {
return &LocalFSStore[K]{
Resolver: func(K) (string, error) {
// do nothing with K and just return a static path
return path, nil
},
}
}