From 81a2d1036bbe0d2bea3c68449e79e9a5d7d2f0c3 Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 11 Aug 2025 16:13:01 +0300 Subject: [PATCH] add files router and update image URL path --- app/api/v1/__init__.py | 4 +++- app/api/v1/files.py | 21 +++++++++++++++++++++ app/api/v1/product/images.py | 2 +- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 app/api/v1/files.py diff --git a/app/api/v1/__init__.py b/app/api/v1/__init__.py index 8fd37ae..206a020 100644 --- a/app/api/v1/__init__.py +++ b/app/api/v1/__init__.py @@ -3,7 +3,8 @@ from fastapi import APIRouter from app.api.v1 import ( project, board, client, service, product, barcode, deal, shipping_warehouse, statistics, user, role, marketplace, - residues, department, position, payroll, status, auth, task, billing, work_shifts, transaction, time_tracking, shipping + residues, department, position, payroll, status, auth, task, billing, work_shifts, transaction, time_tracking, + shipping, files ) router = APIRouter() @@ -33,3 +34,4 @@ router.include_router(time_tracking.router, prefix='/time-tracking') router.include_router(transaction.router, prefix='/transaction') router.include_router(user.router, prefix='/user') router.include_router(work_shifts.router, prefix='/work-shifts') +router.include_router(files.router, prefix='/files') diff --git a/app/api/v1/files.py b/app/api/v1/files.py new file mode 100644 index 0000000..d75ab35 --- /dev/null +++ b/app/api/v1/files.py @@ -0,0 +1,21 @@ +import os + +from fastapi import APIRouter, HTTPException +from starlette.responses import FileResponse + +from app.api.v1.product.images import PRODUCTS_FOLDER + +router = APIRouter( + prefix="/files", + tags=["Files"] +) + + +@router.get('/images/{file_name}', response_class=FileResponse) +async def get_image(file_name: str): + products_folder = PRODUCTS_FOLDER + file_path = os.path.join(products_folder, file_name) + if not os.path.exists(file_path): + raise HTTPException(status_code=404, detail="File not found") + + return FileResponse(file_path) diff --git a/app/api/v1/product/images.py b/app/api/v1/product/images.py index 5d7b3d1..4cc8687 100644 --- a/app/api/v1/product/images.py +++ b/app/api/v1/product/images.py @@ -32,7 +32,7 @@ async def upload_product_image( with open(file_path, "wb") as file: file.write(await upload_file.read()) - image_url = f"{str(request.base_url).rstrip('/')}/api/files/images/{filename}" + image_url = f"{str(request.base_url).rstrip('/')}/api/v1/files/images/{filename}" return response({ "imageUrl": image_url, "message": "Фото успешно загружено!",