/
fileref.go
289 lines (242 loc) · 7.91 KB
/
fileref.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package fileref
import (
"fmt"
"hash/fnv"
"math"
"strconv"
"strings"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/core/encryption"
)
const CHUNK_SIZE = 64 * 1024
const (
FILE = "f"
DIRECTORY = "d"
)
type Collaborator struct {
RefID int64 `json:"ref_id"`
ClientID string `json:"client_id"`
CreatedAt string `json:"created_at"`
}
type FileRef struct {
Ref `mapstructure:",squash"`
CustomMeta string `json:"custom_meta" mapstructure:"custom_meta"`
ValidationRoot string `json:"validation_root" mapstructure:"validation_root"`
// ValidationRootSignature is signature signed by client for hash_of(ActualFileHashSignature + ValidationRoot)
ValidationRootSignature string `json:"validation_root_signature" mapstructure:"validation_root_signature"`
FixedMerkleRoot string `json:"fixed_merkle_root" mapstructure:"fixed_merkle_root"`
ThumbnailSize int64 `json:"thumbnail_size" mapstructure:"thumbnail_size"`
ThumbnailHash string `json:"thumbnail_hash" mapstructure:"thumbnail_hash"`
ActualFileSize int64 `json:"actual_file_size" mapstructure:"actual_file_size"`
ActualFileHash string `json:"actual_file_hash" mapstructure:"actual_file_hash"`
// ActualFileHashSignature is signature signed by client for ActualFileHash
ActualFileHashSignature string `json:"actual_file_hash_signature" mapstructure:"actual_file_hash_signature"`
ActualThumbnailSize int64 `json:"actual_thumbnail_size" mapstructure:"actual_thumbnail_size"`
ActualThumbnailHash string `json:"actual_thumbnail_hash" mapstructure:"actual_thumbnail_hash"`
MimeType string `json:"mimetype" mapstructure:"mimetype"`
EncryptedKey string `json:"encrypted_key" mapstructure:"encrypted_key"`
EncryptedKeyPoint string `json:"encrypted_key_point" mapstructure:"encrypted_key_point"`
Collaborators []Collaborator `json:"collaborators" mapstructure:"collaborators"`
}
func (fRef *FileRef) MetaID() string {
hash := fnv.New64a()
hash.Write([]byte(fRef.Path))
return strconv.FormatUint(hash.Sum64(), 36)
}
type RefEntity interface {
GetNumBlocks() int64
GetSize() int64
GetFileMetaHash() string
GetHash() string
CalculateHash() string
GetType() string
GetPathHash() string
GetLookupHash() string
GetPath() string
GetName() string
GetFileID() string
GetCreatedAt() common.Timestamp
GetUpdatedAt() common.Timestamp
}
type Ref struct {
Type string `json:"type" mapstructure:"type"`
AllocationID string `json:"allocation_id" mapstructure:"allocation_id"`
Name string `json:"name" mapstructure:"name"`
Path string `json:"path" mapstructure:"path"`
Size int64 `json:"size" mapstructure:"size"`
ActualSize int64 `json:"actual_file_size" mapstructure:"actual_file_size"`
Hash string `json:"hash" mapstructure:"hash"`
ChunkSize int64 `json:"chunk_size" mapstructure:"chunk_size"`
NumBlocks int64 `json:"num_of_blocks" mapstructure:"num_of_blocks"`
PathHash string `json:"path_hash" mapstructure:"path_hash"`
LookupHash string `json:"lookup_hash" mapstructure:"lookup_hash"`
FileID string `json:"file_id" mapstructure:"file_id"`
FileMetaHash string `json:"file_meta_hash" mapstructure:"file_meta_hash"`
ThumbnailHash string `json:"thumbnail_hash" mapstructure:"thumbnail_hash"`
ThumbnailSize int64 `json:"thumbnail_size" mapstructure:"thumbnail_size"`
ActualThumbnailHash string `json:"actual_thumbnail_hash" mapstructure:"actual_thumbnail_hash"`
ActualThumbnailSize int64 `json:"actual_thumbnail_size" mapstructure:"actual_thumbnail_size"`
HashToBeComputed bool
ChildrenLoaded bool
Children []RefEntity `json:"-" mapstructure:"-"`
CreatedAt common.Timestamp `json:"created_at" mapstructure:"created_at"`
UpdatedAt common.Timestamp `json:"updated_at" mapstructure:"updated_at"`
}
func GetReferenceLookup(allocationID string, path string) string {
return encryption.Hash(allocationID + ":" + path)
}
func (r *Ref) CalculateHash() string {
if len(r.Children) == 0 && !r.ChildrenLoaded && !r.HashToBeComputed {
return r.Hash
}
childHashes := make([]string, len(r.Children))
childFileMetaHashes := make([]string, len(r.Children))
childPaths := make([]string, len(r.Children))
var refNumBlocks int64
var size int64
for index, childRef := range r.Children {
childRef.CalculateHash()
childFileMetaHashes[index] = childRef.GetFileMetaHash()
childHashes[index] = childRef.GetHash()
childPaths[index] = childRef.GetPath()
refNumBlocks += childRef.GetNumBlocks()
size += childRef.GetSize()
}
r.FileMetaHash = encryption.Hash(r.GetPath() + strings.Join(childFileMetaHashes, ":"))
r.Hash = encryption.Hash(r.GetHashData() + strings.Join(childHashes, ":"))
r.PathHash = encryption.Hash(strings.Join(childPaths, ":"))
r.NumBlocks = refNumBlocks
r.Size = size
return r.Hash
}
func (r *Ref) GetFileMetaHash() string {
return r.FileMetaHash
}
func (r *Ref) GetHash() string {
return r.Hash
}
func (r *Ref) GetHashData() string {
return fmt.Sprintf("%s:%s:%s", r.AllocationID, r.Path, r.FileID)
}
func (r *Ref) GetType() string {
return r.Type
}
func (r *Ref) GetNumBlocks() int64 {
return r.NumBlocks
}
func (r *Ref) GetSize() int64 {
return r.Size
}
func (r *Ref) GetPathHash() string {
return r.PathHash
}
func (r *Ref) GetLookupHash() string {
return r.LookupHash
}
func (r *Ref) GetPath() string {
return r.Path
}
func (r *Ref) GetName() string {
return r.Name
}
func (r *Ref) GetFileID() string {
return r.FileID
}
func (r *Ref) GetCreatedAt() common.Timestamp {
return r.CreatedAt
}
func (r *Ref) GetUpdatedAt() common.Timestamp {
return r.UpdatedAt
}
func (r *Ref) AddChild(child RefEntity) {
if r.Children == nil {
r.Children = make([]RefEntity, 0)
}
var index int
var ltFound bool // less than found
// Add child in sorted fashion
for i, ref := range r.Children {
if strings.Compare(child.GetPath(), ref.GetPath()) == -1 {
index = i
ltFound = true
break
}
}
if ltFound {
r.Children = append(r.Children[:index+1], r.Children[index:]...)
r.Children[index] = child
} else {
r.Children = append(r.Children, child)
}
r.ChildrenLoaded = true
}
func (r *Ref) RemoveChild(idx int) {
if idx < 0 {
return
}
r.Children = append(r.Children[:idx], r.Children[idx+1:]...)
}
func (fr *FileRef) GetFileMetaHash() string {
return fr.FileMetaHash
}
func (fr *FileRef) GetFileMetaHashData() string {
return fmt.Sprintf(
"%s:%d:%d:%s",
fr.Path, fr.Size,
fr.ActualFileSize, fr.ActualFileHash)
}
func (fr *FileRef) GetHashData() string {
return fmt.Sprintf(
"%s:%s:%s:%s:%d:%s:%s:%d:%s:%d:%s",
fr.AllocationID,
fr.Type, // don't need to add it as well
fr.Name, // don't see any utility as fr.Path below has name in it
fr.Path,
fr.Size,
fr.ValidationRoot,
fr.FixedMerkleRoot,
fr.ActualFileSize,
fr.ActualFileHash,
fr.ChunkSize,
fr.FileID,
)
}
func (fr *FileRef) GetHash() string {
return fr.Hash
}
func (fr *FileRef) CalculateHash() string {
fr.Hash = encryption.Hash(fr.GetHashData())
fr.FileMetaHash = encryption.Hash(fr.GetFileMetaHashData())
fr.NumBlocks = int64(math.Ceil(float64(fr.Size*1.0) / CHUNK_SIZE))
return fr.Hash
}
func (fr *FileRef) GetType() string {
return fr.Type
}
func (fr *FileRef) GetNumBlocks() int64 {
return fr.NumBlocks
}
func (fr *FileRef) GetSize() int64 {
return fr.Size
}
func (fr *FileRef) GetPathHash() string {
return fr.PathHash
}
func (fr *FileRef) GetLookupHash() string {
return fr.LookupHash
}
func (fr *FileRef) GetPath() string {
return fr.Path
}
func (fr *FileRef) GetName() string {
return fr.Name
}
func (fr *FileRef) GetFileID() string {
return fr.FileID
}
func (fr *FileRef) GetCreatedAt() common.Timestamp {
return fr.CreatedAt
}
func (fr *FileRef) GetUpdatedAt() common.Timestamp {
return fr.UpdatedAt
}