Add copy to download dir keybinding

This commit is contained in:
Paul Nameless 2020-09-22 10:13:13 +03:00
parent 4a2569b12b
commit 8e7d4241e5
4 changed files with 33 additions and 0 deletions

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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)