feat: CRUD for product barcode images
This commit is contained in:
1
barcodes/images_uploader/__init__.py
Normal file
1
barcodes/images_uploader/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .images_uploader import BarcodeImagesUploader
|
||||
18
barcodes/images_uploader/base.py
Normal file
18
barcodes/images_uploader/base.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from abc import abstractmethod
|
||||
|
||||
from fastapi import UploadFile
|
||||
|
||||
|
||||
class BaseImagesUploader:
|
||||
|
||||
@abstractmethod
|
||||
def get_url(self, filename: str) -> bytes:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def delete(self, filename: str):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def upload(self, upload_file: UploadFile) -> str:
|
||||
pass
|
||||
46
barcodes/images_uploader/images_uploader.py
Normal file
46
barcodes/images_uploader/images_uploader.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
from aioshutil import copyfileobj
|
||||
from fastapi import UploadFile
|
||||
|
||||
from barcodes.images_uploader.base import BaseImagesUploader
|
||||
from barcodes.pdf import PDFGenerator
|
||||
from constants import APP_PATH, API_ROOT
|
||||
|
||||
|
||||
class BarcodeImagesUploader(BaseImagesUploader):
|
||||
def __init__(self):
|
||||
self.relative_path = Path("static/images/product_barcodes")
|
||||
self.storage_path = APP_PATH / self.relative_path
|
||||
if not Path.exists(self.storage_path):
|
||||
Path.mkdir(self.storage_path)
|
||||
|
||||
def get_url(self, filename: str) -> str:
|
||||
file_location = self.relative_path / filename
|
||||
return f"{API_ROOT}/{file_location}"
|
||||
|
||||
def delete(self, filename: str):
|
||||
file_location = self.storage_path / filename
|
||||
if file_location.exists():
|
||||
file_location.unlink()
|
||||
|
||||
async def upload(self, upload_file: UploadFile) -> str:
|
||||
# Create temp file in filesystem
|
||||
temp_filename = str(uuid4()) + '.' + upload_file.filename.split('.')[-1]
|
||||
temp_file_location = self.storage_path / temp_filename
|
||||
with open(temp_file_location, 'wb') as buffer:
|
||||
await copyfileobj(upload_file.file, buffer)
|
||||
|
||||
# Generate PDF file and save it
|
||||
res_filename = str(uuid4()) + '.pdf'
|
||||
res_file_location = f"{self.storage_path}/{res_filename}"
|
||||
temp_file_url = f"{self.relative_path}/{temp_filename}"
|
||||
|
||||
pdf_gen = PDFGenerator()
|
||||
pdf_gen.generate_barcode_image(temp_file_url, res_file_location)
|
||||
|
||||
# Remove temp file
|
||||
self.delete(temp_filename)
|
||||
|
||||
return res_filename
|
||||
@@ -7,6 +7,7 @@ from reportlab.lib.pagesizes import mm
|
||||
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
||||
from reportlab.pdfbase import pdfmetrics
|
||||
from reportlab.pdfbase.ttfonts import TTFont
|
||||
from reportlab.pdfgen import canvas
|
||||
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
|
||||
|
||||
from constants import APP_PATH
|
||||
@@ -181,3 +182,10 @@ class PDFGenerator:
|
||||
|
||||
buffer.seek(0)
|
||||
return buffer
|
||||
|
||||
def generate_barcode_image(self, barcode_image_url: str, path_to_save_pdf: str):
|
||||
print(type(path_to_save_pdf))
|
||||
print(path_to_save_pdf)
|
||||
c = canvas.Canvas(path_to_save_pdf, pagesize=(self.page_width, self.page_height))
|
||||
c.drawImage(barcode_image_url, 0, 0, width=self.page_width, height=self.page_height)
|
||||
c.save()
|
||||
|
||||
Reference in New Issue
Block a user