/
puid.go
254 lines (220 loc) · 6.26 KB
/
puid.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
package hms
import (
"crypto/rand"
"encoding/base32"
"encoding/binary"
"encoding/xml"
"time"
jsoniter "github.com/json-iterator/go"
"gopkg.in/yaml.v3"
)
var json = jsoniter.ConfigFastest
// Puid_t represents integer form of path unique ID.
type Puid_t uint64
// Unix_t is UNIX time in milliseconds.
type Unix_t uint64
// Predefined PUIDs.
const (
PUIDhome Puid_t = 1
PUIDlocal Puid_t = 2
PUIDremote Puid_t = 3
PUIDshares Puid_t = 4
PUIDmedia Puid_t = 5
PUIDvideo Puid_t = 6
PUIDaudio Puid_t = 7
PUIDimage Puid_t = 8
PUIDbooks Puid_t = 9
PUIDtexts Puid_t = 10
PUIDmap Puid_t = 11
PUIDcache = 32 // first PUID of file system paths
)
// Categories paths constants.
const (
CPhome = "<home>"
CPlocal = "<local>"
CPremote = "<remote>"
CPshares = "<shares>"
CPmedia = "<media>"
CPvideo = "<video>"
CPaudio = "<audio>"
CPimage = "<image>"
CPbooks = "<books>"
CPtexts = "<texts>"
CPmap = "<map>"
)
var CatNames = map[string]string{
CPhome: "Home",
CPlocal: "Local disks",
CPremote: "Network disks",
CPshares: "Shared resources",
CPmedia: "Multimedia files",
CPvideo: "Movie and video files",
CPaudio: "Music and audio files",
CPimage: "Photos and images",
CPbooks: "Books",
CPtexts: "Text files",
CPmap: "Map",
}
// CatKeyPath is predefined read-only maps with PUIDs keys and categories values.
var CatKeyPath = map[Puid_t]string{
PUIDhome: CPhome,
PUIDlocal: CPlocal,
PUIDremote: CPremote,
PUIDshares: CPshares,
PUIDmedia: CPmedia,
PUIDvideo: CPvideo,
PUIDaudio: CPaudio,
PUIDimage: CPimage,
PUIDbooks: CPbooks,
PUIDtexts: CPtexts,
PUIDmap: CPmap,
}
// CatPathKey is predefined read-only map with categories keys and PUIDs values.
var CatPathKey = map[string]Puid_t{
CPhome: PUIDhome,
CPlocal: PUIDlocal,
CPremote: PUIDremote,
CPshares: PUIDshares,
CPmedia: PUIDmedia,
CPvideo: PUIDvideo,
CPaudio: PUIDaudio,
CPimage: PUIDimage,
CPbooks: PUIDbooks,
CPtexts: PUIDtexts,
CPmap: PUIDmap,
}
// Produce base32 string representation of given random bytes slice.
var idenc = base32.HexEncoding.WithPadding(base32.NoPadding)
// String converts path unique ID to base32 string representation.
func (pt Puid_t) String() string {
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], uint64(pt))
var n int
for n = 7; n >= 0 && buf[n] == 0; n-- {
}
return idenc.EncodeToString(buf[:n+1])
}
// Set writes base32 string representation of ID into integer value.
func (pt *Puid_t) Set(puid string) error {
var buf [8]byte
_, err := idenc.Decode(buf[:], S2B(puid))
*pt = Puid_t(binary.LittleEndian.Uint64(buf[:]))
return err
}
// MarshalJSON is JSON marshaler interface implementation.
func (pt Puid_t) MarshalJSON() ([]byte, error) {
return json.Marshal(pt.String())
}
// UnmarshalJSON is JSON unmarshaler interface implementation.
func (pt *Puid_t) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
return pt.Set(s)
}
// MarshalYAML is YAML marshaler interface implementation.
func (pt Puid_t) MarshalYAML() (any, error) {
return pt.String(), nil
}
// UnmarshalYAML is YAML unmarshaler interface implementation.
func (pt *Puid_t) UnmarshalYAML(value *yaml.Node) error {
return pt.Set(value.Value)
}
// MarshalXML is XML marshaler interface implementation.
func (pt Puid_t) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(pt.String(), start)
}
// UnmarshalXML is XML unmarshaler interface implementation.
func (pt *Puid_t) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var s string
if err := d.DecodeElement(&s, &start); err != nil {
return err
}
return pt.Set(s)
}
// Rand generates random identifier of given length in bits, maximum 64 bits.
// Multiply x5 to convert length in base32 symbols to bits.
func (pt *Puid_t) Rand(bits int) {
var buf [8]byte
if _, err := rand.Read(buf[:(bits+7)/8]); err != nil {
panic(err)
}
*pt = Puid_t(binary.LittleEndian.Uint64(buf[:]))
*pt &= 0xffffffffffffffff >> (65 - bits) // throw one more bit to prevent string representation overflow
}
func (ut Unix_t) Time() time.Time {
return time.Unix(int64(ut/1000), int64(ut%1000)*1000000)
}
const ExifDate = "2006-01-02 15:04:05.999"
func (ut Unix_t) String() string {
return ut.Time().Format(ExifDate)
}
// ToDB is Conversion interface implementation for XORM engine.
func (ut Unix_t) ToDB() ([]byte, error) {
return S2B(ut.Time().Format(ExifDate)), nil
}
// FromDB is Conversion interface implementation for XORM engine.
func (ut *Unix_t) FromDB(b []byte) (err error) {
var t time.Time
if t, err = time.Parse(ExifDate, B2S(b)); err != nil {
return
}
*ut = UnixJS(t)
return
}
// MarshalJSON is JSON marshaler interface implementation.
func (ut Unix_t) MarshalJSON() ([]byte, error) {
return json.Marshal(ut.Time().Format(ExifDate))
}
// UnmarshalJSON is JSON unmarshaler interface implementation.
func (ut *Unix_t) UnmarshalJSON(b []byte) (err error) {
var s string
if err = json.Unmarshal(b, &s); err != nil {
return err
}
var t time.Time
if t, err = time.Parse(ExifDate, s); err != nil {
return
}
*ut = UnixJS(t)
return
}
// MarshalYAML is YAML marshaler interface implementation.
func (ut Unix_t) MarshalYAML() (any, error) {
return ut.Time().Format(ExifDate), nil
}
// UnmarshalYAML is YAML unmarshaler interface implementation.
func (ut *Unix_t) UnmarshalYAML(value *yaml.Node) (err error) {
var t time.Time
if t, err = time.Parse(ExifDate, value.Value); err != nil {
return
}
*ut = UnixJS(t)
return
}
// MarshalXML is XML marshaler interface implementation.
func (ut Unix_t) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(ut.Time().Format(ExifDate), start)
}
// UnmarshalXML is XML unmarshaler interface implementation.
func (ut *Unix_t) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
var s string
if err = d.DecodeElement(&s, &start); err != nil {
return err
}
var t time.Time
if t, err = time.Parse(ExifDate, s); err != nil {
return
}
*ut = UnixJS(t)
return
}
// UnixJS converts time to UNIX-time in milliseconds, compatible with javascript time format.
func UnixJS(u time.Time) Unix_t {
return Unix_t(u.UnixMilli())
}
// UnixJSNow returns same result as Date.now() in javascript.
func UnixJSNow() Unix_t {
return Unix_t(time.Now().UnixMilli())
}