feat: images uploader, endpoint for product images uploading
This commit is contained in:
@ -1,12 +1,11 @@
|
||||
import math
|
||||
|
||||
from fastapi import UploadFile
|
||||
|
||||
from external.s3_uploader import S3Uploader
|
||||
from modules.fulfillment_base.models import Product
|
||||
from modules.fulfillment_base.repositories import ProductRepository
|
||||
from modules.fulfillment_base.schemas.product import (
|
||||
CreateProductRequest,
|
||||
ProductSchema,
|
||||
UpdateProductRequest, GetProductsResponse,
|
||||
)
|
||||
from modules.fulfillment_base.schemas.product import *
|
||||
from schemas.base import PaginationSchema, PaginationInfoSchema
|
||||
from services.mixins import *
|
||||
|
||||
@ -46,5 +45,22 @@ class ProductService(
|
||||
),
|
||||
)
|
||||
|
||||
async def is_soft_delete(self, product: ProductSchema) -> bool:
|
||||
return True
|
||||
async def upload_image(
|
||||
self, product_id: int, upload_file: UploadFile
|
||||
) -> ProductUploadImageResponse:
|
||||
try:
|
||||
product: Product = await self.repository.get_by_id(product_id)
|
||||
s3_uploader = S3Uploader()
|
||||
|
||||
for image in product.images:
|
||||
s3_key = image.image_url.split("/")[-1]
|
||||
await s3_uploader.delete_image(s3_key)
|
||||
await self.repository.delete_images(product.images)
|
||||
|
||||
image_url = await s3_uploader.upload_from_upload_file_obj(upload_file)
|
||||
await self.repository.create_image(product_id, image_url)
|
||||
return ProductUploadImageResponse(
|
||||
ok=True, message="Изображение успешно загружено", image_url=image_url
|
||||
)
|
||||
except Exception as e:
|
||||
return ProductUploadImageResponse(ok=False, message=str(e))
|
||||
|
||||
Reference in New Issue
Block a user