File size: 4,680 Bytes
dfdbd3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#  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 <https://www.gnu.org/licenses/>.

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.scripts import edit_or_reply, format_exc, progress
from utils.rentry import paste as rentry_paste


async def read_file(file_path):
    async with aiofiles.open(file_path, mode="r") as file:
        content = await file.read()
    return content


def check_extension(file_path):
    extensions = {
        ".txt": "<pre lang='plaintext'>",
        ".py": "<pre lang='python'>",
        ".js": "<pre lang='javascript'>",
        ".json": "<pre lang='json'>",
        ".smali": "<pre lang='smali'>",
        ".sh": "<pre lang='shell'>",
        ".c": "<pre lang='c'>",
        ".java": "<pre lang='java'>",
        ".php": "<pre lang='php'>",
        ".doc": "<pre lang='doc'>",
        ".docx": "<pre lang='docx'>",
        ".rtf": "<pre lang='rtf'>",
        ".s": "<pre lang='asm'>",
        ".dart": "<pre lang='dart'>",
        ".cfg": "<pre lang='cfg'>",
        ".swift": "<pre lang='swift'>",
        ".cs": "<pre lang='csharp'>",
        ".vb": "<pre lang='vb'>",
        ".css": "<pre lang='css'>",
        ".htm": "<pre lang='html'>",
        ".html": "<pre lang='html'>",
        ".rss": "<pre lang='xml'>",
        ".xhtml": "<pre lang='xtml'>",
        ".cpp": "<pre lang='cpp'>",
    }

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

    return extensions.get(ext, "<pre>")


@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, "<b>Downloading...</b>")
        ct = time.time()
        file_path = await message.reply_to_message.download(
            progress=progress, progress_args=(ms, ct, "Downloading...")
        )
        await ms.edit_text("<code>Trying to open file...</code>")
        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"<b>File Name:</b> <code>{file_name[0]}</code>\n<b>Size:</b> <code>{file_size} bytes</code>\n<b>Last Modified:</b> <code>{last_modified}</code>\n<b>Content:</b> {code_start}{content}</pre>",
        )

    except MessageTooLong:
        await ms.edit_text(
            "<code>File Content is too long... Pasting to rentry...</code>"
        )
        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("<b>Error:</b> <code>Failed to paste to rentry</code>")
            return
        await client.send_message(
            "me",
            f"Here's your edit code for Url: {rentry_url}\nEdit code:  <code>{edit_code}</code>",
            disable_web_page_preview=True,
        )
        await ms.edit_text(
            f"<b>File Name:</b> <code>{file_name[0]}</code>\n<b>Size:</b> <code>{file_size} bytes</code>\n<b>Last Modified:</b> <code>{last_modified}</code>\n<b>Content:</b> {rentry_url}\n<b>Note:</b> <code>Edit Code has been sent to your saved messages</code>",
            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"
}