forked from hashgraph/hedera-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_info.go
85 lines (74 loc) · 1.98 KB
/
file_info.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
package hedera
import (
protobuf "github.com/golang/protobuf/proto"
"github.com/hashgraph/hedera-sdk-go/v2/proto"
"time"
)
type FileInfo struct {
FileID FileID
Size int64
ExpirationTime time.Time
IsDeleted bool
Keys KeyList
FileMemo string
}
func newFileInfo(fileID FileID, size int64, expirationTime time.Time, isDeleted bool, keys KeyList, fileMemo string) FileInfo {
return FileInfo{
FileID: fileID,
Size: size,
ExpirationTime: expirationTime,
IsDeleted: isDeleted,
Keys: keys,
FileMemo: fileMemo,
}
}
func fileInfoFromProtobuf(fileInfo *proto.FileGetInfoResponse_FileInfo) (FileInfo, error) {
var keys KeyList
var err error
if fileInfo.Keys != nil {
keys, err = keyListFromProtobuf(fileInfo.Keys)
if err != nil {
return FileInfo{}, err
}
}
return FileInfo{
FileID: fileIDFromProtobuf(fileInfo.FileID),
Size: fileInfo.Size,
ExpirationTime: timeFromProtobuf(fileInfo.ExpirationTime),
IsDeleted: fileInfo.Deleted,
Keys: keys,
FileMemo: fileInfo.Memo,
}, nil
}
func (fileInfo *FileInfo) toProtobuf() *proto.FileGetInfoResponse_FileInfo {
return &proto.FileGetInfoResponse_FileInfo{
FileID: fileInfo.FileID.toProtobuf(),
Size: fileInfo.Size,
ExpirationTime: &proto.Timestamp{
Seconds: int64(fileInfo.ExpirationTime.Second()),
Nanos: int32(fileInfo.ExpirationTime.Nanosecond()),
},
Deleted: fileInfo.IsDeleted,
Keys: fileInfo.Keys.toProtoKeyList(),
Memo: fileInfo.FileMemo,
}
}
func (fileInfo FileInfo) ToBytes() []byte {
data, err := protobuf.Marshal(fileInfo.toProtobuf())
if err != nil {
return make([]byte, 0)
}
return data
}
func FileInfoFromBytes(data []byte) (FileInfo, error) {
pb := proto.FileGetInfoResponse_FileInfo{}
err := protobuf.Unmarshal(data, &pb)
if err != nil {
return FileInfo{}, err
}
info, err := fileInfoFromProtobuf(&pb)
if err != nil {
return FileInfo{}, err
}
return info, nil
}