2023-09-11 00:46:56 +08:00
|
|
|
# define a object type enum
|
|
|
|
|
from abc import ABC, abstractmethod
|
2023-09-15 14:01:26 +08:00
|
|
|
from enum import IntEnum
|
2023-09-11 00:46:56 +08:00
|
|
|
from .hash import HashValue
|
|
|
|
|
import base58
|
|
|
|
|
import base36
|
|
|
|
|
|
|
|
|
|
|
2023-09-15 14:01:26 +08:00
|
|
|
class ObjectType(IntEnum):
|
2023-09-11 00:46:56 +08:00
|
|
|
Chunk = 7
|
|
|
|
|
Image = 101
|
2023-09-11 02:05:24 +08:00
|
|
|
Video = 102
|
|
|
|
|
Document = 103
|
|
|
|
|
RichText = 104
|
|
|
|
|
Email = 105
|
2023-09-11 00:46:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# define a object ID class to identify a object
|
|
|
|
|
class ObjectID: # pylint: disable=too-few-public-methods
|
2023-09-14 20:09:20 +08:00
|
|
|
def __init__(self, value: bytes):
|
2023-09-11 00:46:56 +08:00
|
|
|
assert len(value) == 32, "ObjectID must be 32 bytes long"
|
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return self.to_base58()
|
|
|
|
|
|
|
|
|
|
def to_base58(self):
|
|
|
|
|
return base58.b58encode(self.value).decode()
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def from_base58(s):
|
|
|
|
|
return ObjectID(base58.b58decode(s))
|
|
|
|
|
|
|
|
|
|
def to_base36(self):
|
|
|
|
|
# Convert the bytes to int before encoding
|
|
|
|
|
num = int.from_bytes(self.value, "big")
|
|
|
|
|
return base36.dumps(num)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def from_base36(s):
|
|
|
|
|
# Decode to int and then convert to bytes
|
|
|
|
|
num = base36.loads(s)
|
|
|
|
|
return ObjectID(num.to_bytes((num.bit_length() + 7) // 8, "big"))
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def new_chunk_id(chunk_hash: HashValue):
|
2023-09-18 11:28:21 +08:00
|
|
|
assert len(chunk_hash.value) == 32, "ObjectID must be 32 bytes long"
|
|
|
|
|
return ObjectID(bytes([ObjectType.Chunk]) + chunk_hash.value[1:])
|
|
|
|
|
|
|
|
|
|
def get_object_type(self) -> ObjectType:
|
|
|
|
|
return ObjectType(self.value[0])
|
2023-09-11 00:46:56 +08:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def hash_data(data: bytes):
|
2023-09-14 20:09:20 +08:00
|
|
|
return ObjectID.new_chunk_id(HashValue.hash_data(data))
|
|
|
|
|
|
|
|
|
|
def __eq__(self, other) -> bool:
|
|
|
|
|
return self.value == other.value
|