diff --git a/sukuinote/plugins/cat.py b/sukuinote/plugins/cat.py
index 5e52161..13b34a3 100644
--- a/sukuinote/plugins/cat.py
+++ b/sukuinote/plugins/cat.py
@@ -1,3 +1,4 @@
+import os
import html
import tempfile
from pyrogram import Client, filters
@@ -7,26 +8,40 @@ from .. import config, help_dict, log_errors, session, progress_callback, public
@log_errors
@public_log_errors
async def cat(client, message):
- media = message.document
- if not media and not getattr(message.reply_to_message, 'empty', True):
- media = message.reply_to_message.document
- if not media:
- await message.reply_text('Document required')
- return
+ media = (message.text or message.caption).markdown.split(' ', 1)[1:]
+ if media:
+ media = os.path.expanduser(media[0])
+ else:
+ media = message.document
+ if not media and not getattr(message.reply_to_message, 'empty', True):
+ media = message.reply_to_message.document
+ if not media:
+ await message.reply_text('Document or local file path required')
+ return
done = False
- with tempfile.NamedTemporaryFile() as file:
- reply = await message.reply_text('Downloading...')
- await client.download_media(media, file_name=file.name, progress=progress_callback, progress_args=(reply, 'Downloading...', False))
- with open(file.name) as nfile:
+ reply = rfile = None
+ try:
+ if not isinstance(media, str):
+ rfile = tempfile.NamedTemporaryFile()
+ reply = await message.reply_text('Downloading...')
+ await client.download_media(media, file_name=rfile.name, progress=progress_callback, progress_args=(reply, 'Downloading...', False))
+ media = rfile.name
+ with open(media, 'rb') as file:
while True:
- chunk = nfile.read(4096)
+ chunk = file.read(4096)
if not chunk:
break
- chunk = f'{html.escape(chunk)}
'
+ if not chunk.strip():
+ continue
+ chunk = f'{html.escape(chunk.decode())}
'
if done:
await message.reply_text(chunk, quote=False)
else:
- await reply.edit_text(chunk)
+ await getattr(reply, 'edit_text', message.reply_text)(chunk)
done = True
+ finally:
+ if rfile:
+ rfile.close()
-help_dict['cat'] = ('cat', '{prefix}cat (as caption of text file or reply) - Outputs file\'s text to Telegram')
+help_dict['cat'] = ('cat', '''{prefix}cat (as caption of text file or reply) - Outputs file's text to Telegram
+{prefix}cat <path to local file> - Outputs file's text to Telegram''')