move_images_to_minio #44
+47
-29
@@ -16,11 +16,11 @@ from pydantic import ValidationError
|
||||
from pymongo.collection import Collection
|
||||
|
||||
from .layout import app_layout
|
||||
from shared.data_store import delete as delete_from_minio
|
||||
from shared.database import count_documents
|
||||
from shared.database import get_visual_communication
|
||||
from shared.database import NoDocumentFoundException
|
||||
from shared.database import upsert_annotation
|
||||
from shared.database import upsert_visual_communication
|
||||
from shared.database import VisualCommunication
|
||||
from shared.dto import ModelData
|
||||
|
||||
@@ -120,34 +120,52 @@ def init_app(
|
||||
filename_list: list[str] | None,
|
||||
) -> tuple[str | None, str | None]:
|
||||
"""Upload image to database through web ui."""
|
||||
# stop early if possible
|
||||
if content_list is None or filename_list is None:
|
||||
logging.info('nothing to upload.')
|
||||
return None, 'nothing to upload'.title()
|
||||
# build list of visual communication
|
||||
vis_com_list = []
|
||||
for content, filename in zip(content_list, filename_list):
|
||||
try:
|
||||
image = VisualCommunication.decode_image(content=content)
|
||||
vis_com = VisualCommunication(
|
||||
name=filename,
|
||||
image=image,
|
||||
)
|
||||
except Exception as exc:
|
||||
logging.error('failed handling data from file: %s', filename)
|
||||
logging.debug(exc)
|
||||
continue
|
||||
vis_com_list.append(vis_com)
|
||||
# upsert documents
|
||||
success = upsert_visual_communication(
|
||||
collection=mongo_collection,
|
||||
visual_communication_list=vis_com_list,
|
||||
)
|
||||
if success:
|
||||
logging.info('uploaded %s files to database', len(vis_com_list))
|
||||
return 'successfully uploaded images'.title(), None
|
||||
logging.error('failed uploading images to database')
|
||||
return None, 'failed uploading images'.title()
|
||||
try:
|
||||
# stop if no input
|
||||
assert (
|
||||
content_list is not None
|
||||
) and (
|
||||
filename_list is not None
|
||||
), 'nothing to upload'
|
||||
# handle input
|
||||
failed_filename_list = []
|
||||
for content, filename in zip(content_list, filename_list):
|
||||
try:
|
||||
# decode image content
|
||||
image = VisualCommunication.decode_image(content=content)
|
||||
except Exception:
|
||||
logging.debug('failed decoding %s', filename)
|
||||
failed_filename_list.append(filename)
|
||||
continue
|
||||
try:
|
||||
# instantiate to upload image to minio
|
||||
vis_com = VisualCommunication.from_name_and_image(
|
||||
name=filename,
|
||||
image=image,
|
||||
minio_client=minio_client,
|
||||
)
|
||||
except Exception as exc:
|
||||
logging.debug(exc)
|
||||
failed_filename_list.append(filename)
|
||||
continue
|
||||
try:
|
||||
# save to mongodb
|
||||
vis_com.save_to_mongo(collection=mongo_collection)
|
||||
except Exception as exc:
|
||||
logging.debug(exc)
|
||||
failed_filename_list.append(filename)
|
||||
# remove document from minio
|
||||
delete_from_minio(
|
||||
client=minio_client,
|
||||
object_name=vis_com.object_name,
|
||||
)
|
||||
assert len(failed_filename_list) == 0, f"failed uploading:{
|
||||
'\n'.join(failed_filename_list)
|
||||
}"
|
||||
except Exception as exc:
|
||||
logging.debug(exc)
|
||||
return None, str(exc).title()
|
||||
return 'successfully uploaded images'.title(), None
|
||||
|
||||
# define callback: cycle visual communication data
|
||||
@app.callback(
|
||||
|
||||
Reference in New Issue
Block a user