feat: 支持rar资料包导入
This commit is contained in:
@@ -282,6 +282,11 @@ def _expand_uploaded_files(uploaded_files: list) -> list[dict]:
|
||||
expanded_files.extend(extraction["files"])
|
||||
warnings.extend(extraction["warnings"])
|
||||
continue
|
||||
if extension == ".rar":
|
||||
extraction = _extract_rar_entries(uploaded_file)
|
||||
expanded_files.extend(extraction["files"])
|
||||
warnings.extend(extraction["warnings"])
|
||||
continue
|
||||
expanded_files.append(
|
||||
{
|
||||
"relative_path": uploaded_file.name,
|
||||
@@ -435,6 +440,39 @@ def _extract_7z_entries(uploaded_file) -> dict:
|
||||
return {"files": entries, "warnings": warnings}
|
||||
|
||||
|
||||
def _extract_rar_entries(uploaded_file) -> dict:
|
||||
try:
|
||||
import rarfile
|
||||
except ImportError as exc:
|
||||
raise RuntimeError("处理 .rar 资料包需要安装 rarfile。") from exc
|
||||
|
||||
archive_bytes = uploaded_file.read()
|
||||
uploaded_file.seek(0)
|
||||
entries = []
|
||||
warnings = []
|
||||
with rarfile.RarFile(BytesIO(archive_bytes)) as archive:
|
||||
for info in archive.infolist():
|
||||
if info.is_dir():
|
||||
continue
|
||||
relative_path = info.filename.replace("\\", "/")
|
||||
extension = Path(relative_path).suffix.lower()
|
||||
if extension not in {".txt", ".md", ".pdf", ".docx"}:
|
||||
warnings.append(f"跳过不支持的文件:{relative_path}")
|
||||
continue
|
||||
file_data = archive.read(info.filename)
|
||||
extracted_file = SimpleUploadedFile(
|
||||
Path(relative_path).name,
|
||||
file_data,
|
||||
)
|
||||
entries.append(
|
||||
{
|
||||
"relative_path": relative_path,
|
||||
"uploaded_file": extracted_file,
|
||||
}
|
||||
)
|
||||
return {"files": entries, "warnings": warnings}
|
||||
|
||||
|
||||
def _detect_document_role(file_name: str) -> str:
|
||||
normalized = file_name.lower()
|
||||
if "申请表" in file_name:
|
||||
|
||||
Reference in New Issue
Block a user