first whack at file upload

This commit is contained in:
Moon 2025-06-12 11:08:00 +09:00
parent e0cf42f8f6
commit a47530180d
3 changed files with 42 additions and 5 deletions

View file

@ -1,5 +1,5 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import List, Optional from typing import List, Optional, Union, BinaryIO
from fediverse_types import FediverseNotification, FediversePost, Visibility from fediverse_types import FediverseNotification, FediversePost, Visibility
@ -54,4 +54,21 @@ class FediverseService(ABC):
Returns: Returns:
FediversePost object if found, None otherwise 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 pass

View file

@ -1,5 +1,5 @@
import misskey 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_service import FediverseService
from fediverse_types import ( from fediverse_types import (
FediverseNotification, FediversePost, FediverseUser, FediverseFile, FediverseNotification, FediversePost, FediverseUser, FediverseFile,
@ -138,4 +138,16 @@ class MisskeyService(FediverseService):
note = self.client.notes_show(noteId=post_id) note = self.client.notes_show(noteId=post_id)
return self._convert_misskey_post(note) return self._convert_misskey_post(note)
except Exception: except Exception:
return None 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

View file

@ -1,5 +1,5 @@
from mastodon import Mastodon 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_service import FediverseService
from fediverse_types import ( from fediverse_types import (
FediverseNotification, FediversePost, FediverseUser, FediverseFile, FediverseNotification, FediversePost, FediverseUser, FediverseFile,
@ -152,4 +152,12 @@ class PleromaService(FediverseService):
status = self.client.status(post_id) status = self.client.status(post_id)
return self._convert_mastodon_status(status) return self._convert_mastodon_status(status)
except Exception: except Exception:
return None 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