-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathfile.go
More file actions
39 lines (32 loc) · 750 Bytes
/
Copy pathfile.go
File metadata and controls
39 lines (32 loc) · 750 Bytes
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
package main
import (
"context"
"encoding/hex"
"os"
"path"
altda "github.com/ethereum-optimism/optimism/op-alt-da"
)
type FileStore struct {
directory string
}
func NewFileStore(directory string) *FileStore {
return &FileStore{
directory: directory,
}
}
func (s *FileStore) Get(ctx context.Context, key []byte) ([]byte, error) {
data, err := os.ReadFile(s.fileName(key))
if err != nil {
if os.IsNotExist(err) {
return nil, altda.ErrNotFound
}
return nil, err
}
return data, nil
}
func (s *FileStore) Put(ctx context.Context, key []byte, value []byte) error {
return os.WriteFile(s.fileName(key), value, 0600)
}
func (s *FileStore) fileName(key []byte) string {
return path.Join(s.directory, hex.EncodeToString(key))
}