From acbc574b4b7267f02795e5f87969df48f219d1f0 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Fri, 11 Aug 2023 13:24:10 +0200 Subject: [PATCH] [images] Store in map url -> filename for images and others. Refs #257 --- src/session/download.cpp | 49 ++++++++++++++++++++++++++++++++++++++- src/terminal/terminal.cpp | 2 ++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/session/download.cpp b/src/session/download.cpp index 210df959e..ac372c488 100644 --- a/src/session/download.cpp +++ b/src/session/download.cpp @@ -59,12 +59,28 @@ #include "util/string.h" #include "util/time.h" +#include +#include /* TODO: tp_*() should be in separate file, I guess? --pasky */ INIT_LIST_OF(struct file_download, downloads); +std::map uri_tempfiles; + +static char * +check_url_tempfiles(const char *url) +{ + /* Caller must free value */ + auto value = uri_tempfiles.find(url); + + if (value == uri_tempfiles.end()) { + return NULL; + } + return null_or_stracpy((value->second).c_str()); +} + int download_is_progressing(struct download *download) { @@ -537,6 +553,12 @@ download_data_store(struct download *download, struct file_download *file_downlo /* Temporary file is deleted by the dgi_protocol_handler */ file_download->delete_ = 0; } else { + char *url = get_uri_string(file_download->uri, URI_PUBLIC); + + if (url) { + uri_tempfiles[url] = file_download->file; + mem_free(url); + } exec_on_terminal(term, file_download->external_handler, file_download->file, file_download->block ? TERM_EXEC_FG : @@ -1624,8 +1646,33 @@ tp_open(struct type_query *type_query) done_type_query(type_query); return; - } + } else { // Check in cache + char *url = get_uri_string(type_query->uri, URI_PUBLIC); + if (url) { + char *filename = check_url_tempfiles(url); + + if (filename) { + char *handler = subst_file(type_query->external_handler, filename, filename); + + if (handler) { + if (type_query->copiousoutput) { + exec_later(type_query->ses, handler, NULL); + } else { + exec_on_terminal(type_query->ses->tab->term, + handler, "", type_query->block ? + TERM_EXEC_FG : TERM_EXEC_BG); + } + mem_free(handler); + } + mem_free(filename); + done_type_query(type_query); + mem_free(url); + return; + } + mem_free(url); + } + } continue_download(type_query, (char *)("")); } diff --git a/src/terminal/terminal.cpp b/src/terminal/terminal.cpp index cf77aa34b..5cf8d4ae1 100644 --- a/src/terminal/terminal.cpp +++ b/src/terminal/terminal.cpp @@ -53,6 +53,7 @@ INIT_LIST_OF(struct terminal, terminals); std::map temporary_files; +extern std::map uri_tempfiles; static void check_if_no_terminal(void); @@ -70,6 +71,7 @@ clean_temporary_files(void) } } temporary_files.clear(); + uri_tempfiles.clear(); } void