Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
upload file directly if it is small
Browse files Browse the repository at this point in the history
  • Loading branch information
adbenitez committed Aug 12, 2021
1 parent adb7e63 commit 351f271
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions simplebot_file2link.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ def deltabot_init(bot: DeltaBot) -> None:
def file2link(bot: DeltaBot, message: Message, replies: Replies) -> None:
"""Send me any file in private and I will upload it and give you a download link that you can share with others."""
if not message.chat.is_group() and message.filename:
rep = Replies(message, bot.logger)
rep.add(text="⬆️ Uploading...", quote=message)
rep.send_reply_messages()
num = os.stat(message.filename).st_size
if num > 1024**2:
rep = Replies(message, bot.logger)
rep.add(text="⬆️ Uploading...", quote=message)
rep.send_reply_messages()
url = _getdefault(bot, "server")
try:
with open(message.filename, "rb") as file:
with session.post(url, files=dict(file=file)) as resp:
resp.raise_for_status()
name = os.path.basename(message.filename)
size = _get_size(message.filename)
size = _sizeof_fmt(num)
replies.add(
text=f"Name: {name}\nSize: {size}\nLink: {resp.text.strip()}"
)
Expand All @@ -57,8 +59,7 @@ def _getdefault(bot: DeltaBot, key: str, value: str = None) -> str:
return val


def _get_size(path: str) -> str:
num: float = os.stat(path).st_size
def _sizeof_fmt(num: float) -> str:
suffix = "B"
for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
if abs(num) < 1024.0:
Expand Down

0 comments on commit 351f271

Please sign in to comment.