-
Notifications
You must be signed in to change notification settings - Fork 1
/
localstorage.go
55 lines (45 loc) · 1.45 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
package filestorage
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"github.com/linkai-io/am/am"
"github.com/linkai-io/am/pkg/convert"
)
type LocalStorage struct {
prefixPath string
}
func NewLocalStorage() *LocalStorage {
return &LocalStorage{}
}
func (s *LocalStorage) Init() error {
return nil
}
func (s *LocalStorage) WriteWithHash(ctx context.Context, userContext am.UserContext, address *am.ScanGroupAddress, data []byte, hashName string) (string, error) {
if data == nil || len(data) == 0 {
return "", nil
}
fileName := PathFromData(address, hashName)
if fileName == "null" {
return "", nil
}
dir := filepath.Dir(userContext.GetOrgCID() + fileName)
if err := os.MkdirAll(dir, 0766); err != nil {
return "", err
}
err := ioutil.WriteFile(userContext.GetOrgCID()+fileName, data, 0766)
return fileName, err
}
// Writes the data to local storage, returning the hash and link/path
func (s *LocalStorage) Write(ctx context.Context, userContext am.UserContext, address *am.ScanGroupAddress, data []byte) (string, string, error) {
hashName := convert.HashData(data)
fileName, err := s.WriteWithHash(ctx, userContext, address, data, hashName)
return hashName, userContext.GetOrgCID() + fileName, err
}
func (s *LocalStorage) GetInfraFile(ctx context.Context, pathName, objectName string) ([]byte, error) {
return nil, nil
}
func (s *LocalStorage) PutInfraFile(ctx context.Context, pathName, objectName string, data []byte) error {
return nil
}