# Moon-Userbot - telegram userbot # Copyright (C) 2020-present Moon Userbot Organization # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . import datetime import os import time import aiofiles from pyrogram import Client, filters from pyrogram.errors import MessageTooLong from pyrogram.types import Message from utils.misc import modules_help, prefix from utils.rentry import paste as rentry_paste from utils.scripts import edit_or_reply, format_exc, progress async def read_file(file_path): async with aiofiles.open(file_path) as file: content = await file.read() return content def check_extension(file_path): extensions = { '.txt': "
",
        '.py': "
",
        '.js': "
",
        '.json': "
",
        '.smali': "
",
        '.sh': "
",
        '.c': "
",
        '.java': "
",
        '.php': "
",
        '.doc': "
",
        '.docx': "
",
        '.rtf': "
",
        '.s': "
",
        '.dart': "
",
        '.cfg': "
",
        '.swift': "
",
        '.cs': "
",
        '.vb': "
",
        '.css': "
",
        '.htm': "
",
        '.html': "
",
        '.rss': "
",
        '.xhtml': "
",
        '.cpp': "
",
    }

    ext = os.path.splitext(file_path)[1].lower()

    return extensions.get(ext, '
')


@Client.on_message(filters.command('open', prefix) & filters.me)
async def openfile(client: Client, message: Message):
    if not message.reply_to_message:
        return await message.edit_text('Kindly Reply to a File')

    try:
        ms = await edit_or_reply(message, 'Downloading...')
        ct = time.time()
        file_path = await message.reply_to_message.download(progress=progress, progress_args=(ms, ct, 'Downloading...'))
        await ms.edit_text('Trying to open file...')
        file_info = os.stat(file_path)
        file_name = file_path.split('/')[-1:]
        file_size = file_info.st_size
        last_modified = datetime.datetime.fromtimestamp(file_info.st_mtime).strftime('%Y-%m-%d %H:%M:%S')
        code_start = check_extension(file_path=file_path)
        content = await read_file(file_path=file_path)
        await ms.edit_text(
            f'File Name: {file_name[0]}\nSize: {file_size} bytes\nLast Modified: {last_modified}\nContent: {code_start}{content}
', ) except MessageTooLong: await ms.edit_text('File Content is too long... Pasting to rentry...') content_new = f'```{code_start[11:-2]}\n{content}```' try: rentry_url, edit_code = await rentry_paste(text=content_new, return_edit=True) except RuntimeError: await ms.edit_text('Error: Failed to paste to rentry') return await client.send_message( 'me', f"Here's your edit code for Url: {rentry_url}\nEdit code: {edit_code}", disable_web_page_preview=True, ) await ms.edit_text( f'File Name: {file_name[0]}\nSize: {file_size} bytes\nLast Modified: {last_modified}\nContent: {rentry_url}\nNote: Edit Code has been sent to your saved messages', disable_web_page_preview=True, ) except Exception as e: await ms.edit_text(format_exc(e)) finally: if os.path.exists(file_path): os.remove(file_path) modules_help['open'] = {'open': "Open content of any text supported filetype and show it's raw data"}