From e0e7d631b84422602a92ff26b40d5097567255a0 Mon Sep 17 00:00:00 2001 From: Xtao_dada Date: Sat, 18 Sep 2021 14:23:46 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20PhotoInvalidDimensionsErro?= =?UTF-8?q?r=20(#132)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐛 修复图片尺寸无效的问题 --- languages/built-in/en.yml | 3 ++- languages/built-in/zh-cn.yml | 1 + languages/built-in/zh-tw.yml | 1 + pagermaid/modules/captions.py | 30 ++++++++++++++++++++---------- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/languages/built-in/en.yml b/languages/built-in/en.yml index 9cb82cf..f418010 100644 --- a/languages/built-in/en.yml +++ b/languages/built-in/en.yml @@ -131,7 +131,8 @@ parseqr_log: Parsed QR Code with content convert_des: Reply to an attachment message and convert it to image output convert_process: Converting... convert_no_file: Something went wrong. There seems to be no attachment in the reply message. -convert_error: Something went wrong, woooo~ An error occurred during the conversion. +convert_error: Something went wrong, woooo ~ An error occurred during the conversion. +convert_invalid: Something went wrong, woooo ~ The photo dimensions are invalid. # caption caption_des: Add two lines of captions to the reply picture. The captions will be added to the top and bottom respectively. The captions need to be separated by commas. caption_process: The image is being rendered... diff --git a/languages/built-in/zh-cn.yml b/languages/built-in/zh-cn.yml index e90cf33..353ac5a 100644 --- a/languages/built-in/zh-cn.yml +++ b/languages/built-in/zh-cn.yml @@ -139,6 +139,7 @@ convert_des: 回复某条附件消息然后转换为图片输出 convert_process: 正在转换中 . . . convert_no_file: 出错了呜呜呜 ~ 回复的消息中好像没有附件。 convert_error: 出错了呜呜呜 ~ 转换期间发生了错误。 +convert_invalid: 出错了呜呜呜 ~ 图片尺寸无效。 ## caption caption_des: 将两行字幕添加到回复的图片中,字幕将分别添加到顶部和底部,字幕需要以逗号分隔。 caption_process: 正在渲染图像中 . . . diff --git a/languages/built-in/zh-tw.yml b/languages/built-in/zh-tw.yml index d24b3d0..515eb70 100644 --- a/languages/built-in/zh-tw.yml +++ b/languages/built-in/zh-tw.yml @@ -127,6 +127,7 @@ convert_des: 回覆附件訊息並轉換為圖片 convert_process: 正在轉換…… convert_no_file: Error!回覆的消息沒有附件。 convert_error: Error!轉換期間發生錯誤! +convert_invalid: Error!圖片尺寸無效! # caption caption_des: 將兩行字幕添加到回覆的照片中,分別位於底部和頂部,請以逗號分割兩句。 caption_process: 正在渲染照片… diff --git a/pagermaid/modules/captions.py b/pagermaid/modules/captions.py index b2b8a3c..06c0260 100644 --- a/pagermaid/modules/captions.py +++ b/pagermaid/modules/captions.py @@ -5,6 +5,8 @@ from magic import Magic from pygments import highlight as syntax_highlight from pygments.formatters import img from pygments.lexers import guess_lexer +from telethon.errors import PhotoInvalidDimensionsError + from pagermaid import log, module_dir from pagermaid.listener import listener from pagermaid.utils import execute, upload_attachment, lang, alias_command @@ -33,11 +35,15 @@ async def convert(context): await context.edit(lang('convert_error')) return if not result: - await handle_failure(context, target_file_path) + await handle_failure(context, target_file_path, 'convert_error') return - if not await upload_attachment("result.png", context.chat_id, reply_id): - await context.edit(lang('convert_error')) - remove(target_file_path) + try: + if not await upload_attachment("result.png", context.chat_id, reply_id): + await context.edit(lang('convert_error')) + remove(target_file_path) + return + except PhotoInvalidDimensionsError: + await handle_failure(context, target_file_path, 'convert_invalid') return await context.delete() remove(target_file_path) @@ -80,11 +86,15 @@ async def caption(context): f"\"{str(string_1)}\" \"{str(string_2)}\"") result_file = "result.gif" if not result: - await handle_failure(context, target_file_path) + await handle_failure(context, target_file_path, 'convert_error') return - if not await upload_attachment(result_file, context.chat_id, reply_id): - await context.edit(lang('caption_error')) - remove(target_file_path) + try: + if not await upload_attachment(result_file, context.chat_id, reply_id): + await context.edit(lang('caption_error')) + remove(target_file_path) + return + except PhotoInvalidDimensionsError: + await handle_failure(context, target_file_path, 'convert_invalid') return await context.delete() if string_2 != " ": @@ -188,8 +198,8 @@ async def highlight(context): await context.delete() -async def handle_failure(context, target_file_path): - await context.edit(lang('handle_failure_error')) +async def handle_failure(context, target_file_path, name): + await context.edit(lang(name)) try: remove("result.png") remove(target_file_path)