2024-02-06 08:41:56 +00:00
|
|
|
from message_system_db_manager import DatabaseManager
|
|
|
|
from message_system_db_model import MessageAttachment, Attachment, P2PMessage
|
|
|
|
import json
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DatabaseManagerAttachments(DatabaseManager):
|
|
|
|
def __init__(self, uri='sqlite:///freedata-messages.db'):
|
|
|
|
super().__init__(uri)
|
|
|
|
|
|
|
|
|
|
|
|
def add_attachment(self, session, message, attachment_data):
|
|
|
|
hash_sha512 = hashlib.sha512(attachment_data['data'].encode()).hexdigest()
|
|
|
|
existing_attachment = session.query(Attachment).filter_by(hash_sha512=hash_sha512).first()
|
|
|
|
|
|
|
|
if not existing_attachment:
|
|
|
|
attachment = Attachment(
|
|
|
|
name=attachment_data['name'],
|
|
|
|
data_type=attachment_data['type'],
|
|
|
|
data=attachment_data['data'],
|
|
|
|
checksum_crc32=attachment_data.get('checksum_crc32', ''),
|
|
|
|
hash_sha512=hash_sha512
|
|
|
|
)
|
|
|
|
session.add(attachment)
|
|
|
|
session.flush() # Ensure the attachment is persisted and has an ID
|
2024-02-07 22:03:55 +00:00
|
|
|
self.log(f"Added attachment to database: {attachment.name}")
|
2024-02-06 08:41:56 +00:00
|
|
|
else:
|
|
|
|
attachment = existing_attachment
|
2024-02-07 22:03:55 +00:00
|
|
|
self.log(f"Attachment {attachment.name} already exists in database")
|
2024-02-06 08:41:56 +00:00
|
|
|
|
|
|
|
# Link the message and the attachment through MessageAttachment
|
|
|
|
link = MessageAttachment(message=message, attachment=attachment)
|
|
|
|
session.add(link)
|
2024-02-07 22:03:55 +00:00
|
|
|
self.log(f"Linked message with attachment: {attachment.name}")
|
|
|
|
|
2024-02-06 08:41:56 +00:00
|
|
|
return attachment
|
|
|
|
|
|
|
|
def get_attachments_by_message_id(self, message_id):
|
|
|
|
session = self.get_thread_scoped_session()
|
|
|
|
try:
|
|
|
|
# Fetch the message by its ID
|
|
|
|
message = session.query(P2PMessage).filter_by(id=message_id).first()
|
|
|
|
if message:
|
|
|
|
# Navigate through the association to get attachments
|
|
|
|
attachments = [ma.attachment.to_dict() for ma in message.message_attachments]
|
|
|
|
return attachments
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
except Exception as e:
|
|
|
|
self.log(f"Error fetching attachments for message ID {message_id}: {e}", isWarning=True)
|
|
|
|
return []
|
|
|
|
finally:
|
|
|
|
session.remove()
|
|
|
|
|
|
|
|
def get_attachments_by_message_id_json(self, message_id):
|
|
|
|
attachments = self.get_attachments_by_message_id(message_id)
|
2024-02-06 19:09:20 +00:00
|
|
|
return json.dumps(attachments)
|
|
|
|
|
|
|
|
def get_attachment_by_sha512(self, hash_sha512):
|
|
|
|
session = self.get_thread_scoped_session()
|
|
|
|
try:
|
|
|
|
attachment = session.query(Attachment).filter_by(hash_sha512=hash_sha512).first()
|
|
|
|
if attachment:
|
2024-02-06 19:33:36 +00:00
|
|
|
return attachment.to_dict()
|
2024-02-06 19:09:20 +00:00
|
|
|
else:
|
|
|
|
self.log(f"No attachment found with SHA-512 hash: {hash_sha512}")
|
|
|
|
return None
|
|
|
|
except Exception as e:
|
|
|
|
self.log(f"Error fetching attachment with SHA-512 hash {hash_sha512}: {e}", isWarning=True)
|
|
|
|
return None
|
|
|
|
finally:
|
|
|
|
session.remove()
|