feat: 支持rar资料包导入
This commit is contained in:
@@ -5,7 +5,7 @@ from django import forms
|
||||
from apps.scenarios.services import ScenarioNotFound, get_scenario
|
||||
from apps.scenarios.services import list_scenarios
|
||||
|
||||
SUPPORTED_EXTENSIONS = {".txt", ".md", ".pdf", ".docx", ".zip", ".7z"}
|
||||
SUPPORTED_EXTENSIONS = {".txt", ".md", ".pdf", ".docx", ".zip", ".7z", ".rar"}
|
||||
|
||||
|
||||
class MultipleFileInput(forms.ClearableFileInput):
|
||||
@@ -52,7 +52,7 @@ class DocumentUploadForm(forms.Form):
|
||||
return uploaded_file
|
||||
extension = Path(uploaded_file.name).suffix.lower()
|
||||
if extension not in SUPPORTED_EXTENSIONS:
|
||||
raise forms.ValidationError("仅支持 .txt、.md、.pdf、.docx、.zip 和 .7z 文件")
|
||||
raise forms.ValidationError("仅支持 .txt、.md、.pdf、.docx、.zip、.7z 和 .rar 文件")
|
||||
return uploaded_file
|
||||
|
||||
def clean_files(self):
|
||||
@@ -60,7 +60,7 @@ class DocumentUploadForm(forms.Form):
|
||||
for uploaded_file in uploaded_files:
|
||||
extension = Path(uploaded_file.name).suffix.lower()
|
||||
if extension not in SUPPORTED_EXTENSIONS:
|
||||
raise forms.ValidationError("仅支持 .txt、.md、.pdf、.docx、.zip 和 .7z 文件")
|
||||
raise forms.ValidationError("仅支持 .txt、.md、.pdf、.docx、.zip、.7z 和 .rar 文件")
|
||||
return uploaded_files
|
||||
|
||||
def clean(self):
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -76,7 +76,7 @@ def upload(request):
|
||||
"form": form,
|
||||
"scenarios": list_scenarios(),
|
||||
"upload_checks": [
|
||||
"文件格式支持 PDF、DOCX、MD、TXT、ZIP 与 7Z 资料包",
|
||||
"文件格式支持 PDF、DOCX、MD、TXT、ZIP、7Z 与 RAR 资料包",
|
||||
"业务资料与法规依据资料需分开归属",
|
||||
"支持一次上传多份文件并归并到同一个资料包",
|
||||
"目录类文件会优先参与完整性校验",
|
||||
|
||||
Reference in New Issue
Block a user