Фикс скачивания файла
All checks were successful
Build And Push / publish (push) Successful in 3m48s

This commit is contained in:
2026-04-16 18:01:05 +03:00
parent c1c176d642
commit eed99d99ea
2 changed files with 22 additions and 14 deletions

View File

@ -1,6 +1,6 @@
[project] [project]
name = "HospitalAssistantBackend" name = "HospitalAssistantBackend"
version = "1.5.9" version = "1.5.10"
description = "Backend for Hospital Assistant" description = "Backend for Hospital Assistant"
readme = "README.md" readme = "README.md"
requires-python = ">=3.13,<3.14" requires-python = ">=3.13,<3.14"
@ -37,7 +37,6 @@ dependencies = [
"typer-slim==0.16.1", "typer-slim==0.16.1",
"xsdata[cli]>=26.2", "xsdata[cli]>=26.2",
"xsdata-pydantic>=24.5", "xsdata-pydantic>=24.5",
"chardet>=7.4.3",
] ]
[dependency-groups] [dependency-groups]

View File

@ -5,7 +5,6 @@ from logging import getLogger
from secrets import token_urlsafe from secrets import token_urlsafe
from typing import Annotated from typing import Annotated
import chardet
from fastapi import APIRouter, Body, Depends, UploadFile, status from fastapi import APIRouter, Body, Depends, UploadFile, status
from orjson import loads from orjson import loads
from xsdata.formats.dataclass.parsers.config import ParserConfig from xsdata.formats.dataclass.parsers.config import ParserConfig
@ -120,27 +119,37 @@ async def get_vaccs_report_download(
""" """
if resultId is not None: if resultId is not None:
file = await c.vitacore_api.getDiagResultFile(resultId) file = await c.vitacore_api.getDiagResultFile(resultId)
if file is None:
raise e.BadRequestException
filename = f'vaccs_report_{resultId}'
file_extension = 'pdf'
content_type = 'application/pdf'
xml_data = base64.b64decode(file.content)
xml_data = xml_data.decode('utf-8').strip()
xml_model = parser.from_string(xml_data, ClinicalDocument)
file_string = xml_model.component.non_xmlbody.text.value.strip()
else: else:
file = await c.vitacore_api.getVaccsReport(user.vita_id) file = await c.vitacore_api.getVaccsReport(user.vita_id)
if file is None: if file is None:
raise e.BadRequestException raise e.BadRequestException
filename = f'vaccs_report_{resultId or user.vita_id}.pdf' filename = f'vaccs_report_{token_urlsafe(10)}'
xml_data = base64.b64decode(file.content) file_extension = 'doc'
result = chardet.detect(xml_data) content_type = 'application/msword'
xml_data = xml_data.decode(result['encoding'] or 'utf-8').strip() file_string = file.content
xml_model = parser.from_string(xml_data, ClinicalDocument)
pdf_string = xml_model.component.non_xmlbody.text.value.strip()
temp_link_token = token_urlsafe(32) temp_link_token = token_urlsafe(32)
await cache.set( await cache.set(
f'download:{temp_link_token}', f'download:{temp_link_token}',
dumps( dumps(
{ {
'filename': filename, 'filename': f'{filename}.{file_extension}',
'content_type': 'application/pdf', 'content_type': content_type,
'data': pdf_string, 'data': file_string,
} }
), ),
ex=600, ex=600,