-
Notifications
You must be signed in to change notification settings - Fork 2
/
storage.go
165 lines (152 loc) · 3.14 KB
/
storage.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package account
import (
"encoding/hex"
"errors"
multihash "github.com/jbenet/go-multihash"
"os"
)
type Storage interface {
Store(data []byte) (string, error)
Retrieve(hash string) ([]byte, error)
Put(key string, value string) error
Post(key string, value string) error
Write(hash string, data []byte) error
Get(key string) (string, error)
}
type localStorage struct{}
var accountstore Storage
func InitLocalStorage() {
accountstore = new(localStorage)
}
func SetStorage(store Storage) {
accountstore = store
}
func (l *localStorage) Store(data []byte) (string, error) {
var ch []byte
var err error
if len(data) > 120 {
ch, err = multihash.EncodeName(data[:120], "sha1")
if err != nil {
return "", err
}
} else {
ch, err = multihash.EncodeName(data, "sha1")
if err != nil {
return "", err
}
}
can := hex.EncodeToString(ch)
if _, err := os.Stat("login"); os.IsNotExist(err) {
os.Mkdir("login", 0777)
}
filename := "login/" + can
if _, err := os.Stat(filename); os.IsNotExist(err) {
file, err := os.Create(filename)
if err != nil {
return "", err
}
defer file.Close()
_, err = file.Write(data)
if err != nil {
return "", err
}
file.Sync()
return can, nil
} else {
return "", errors.New("File Exists")
}
return "", nil
}
func (l *localStorage) Retrieve(hash string) ([]byte, error) {
filename := "login/" + hash
stat, err := os.Stat(filename)
if os.IsNotExist(err) {
return nil, err
}
file, err := os.Open(filename)
if err != nil {
return nil, err
}
var data []byte
data = make([]byte, stat.Size())
_, err = file.Read(data)
if err != nil {
return nil, err
}
return data, nil
}
func (l *localStorage) Put(key string, value string) error {
if _, err := os.Stat("login"); os.IsNotExist(err) {
os.Mkdir("login", 0777)
}
filename := "login/" + key
if _, err := os.Stat(filename); os.IsNotExist(err) {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write([]byte(value))
if err != nil {
return err
}
file.Sync()
}
return nil
}
func (l *localStorage) Get(key string) (string, error) {
filename := "login/" + key
stat, err := os.Stat(filename)
if os.IsNotExist(err) {
return "", err
}
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer file.Close()
var data []byte
data = make([]byte, stat.Size())
_, err = file.Read(data)
if err != nil {
return "", err
}
return string(data), nil
}
//Change DHT value
func (l *localStorage) Post(key string, value string) error {
filename := "login/" + key
_, err := os.Stat(filename)
if os.IsNotExist(err) {
return err
}
file, err := os.OpenFile(filename, os.O_RDWR, 0777)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write([]byte(value))
if err != nil {
return err
}
file.Sync()
return nil
}
// Change storage value
func (l *localStorage) Write(hash string, data []byte) error {
filename := "login/" + hash
_, err := os.Stat(filename)
if os.IsNotExist(err) {
return err
}
file, err := os.Open(filename)
if err != nil {
return err
}
_, err = file.Write([]byte(data))
if err != nil {
return err
}
file.Sync()
return nil
}