async def index(self, req): alias_id = req.match_info["chat"] chat = self.chat_ids[alias_id] log_msg = "" try: offset_val = int(req.query.get("page", "1")) except Exception: offset_val = 1 log_msg += f"page: {offset_val} | " try: search_query = req.query.get("search", "") except Exception: search_query = "" log_msg += f"search query: {search_query} | " offset_val = 0 if offset_val <= 1 else offset_val - 1 try: kwargs = { "entity": chat["chat_id"], "limit": results_per_page, "add_offset": results_per_page * offset_val, } if search_query: kwargs.update({"search": search_query}) messages = (await self.client.get_messages(**kwargs)) or [] except Exception: log.debug("failed to get messages", exc_info=True) messages = [] log_msg += f"found {len(messages)} results | " log.debug(log_msg) results = [] for m in messages: entry = None if m.file and not isinstance(m.media, types.MessageMediaWebPage): filename = get_file_name(m) insight = m.text[:60] if m.text else filename entry = dict( file_id=m.id, media=True, thumbnail=f"/{alias_id}/{m.id}/thumbnail", mime_type=m.file.mime_type, filename=filename, insight=insight, human_size=get_human_size(m.file.size), url=f"/{alias_id}/{m.id}/view", download=f"{alias_id}/{m.id}/{filename}", ) elif m.message: entry = dict( file_id=m.id, media=False, mime_type="text/plain", insight=m.raw_text[:100], url=f"/{alias_id}/{m.id}/view", ) if entry: results.append(entry) prev_page = None next_page = None if offset_val: query = {"page": offset_val} if search_query: query.update({"search": search_query}) prev_page = { "url": str(req.rel_url.with_query(query)), "no": offset_val } if len(messages) == results_per_page: query = {"page": offset_val + 2} if search_query: query.update({"search": search_query}) next_page = { "url": str(req.rel_url.with_query(query)), "no": offset_val + 2, } return { "item_list": results, "prev_page": prev_page, "cur_page": offset_val + 1, "next_page": next_page, "search": search_query, "name": chat["title"], "logo": f"/{alias_id}/logo", "title": "Index of " + chat["title"], "authenticated": req.app["is_authenticated"], "block_downloads": block_downloads, "m3u_option": "" if not req.app["is_authenticated"] else f"{req.app['username']}:{req.app['password']}@", }
async def info(self, req): file_id = int(req.match_info["id"]) alias_id = req.match_info["chat"] chat = self.chat_ids[alias_id] chat_id = chat["chat_id"] try: message = await self.client.get_messages(entity=chat_id, ids=file_id) except Exception: log.debug(f"Error in getting message {file_id} in {chat_id}", exc_info=True) message = None if not message or not isinstance(message, Message): log.debug(f"no valid entry for {file_id} in {chat_id}") return { "found": False, "reason": "Resource you are looking for cannot be retrived!", "authenticated": req.app["is_authenticated"], } return_val = { "authenticated": req.app["is_authenticated"], } reply_btns = [] if message.reply_markup: if isinstance(message.reply_markup, types.ReplyInlineMarkup): reply_btns = [ [ {"url": button.url, "text": button.text} for button in button_row.buttons if isinstance(button, types.KeyboardButtonUrl) ] for button_row in message.reply_markup.rows ] if message.file and not isinstance(message.media, types.MessageMediaWebPage): file_name = get_file_name(message) human_file_size = get_human_size(message.file.size) media = {"type": message.file.mime_type} if "video/" in message.file.mime_type: media["video"] = True elif "audio/" in message.file.mime_type: media["audio"] = True elif "image/" in message.file.mime_type: media["image"] = True if message.text: caption = message.raw_text else: caption = "" caption_html = Markup.escape(caption).__str__().replace("\n", "<br>") return_val.update( { "found": True, "name": file_name, "file_id": file_id, "human_size": human_file_size, "media": media, "caption_html": caption_html, "title": f"Download | {file_name} | {human_file_size}", "reply_btns": reply_btns, "thumbnail": f"/{alias_id}/{file_id}/thumbnail", "download_url": "#" if block_downloads else f"/{alias_id}/{file_id}/{file_name}", "page_id": alias_id, "block_downloads": block_downloads, } ) elif message.message: text = message.raw_text text_html = Markup.escape(text).__str__().replace("\n", "<br>") return_val.update( { "found": True, "media": False, "text_html": text_html, "reply_btns": reply_btns, "page_id": alias_id, } ) else: return_val.update( { "found": False, "reason": "Some kind of resource that I cannot display", } ) log.debug(f"data for {file_id} in {chat_id} returned as {return_val}") return return_val
async def info(self, req): file_id = int(req.match_info["id"]) alias_id = req.match_info['chat'] chat = [i for i in self.chat_ids if i['alias_id'] == alias_id][0] chat_id = chat['chat_id'] try: message = await self.client.get_messages(entity=chat_id, ids=file_id) except: log.debug(f"Error in getting message {file_id} in {chat_id}", exc_info=True) message = None if not message or not isinstance(message, Message): log.debug(f"no valid entry for {file_id} in {chat_id}") return { 'found': False, 'reason': "Resource you are looking for cannot be retrived!", } return_val = {} reply_btns = [] if message.reply_markup: if isinstance(message.reply_markup, types.ReplyInlineMarkup): for button_row in message.reply_markup.rows: btns = [] for button in button_row.buttons: if isinstance(button, types.KeyboardButtonUrl): btns.append({ 'url': button.url, 'text': button.text }) reply_btns.append(btns) if message.file and not isinstance(message.media, types.MessageMediaWebPage): file_name = get_file_name(message) human_file_size = get_human_size(message.file.size) media = {'type': message.file.mime_type} if 'video/' in message.file.mime_type: media['video'] = True elif 'audio/' in message.file.mime_type: media['audio'] = True elif 'image/' in message.file.mime_type: media['image'] = True if message.text: caption = message.raw_text else: caption = '' caption_html = Markup.escape(caption).__str__().replace( '\n', '<br>') return_val = { 'found': True, 'name': file_name, 'file_id': file_id, 'human_size': human_file_size, 'media': media, 'caption_html': caption_html, 'title': f"Download | {file_name} | {human_file_size}", 'reply_btns': reply_btns, 'thumbnail': f"/{alias_id}/{file_id}/thumbnail", 'download_url': '#' if block_downloads else f"/{alias_id}/{file_id}/download", 'page_id': alias_id, 'block_downloads': block_downloads } elif message.message: text = message.raw_text text_html = Markup.escape(text).__str__().replace('\n', '<br>') return_val = { 'found': True, 'media': False, 'text_html': text_html, 'reply_btns': reply_btns, 'page_id': alias_id } else: return_val = { 'found': False, 'reason': "Some kind of resource that I cannot display", } log.debug(f"data for {file_id} in {chat_id} returned as {return_val}") return return_val
async def index(self, req): alias_id = req.match_info['chat'] chat = [i for i in self.chat_ids if i['alias_id'] == alias_id][0] log_msg = '' try: offset_val = int(req.query.get('page', '1')) except: offset_val = 1 log_msg += f"page: {offset_val} | " try: search_query = req.query.get('search', '') except: search_query = '' log_msg += f"search query: {search_query} | " offset_val = 0 if offset_val <= 1 else offset_val - 1 try: kwargs = { 'entity': chat['chat_id'], 'limit': results_per_page, 'add_offset': results_per_page * offset_val } if search_query: kwargs.update({'search': search_query}) messages = (await self.client.get_messages(**kwargs)) or [] except: log.debug("failed to get messages", exc_info=True) messages = [] log_msg += f"found {len(messages)} results | " log.debug(log_msg) results = [] for m in messages: entry = None if m.file and not isinstance(m.media, types.MessageMediaWebPage): entry = dict(file_id=m.id, media=True, thumbnail=f"/{alias_id}/{m.id}/thumbnail", mime_type=m.file.mime_type, insight=get_file_name(m), human_size=get_human_size(m.file.size), url=f"/{alias_id}/{m.id}/view") elif m.message: entry = dict(file_id=m.id, media=False, mime_type='text/plain', insight=m.raw_text[:100], url=f"/{alias_id}/{m.id}/view") if entry: results.append(entry) prev_page = False next_page = False if offset_val: query = {'page': offset_val} if search_query: query.update({'search': search_query}) prev_page = { 'url': str(req.rel_url.with_query(query)), 'no': offset_val } if len(messages) == results_per_page: query = {'page': offset_val + 2} if search_query: query.update({'search': search_query}) next_page = { 'url': str(req.rel_url.with_query(query)), 'no': offset_val + 2 } return { 'item_list': results, 'prev_page': prev_page, 'cur_page': offset_val + 1, 'next_page': next_page, 'search': search_query, 'name': chat['title'], 'logo': f"/{alias_id}/logo", 'title': "Index of " + chat['title'] }