75 lines
2.1 KiB
Python
75 lines
2.1 KiB
Python
"""
|
|
********************************************
|
|
* @Date: 2024 09 27
|
|
* @Description: BlobObject存储对象
|
|
********************************************
|
|
"""
|
|
|
|
import hashlib
|
|
import os
|
|
import zlib
|
|
import aiofiles
|
|
|
|
|
|
class BlobObject:
|
|
object_id: str
|
|
__buff_size: int = 8192
|
|
|
|
def __init__(self, file_path: str) -> None:
|
|
self.file_path = file_path
|
|
self.object_id = self.__contentSha1()
|
|
|
|
async def writeBlob(self, base_path: str) -> None:
|
|
Folder = base_path + "/" + self.object_id[:2]
|
|
if not os.path.exists(Folder):
|
|
os.makedirs(Folder)
|
|
await self.__compressFile(
|
|
self.file_path,
|
|
base_path + "/" + self.object_id[:2] + "/" + self.object_id[2:],
|
|
)
|
|
|
|
def getBlobAbsPath(self) -> str:
|
|
"""
|
|
获取blob的绝对路径,此相对路径是基于存储路径的,不是相对当前文件的路径
|
|
|
|
:return 相对路径,格式为xx/xxxx...
|
|
"""
|
|
return self.object_id[:2] + "/" + self.object_id[2:]
|
|
|
|
async def __compressFile(self, file_path: str, save_path: str) -> None:
|
|
"""
|
|
压缩文件
|
|
|
|
:param file_path: 要压缩的文件路径
|
|
:param save_path: 保存路径
|
|
:return
|
|
"""
|
|
compresser = zlib.compressobj(9)
|
|
async with aiofiles.open(save_path, mode='wb') as wf, aiofiles.open(file_path, mode='rb') as rf:
|
|
while True:
|
|
data = await rf.read(self.__buff_size)
|
|
if not data:
|
|
break
|
|
compressedData = compresser.compress(data)
|
|
await wf.write(compressedData)
|
|
await wf.write(compresser.flush())
|
|
|
|
|
|
def __contentSha1(self) -> str:
|
|
"""
|
|
计算文件内容的sha1值
|
|
|
|
:return sha1的hex值
|
|
"""
|
|
with open(self.file_path, "rb") as f:
|
|
sha1 = hashlib.sha1()
|
|
while True:
|
|
data = f.read(self.__buff_size)
|
|
if not data:
|
|
break
|
|
sha1.update(data)
|
|
return sha1.hexdigest()
|
|
|
|
|
|
def newBlobObject(file_path: str) -> BlobObject:
|
|
return BlobObject(file_path) |