diff --git a/bot/fediverse_service.py b/bot/fediverse_service.py index 2d12d89..d4e32fe 100644 --- a/bot/fediverse_service.py +++ b/bot/fediverse_service.py @@ -1,5 +1,5 @@ from abc import ABC, abstractmethod -from typing import List, Optional +from typing import List, Optional, Union, BinaryIO from fediverse_types import FediverseNotification, FediversePost, Visibility @@ -54,4 +54,21 @@ class FediverseService(ABC): Returns: FediversePost object if found, None otherwise """ + pass + + @abstractmethod + def upload_file(self, file_data: Union[BinaryIO, bytes], filename: Optional[str] = None) -> str: + """ + Upload a file to the Fediverse instance. + + Args: + file_data: File data as binary stream or bytes + filename: Optional filename for the uploaded file + + Returns: + File ID that can be used in posts + + Raises: + RuntimeError: If file upload fails + """ pass \ No newline at end of file diff --git a/bot/misskey_service.py b/bot/misskey_service.py index 845ed27..e8bd396 100644 --- a/bot/misskey_service.py +++ b/bot/misskey_service.py @@ -1,5 +1,5 @@ import misskey -from typing import List, Optional, Dict, Any +from typing import List, Optional, Dict, Any, Union, BinaryIO from fediverse_service import FediverseService from fediverse_types import ( FediverseNotification, FediversePost, FediverseUser, FediverseFile, @@ -138,4 +138,16 @@ class MisskeyService(FediverseService): note = self.client.notes_show(noteId=post_id) return self._convert_misskey_post(note) except Exception: - return None \ No newline at end of file + return None + + def upload_file(self, file_data: Union[BinaryIO, bytes], filename: Optional[str] = None) -> str: + """Upload a file to Misskey Drive""" + try: + from misskey.exceptions import MisskeyAPIException + + media = self.client.drive_files_create(file_data) + return media["id"] + except MisskeyAPIException as e: + raise RuntimeError(f"Failed to upload file to Misskey Drive: {e}") from e + except Exception as e: + raise RuntimeError(f"Unexpected error during file upload: {e}") from e \ No newline at end of file diff --git a/bot/pleroma_service.py b/bot/pleroma_service.py index 1e53cd7..7333df0 100644 --- a/bot/pleroma_service.py +++ b/bot/pleroma_service.py @@ -1,5 +1,5 @@ from mastodon import Mastodon -from typing import List, Optional, Dict, Any +from typing import List, Optional, Dict, Any, Union, BinaryIO from fediverse_service import FediverseService from fediverse_types import ( FediverseNotification, FediversePost, FediverseUser, FediverseFile, @@ -152,4 +152,12 @@ class PleromaService(FediverseService): status = self.client.status(post_id) return self._convert_mastodon_status(status) except Exception: - return None \ No newline at end of file + return None + + def upload_file(self, file_data: Union[BinaryIO, bytes], filename: Optional[str] = None) -> str: + """Upload a file to Pleroma instance""" + try: + media = self.client.media_post(file_data, mime_type=None, description=filename) + return str(media["id"]) + except Exception as e: + raise RuntimeError(f"Failed to upload file to Pleroma: {e}") from e \ No newline at end of file