🐛 fix(upload): 校验旧会话末片实际大小
This commit is contained in:
1 parent
c2d743ba51
commit
5c1e1ef368
2 files changed
+31
No files matched your search
@@ -85,6 +85,12 @@ class FileStorageInterface:
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@staticmethod
|
||||
def validate_stored_chunk_size(chunk_info: UploadChunk, index: int, data: bytes):
|
||||
expected = min(chunk_info.chunk_size, chunk_info.file_size - index * chunk_info.chunk_size)
|
||||
if len(data) != expected:
|
||||
raise ValueError(f"分片{index}大小与上传声明不一致,请重新上传")
|
||||
|
||||
async def generate_presigned_upload_url(self, save_path: str, expires_in: int = 900) -> Optional[str]:
|
||||
"""
|
||||
生成预签名上传URL
|
||||
@@ -245,6 +251,7 @@ class SystemFileStorage(FileStorageInterface):
|
||||
raise ValueError(f"分片{i}文件不存在")
|
||||
async with aiofiles.open(chunk_path, "rb") as in_file:
|
||||
chunk_data = await in_file.read()
|
||||
self.validate_stored_chunk_size(chunk_info, i, chunk_data)
|
||||
current_hash = hashlib.sha256(chunk_data).hexdigest()
|
||||
if current_hash != chunk_record.chunk_hash:
|
||||
raise ValueError(f"分片{i}哈希不匹配: 期望 {chunk_record.chunk_hash}, 实际 {current_hash}")
|
||||
@@ -489,6 +496,7 @@ class S3FileStorage(FileStorageInterface):
|
||||
except Exception as e:
|
||||
raise ValueError(f"分片{i}文件不存在: {e}")
|
||||
|
||||
self.validate_stored_chunk_size(chunk_info, i, chunk_data)
|
||||
current_hash = hashlib.sha256(chunk_data).hexdigest()
|
||||
if current_hash != chunk_record.chunk_hash:
|
||||
raise ValueError(f"分片{i}哈希不匹配: 期望 {chunk_record.chunk_hash}, 实际 {current_hash}")
|
||||
@@ -843,6 +851,7 @@ class OneDriveFileStorage(FileStorageInterface):
|
||||
except Exception as e:
|
||||
raise ValueError(f"分片{i}文件不存在: {e}")
|
||||
|
||||
self.validate_stored_chunk_size(chunk_info, i, chunk_data)
|
||||
current_hash = hashlib.sha256(chunk_data).hexdigest()
|
||||
if current_hash != chunk_record.chunk_hash:
|
||||
raise ValueError(f"分片{i}哈希不匹配: 期望 {chunk_record.chunk_hash}, 实际 {current_hash}")
|
||||
@@ -1009,6 +1018,7 @@ class OpenDALFileStorage(FileStorageInterface):
|
||||
except Exception as e:
|
||||
raise ValueError(f"分片{i}文件不存在: {e}")
|
||||
|
||||
self.validate_stored_chunk_size(chunk_info, i, chunk_data)
|
||||
current_hash = hashlib.sha256(chunk_data).hexdigest()
|
||||
if current_hash != chunk_record.chunk_hash:
|
||||
raise ValueError(f"分片{i}哈希不匹配: 期望 {chunk_record.chunk_hash}, 实际 {current_hash}")
|
||||
@@ -1293,6 +1303,7 @@ class WebDAVFileStorage(WebDAVStreamMixin, WebDAVOffsetMixin, FileStorageInterfa
|
||||
chunk_data = await resp.read()
|
||||
|
||||
# 验证哈希
|
||||
self.validate_stored_chunk_size(chunk_info, i, chunk_data)
|
||||
current_hash = hashlib.sha256(chunk_data).hexdigest()
|
||||
if current_hash != chunk_record.chunk_hash:
|
||||
raise ValueError(f"分片{i}哈希不匹配: 期望 {chunk_record.chunk_hash}, 实际 {current_hash}")
|
||||
|
||||
Reference in new issue
Block a user