first whack at file upload
This commit is contained in:
parent
e0cf42f8f6
commit
a47530180d
3 changed files with 42 additions and 5 deletions
|
@ -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
|
|
@ -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
|
||||
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
|
|
@ -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
|
||||
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
|
Loading…
Add table
Reference in a new issue