From 123cab2500d20336b94bcf0e169f840c11ab3e9b Mon Sep 17 00:00:00 2001 From: Brian Bjarke Jensen Date: Sat, 15 Jun 2024 23:16:42 +0200 Subject: [PATCH] updated to use new functions --- .../database/classes/visual_communication.py | 61 ++++++++++++++++--- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/shared/database/classes/visual_communication.py b/shared/database/classes/visual_communication.py index 9a9898b..6d63a67 100755 --- a/shared/database/classes/visual_communication.py +++ b/shared/database/classes/visual_communication.py @@ -10,6 +10,7 @@ from pathlib import Path from minio import Minio from PIL import Image from pydantic import BaseModel +from pymongo.collection import Collection from shared.data_store import get from shared.data_store import put @@ -34,23 +35,60 @@ class VisualCommunication(BaseModel): return cls.__name__ @classmethod - def from_file(cls, path: Path, minio_client: Minio) -> VisualCommunication: - """Instantiate from file.""" - assert isinstance(path, Path) + def upload_image_to_minio( + cls, + image: Image.Image, + minio_client: Minio, + ) -> str: + """Upload image to MinIO and return MD5 checksum of hashed image.""" + assert isinstance(image, Image.Image) assert isinstance(minio_client, Minio) - # determine name - name = path.stem - # open and upload image to minio - image = Image.open(path) - image.load() buffer = BytesIO() image.save(buffer, 'png') object_name = put( client=minio_client, buffer=buffer, ) + return object_name + + @classmethod + def from_name_and_image( + cls, + name: str, + image: Image.Image, + minio_client: Minio, + ) -> VisualCommunication: + """ + Instantiate from filename and image + that is automatically uploaded to MinIO. + """ + assert isinstance(name, str) + assert isinstance(image, Image.Image) + assert isinstance(minio_client, Minio) + # upload file to minio + object_name = VisualCommunication.upload_image_to_minio( + image=image, + minio_client=minio_client, + ) return VisualCommunication(name=name, object_name=object_name) + @classmethod + def from_file(cls, path: Path, minio_client: Minio) -> VisualCommunication: + """Instantiate from file.""" + assert isinstance(path, Path) + assert isinstance(minio_client, Minio) + # determine name + name = path.stem + # open image + image = Image.open(path) + image.load() + # instantiate object + return VisualCommunication.from_name_and_image( + name=name, + image=image, + minio_client=minio_client, + ) + @classmethod def decode_image(cls, content: str) -> Image.Image: """Extract image from webencoded content.""" @@ -69,6 +107,13 @@ class VisualCommunication(BaseModel): im = Image.open(buffer) return im + def save_to_mongo(self, collection: Collection) -> None: + """Save self as document in MongoDB.""" + res = collection.insert_one( + document=self.model_dump(), + ) + assert res.acknowledged + def webencoded_image(self, minio_client: Minio) -> str: """Convert image to be displayed on webpage.""" assert isinstance(minio_client, Minio)