Skip to content

Commit 94c1801

Browse files
committed
Refactor mongodb client append_log store images internally in "attachment_data field"
This change ends up being *mostly* backwards compatible since we still store the url, though this will be basically useless in the future. Attachment size is currently hardcoded to 8MB, in the future this should be able to be set by users.
1 parent afba718 commit 94c1801

3 files changed

Lines changed: 63 additions & 12 deletions

File tree

core/clients.py

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,58 @@ async def edit_message(self, message_id: Union[int, str], new_content: str) -> N
645645
{"$set": {"messages.$.content": new_content, "messages.$.edited": True}},
646646
)
647647

648+
async def _handle_attachments(self, message: Message) -> list:
649+
attachments = []
650+
in_db_storage = self.bot.config.get("in_db_storage")
651+
# 8 MB to bytes
652+
image_max_size = 1024 * 1024 * 8
653+
654+
if in_db_storage:
655+
for attachment in message.attachments:
656+
# TODO this is non ideal
657+
# ideally we would async iterate over the attachments so we don't block on io for every attachment
658+
image_data = await attachment.read()
659+
# Don't store images larger than
660+
if len(image_data) > image_max_size:
661+
logger.warning(
662+
"Attachment %s (%s) is too large to store in the database, skipping.",
663+
attachment.filename,
664+
attachment.id,
665+
)
666+
continue
667+
attachments.append(
668+
{
669+
"id": attachment.id,
670+
"filename": attachment.filename,
671+
"is_image": attachment.content_type.startswith("image/"),
672+
"size": len(image_data),
673+
"type": "internal",
674+
# URL points to the original discord URL
675+
"url": attachment.url,
676+
# Attachment data is the raw bytes of the image
677+
"attachment_data": image_data,
678+
"content_type": attachment.content_type,
679+
"width": attachment.width,
680+
"height": attachment.height,
681+
}
682+
)
683+
else:
684+
for attachment in message.attachments:
685+
attachments.append(
686+
{
687+
"id": attachment.id,
688+
"filename": attachment.filename,
689+
"is_image": attachment.content_type.startswith("image/"),
690+
"size": attachment.size,
691+
"url": attachment.url,
692+
"content_type": attachment.content_type,
693+
"width": attachment.width,
694+
"height": attachment.height,
695+
}
696+
)
697+
698+
return attachments
699+
648700
async def append_log(
649701
self,
650702
message: Message,
@@ -668,18 +720,7 @@ async def append_log(
668720
},
669721
"content": message.content,
670722
"type": type_,
671-
"attachments": [
672-
{
673-
"id": a.id,
674-
"filename": a.filename,
675-
# In previous versions this was true for both videos and images
676-
"is_image": a.content_type.startswith("image/"),
677-
"size": a.size,
678-
"url": a.url,
679-
"content_type": a.content_type,
680-
}
681-
for a in message.attachments
682-
],
723+
"attachments": await self._handle_attachments(message),
683724
}
684725

685726
return await self.logs.find_one_and_update(

core/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ class ConfigManager:
179179
"log_level": "INFO",
180180
# data collection
181181
"data_collection": True,
182+
"use_in_database_image_store": True,
182183
}
183184

184185
colors = {"mod_color", "recipient_color", "main_color", "error_color"}

core/config_help.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,5 +1170,14 @@
11701170
"If this configuration is enabled, only roles that are hoisted (displayed seperately in member list) will be used. If a user has no hoisted roles, it will return 'None'.",
11711171
"If you would like to display the top role of a user regardless of if it's hoisted or not, disable `use_hoisted_top_role`."
11721172
]
1173+
},
1174+
"use_in_database_image_store": {
1175+
"default": "Yes",
1176+
"description": "Controls if images should be stored in the database as base64 strings.",
1177+
"examples": [],
1178+
"notes": [
1179+
"If this configuration is enabled, images will be stored in the database. If disabled, images will be stored as Discord CDN links.",
1180+
"This configuration can only be set through `.env` file or environment (config) variables."
1181+
]
11731182
}
11741183
}

0 commit comments

Comments
 (0)