保存分片文件到本地文件系统 :param upload_id: 上传会话ID :param chunk_index: 分片索引 :param chunk_data: 分片数据 :param chunk_hash: 分片哈希值 :param save_path: 文件保存路径
(self, upload_id: str, chunk_index: int, chunk_data: bytes, chunk_hash: str, save_path: str)
| 163 | ) |
| 164 | |
| 165 | async def save_chunk(self, upload_id: str, chunk_index: int, chunk_data: bytes, chunk_hash: str, save_path: str): |
| 166 | """ |
| 167 | 保存分片文件到本地文件系统 |
| 168 | :param upload_id: 上传会话ID |
| 169 | :param chunk_index: 分片索引 |
| 170 | :param chunk_data: 分片数据 |
| 171 | :param chunk_hash: 分片哈希值 |
| 172 | :param save_path: 文件保存路径 |
| 173 | """ |
| 174 | chunk_dir = self.root_path / save_path |
| 175 | chunk_path = chunk_dir.parent / 'chunks' / upload_id / f"{chunk_index}.part" |
| 176 | if not chunk_path.parent.exists(): |
| 177 | chunk_path.parent.mkdir(parents=True, exist_ok=True) |
| 178 | # 使用临时文件写入,确保原子性 |
| 179 | temp_path = chunk_path.with_suffix('.tmp') |
| 180 | try: |
| 181 | async with aiofiles.open(temp_path, "wb") as f: |
| 182 | await f.write(chunk_data) |
| 183 | # 原子重命名 |
| 184 | temp_path.rename(chunk_path) |
| 185 | except Exception as e: |
| 186 | if temp_path.exists(): |
| 187 | temp_path.unlink() |
| 188 | raise e |
| 189 | |
| 190 | async def merge_chunks(self, upload_id: str, chunk_info: UploadChunk, save_path: str) -> tuple[str, str]: |
| 191 | """ |