-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathchat_link_code.py
More file actions
48 lines (35 loc) · 1.13 KB
/
chat_link_code.py
File metadata and controls
48 lines (35 loc) · 1.13 KB
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
"""
@project: MaxKB
@Author: niu
@file: chat_link_code.py
@date: 2026/2/9 11:31
@desc:
"""
from typing import Union
import uuid_utils.compat as uuid
class UUIDEncoder:
BASE62_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
BASE62_LEN = 62
@staticmethod
def encode(uuid_obj: Union[uuid.UUID, str] = None) -> str:
if uuid_obj is None:
uuid_obj = uuid.uuid7()
elif isinstance(uuid_obj, str):
uuid_obj = uuid.UUID(uuid_obj)
num = int(uuid_obj.hex, 16)
if num == 0:
return UUIDEncoder.BASE62_ALPHABET[0]
result = []
while num:
num, rem = divmod(num,62)
result.append(UUIDEncoder.BASE62_ALPHABET[rem])
return ''.join(reversed(result))
@staticmethod
def decode(encoded: str) -> uuid.UUID:
num = 0
for char in encoded:
num = num * UUIDEncoder.BASE62_LEN + UUIDEncoder.BASE62_ALPHABET.index(char)
return uuid.UUID(int=num)
@staticmethod
def decode_to_str(encoded: str) -> str:
return str(UUIDEncoder.decode(encoded))