异步优化写入备份

This commit is contained in:
2024-11-12 20:30:19 +08:00
parent 8626664653
commit f33d61e315
21 changed files with 379 additions and 69 deletions

View File

@@ -8,6 +8,7 @@
import hashlib
import os
import zlib
import aiofiles
class BlobObject:
@@ -16,45 +17,45 @@ class BlobObject:
def __init__(self, file_path: str) -> None:
self.file_path = file_path
self.object_id = self.contentSha1()
self.object_id = self.__contentSha1()
def writeBlob(self, base_path) -> None:
Folder = base_path + "/" + self.__getFolderName()
async def writeBlob(self, base_path: str) -> None:
Folder = base_path + "/" + self.object_id[:2]
if not os.path.exists(Folder):
os.makedirs(Folder)
self.__compressFile(
await self.__compressFile(
self.file_path,
base_path + "/" + self.__getFolderName() + "/" + self.__getFileName(),
base_path + "/" + self.object_id[:2] + "/" + self.object_id[2:],
)
def __compressFile(self, file_path, save_path) -> None:
compresser = zlib.compressobj(9)
write_file = open(save_path, "wb")
with open(file_path, "rb") as f:
while True:
data = f.read(self.__buff_size)
if not data:
break
compressedData = compresser.compress(data)
write_file.write(compressedData)
write_file.write(compresser.flush())
write_file.close()
def __getFolderName(self) -> str:
return self.object_id[:2]
def __getFileName(self) -> str:
return self.object_id[2:]
def getBlobAbsPath(self) -> str:
"""
获取blob的绝对路径,此相对路径是基于存储路径的,不是相对当前文件的路径
:return 相对路径,格式为xx/xxxx...
"""
return self.__getFolderName() + "/" + self.__getFileName()
return self.object_id[:2] + "/" + self.object_id[2:]
def contentSha1(self) -> str:
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值
@@ -68,3 +69,7 @@ class BlobObject:
break
sha1.update(data)
return sha1.hexdigest()
def newBlobObject(file_path: str) -> BlobObject:
return BlobObject(file_path)