From 8e7d4241e5e200712bbb940d6c7f9a51129aac04 Mon Sep 17 00:00:00 2001 From: Paul Nameless Date: Tue, 22 Sep 2020 10:13:13 +0300 Subject: [PATCH] Add copy to download dir keybinding --- readme.md | 2 ++ tg/config.py | 1 + tg/controllers.py | 14 ++++++++++++++ tg/models.py | 16 ++++++++++++++++ 4 files changed, 33 insertions(+) diff --git a/readme.md b/readme.md index b0a4dc2..fe79020 100644 --- a/readme.md +++ b/readme.md @@ -190,6 +190,8 @@ FILE_PICKER_CMD = "ranger --choosefile={file_path}" # FILE_PICKER_CMD = "nnn -p {file_path}" MAILCAP_FILE = os.path.expanduser("~/.config/mailcap") + +DOWNLOAD_DIR = os.path.expanduser("~/Downloads/") # copy file to this dir ``` ### Mailcap file diff --git a/tg/config.py b/tg/config.py index aa9a794..fd15d39 100644 --- a/tg/config.py +++ b/tg/config.py @@ -75,6 +75,7 @@ KEEP_MEDIA = 7 FILE_PICKER_CMD = "ranger --choosefile={file_path}" +DOWNLOAD_DIR = os.path.expanduser("~/Downloads/") if os.path.isfile(CONFIG_FILE): config_params = runpy.run_path(CONFIG_FILE) diff --git a/tg/controllers.py b/tg/controllers.py index f32440a..dbf5883 100644 --- a/tg/controllers.py +++ b/tg/controllers.py @@ -100,6 +100,20 @@ class Controller: "\n".join(f"{k}: {v}" for k, v in info.items() if v), ) + @bind(msg_handler, ["O"]) + def save_file_in_folder(self) -> None: + chat_id = self.model.chats.id_by_index(self.model.current_chat) + if not chat_id: + return + msg_ids = self.model.selected[chat_id] + if not msg_ids: + msg = self.model.current_msg + msg_ids = [msg["id"]] + else: + self.discard_selected_msgs() + if self.model.copy_files(chat_id, msg_ids, config.DOWNLOAD_DIR): + self.present_info(f"Copied files to {config.DOWNLOAD_DIR}") + @bind(msg_handler, ["o"]) def open_url(self) -> None: msg = MsgProxy(self.model.current_msg) diff --git a/tg/models.py b/tg/models.py index de853ee..882bcd4 100644 --- a/tg/models.py +++ b/tg/models.py @@ -1,5 +1,6 @@ import base64 import logging +import shutil import sys import time from collections import defaultdict, namedtuple @@ -236,6 +237,21 @@ class Model: copy_to_clipboard("\n".join(buffer)) return True + def copy_files( + self, chat_id: int, msg_ids: List[int], dest_dir: str + ) -> bool: + is_copied = False + for msg_id in msg_ids: + _msg = self.msgs.get_message(chat_id, msg_id) + if not _msg: + return False + msg = MsgProxy(_msg) + if msg.file_id and msg.local_path: + file_path = msg.local_path + shutil.copy2(file_path, dest_dir) + is_copied = True + return is_copied + def get_private_chat_info(self, chat: Dict[str, Any]) -> Dict[str, Any]: user_id = chat["id"] user = self.users.get_user(user_id)