Добавлен роутер на скачивание файлов
All checks were successful
Build And Push / publish (push) Successful in 2m44s

This commit is contained in:
2026-04-14 14:54:40 +03:00
parent 416bd576d9
commit ce8b0238ce
7 changed files with 67 additions and 10 deletions

View File

@ -1,6 +1,6 @@
[project] [project]
name = "HospitalAssistantBackend" name = "HospitalAssistantBackend"
version = "1.4.1" version = "1.5.0"
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"

View File

View File

View File

@ -0,0 +1,33 @@
from logging import getLogger
from fastapi import APIRouter, Response
from orjson import loads
from shared import exceptions as e
from shared.redis import client as cache
logger = getLogger(__name__)
router = APIRouter(
prefix='/download',
tags=[
'Download',
],
)
@router.get('/{token}')
async def download_file(token: str):
file_data = await cache.get(f'download:{token}')
if file_data is None:
raise e.NotFoundException
data = loads(file_data)
return Response(
content=data['data'],
media_type=data['content_type'],
headers={
'Content-Disposition': f'attachment; filename="{data["filename"]}"'
},
)

View File

@ -5,7 +5,7 @@ from logging import getLogger
from secrets import token_urlsafe from secrets import token_urlsafe
from typing import Annotated from typing import Annotated
from fastapi import APIRouter, Body, Depends, Response, UploadFile, status from fastapi import APIRouter, Body, Depends, UploadFile, status
from orjson import loads from orjson import loads
from apps.remd.dependencies import convert_aemd_to_pdf, get_parsable_ids from apps.remd.dependencies import convert_aemd_to_pdf, get_parsable_ids
@ -119,12 +119,21 @@ async def get_vaccs_report_download(
file_bytes = base64.b64decode(file.content) file_bytes = base64.b64decode(file.content)
filename = f'vaccs_report_{resultId or user.vita_id}.doc' filename = f'vaccs_report_{resultId or user.vita_id}.doc'
return Response( temp_link_token = token_urlsafe(32)
content=file_bytes, await cache.set(
media_type='application/msword', f'download:{temp_link_token}',
headers={'Content-Disposition': f'attachment; filename="{filename}"'}, dumps(
{
'filename': filename,
'content_type': 'application/msword',
'data': file_bytes,
}
),
ex=600,
) )
return s.DownloadFile(link=temp_link_token)
@router.get('/getMedExamDict') @router.get('/getMedExamDict')
async def get_med_exam_dict(user: Annotated[User, Depends(login)]): async def get_med_exam_dict(user: Annotated[User, Depends(login)]):
@ -296,12 +305,21 @@ async def get_aemd_file_download(
pdf_bytes = await convert_aemd_to_pdf(decoded, docKind) pdf_bytes = await convert_aemd_to_pdf(decoded, docKind)
filename = f'{emdrId}.pdf' filename = f'{emdrId}.pdf'
return Response( temp_link_token = token_urlsafe(32)
content=pdf_bytes, await cache.set(
media_type='application/pdf', f'download:{temp_link_token}',
headers={'Content-Disposition': f'attachment; filename="{filename}"'}, dumps(
{
'filename': filename,
'content_type': 'application/pdf',
'data': pdf_bytes,
}
),
ex=600,
) )
return s.DownloadFile(link=temp_link_token)
@router.post('/measurement', status_code=status.HTTP_202_ACCEPTED) @router.post('/measurement', status_code=status.HTTP_202_ACCEPTED)
async def measurement( async def measurement(

View File

@ -33,3 +33,7 @@ class Notifications(TypedDict):
class Complaints(BaseModel): class Complaints(BaseModel):
complaints: str complaints: str
class DownloadFile(BaseModel):
link: str

View File

@ -1,6 +1,7 @@
from fastapi import APIRouter, HTTPException from fastapi import APIRouter, HTTPException
from apps.default.v1.router import router as default_router from apps.default.v1.router import router as default_router
from apps.download.v1.router import router as download_router
from apps.esia.v1.router import router as esia_router from apps.esia.v1.router import router as esia_router
from apps.remd.v1.router import router as remd_router from apps.remd.v1.router import router as remd_router
from apps.tmk.v1.router import router as tmk_router from apps.tmk.v1.router import router as tmk_router
@ -17,6 +18,7 @@ router.include_router(users_router)
router.include_router(remd_router) router.include_router(remd_router)
router.include_router(vitacore_router) router.include_router(vitacore_router)
router.include_router(tmk_router) router.include_router(tmk_router)
router.include_router(download_router)
openapi_schema = get_openapi_schema(router) openapi_schema = get_openapi_schema(router)
swagger_ui_html = get_swagger_html(router) swagger_ui_html = get_swagger_html(router)