From d5da6e47e14831b865d418faa32f32df4de5af42 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 19 Nov 2020 17:03:49 +0100 Subject: Matrix: fix too long path in event cache, bug when not using error handler.. --- src/DownloadUtils.cpp | 17 ++++++++++++----- src/Storage.cpp | 4 +++- src/plugins/Matrix.cpp | 11 ++++++----- 3 files changed, 21 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/DownloadUtils.cpp b/src/DownloadUtils.cpp index 2a3f9d3..dae013d 100644 --- a/src/DownloadUtils.cpp +++ b/src/DownloadUtils.cpp @@ -52,9 +52,16 @@ namespace QuickMedia { return DownloadResult::OK; } - DownloadResult download_to_string_cache(const std::string &url, std::string &result, const std::vector &additional_args, bool use_tor, bool use_browser_useragent, DownloadErrorHandler error_handler) { - Path media_dir = get_cache_dir().join("media"); - Path media_file_path = Path(media_dir).join(base64_url::encode(url)); + DownloadResult download_to_string_cache(const std::string &url, std::string &result, const std::vector &additional_args, bool use_tor, bool use_browser_useragent, DownloadErrorHandler error_handler, Path cache_path) { + Path media_dir; + Path media_file_path; + if(cache_path.data.empty()) { + media_dir = get_cache_dir().join("media"); + media_file_path = Path(media_dir).join(base64_url::encode(url)); + } else { + media_dir = cache_path.parent(); + media_file_path = std::move(cache_path); + } if(get_file_type(media_file_path) == FileType::REGULAR) { if(file_get_content(media_file_path, result) == 0) { fprintf(stderr, "Loaded %s from cache\n", url.c_str()); @@ -65,8 +72,8 @@ namespace QuickMedia { } } else { DownloadResult download_result = download_to_string(url, result, additional_args, use_tor, use_browser_useragent, error_handler ? false : true); - if(!error_handler(result)) - download_result = DownloadResult::ERR; + if(error_handler) + download_result = error_handler(result) ? DownloadResult::OK : DownloadResult::ERR; if(download_result == DownloadResult::OK) { Path media_file_path_tmp(media_file_path.data + ".tmp"); if(create_directory_recursive(media_dir) == 0 && file_overwrite(media_file_path_tmp, result) == 0) { diff --git a/src/Storage.cpp b/src/Storage.cpp index dadff5b..c2f62bd 100644 --- a/src/Storage.cpp +++ b/src/Storage.cpp @@ -142,8 +142,10 @@ namespace QuickMedia { static int file_overwrite(const Path &path, const char *str, size_t size) { FILE *file = fopen(path.data.c_str(), "wb"); - if(!file) + if(!file) { + perror(path.data.c_str()); return -1; + } if(fwrite(str, 1, size, file) != size) { fclose(file); diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index f17da30..b0c83cc 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -4,6 +4,7 @@ #include "../../include/NetUtils.hpp" #include "../../include/Notification.hpp" #include "../../include/Program.hpp" +#include "../../include/base64_url.hpp" #include #include #include @@ -2767,33 +2768,33 @@ namespace QuickMedia { } return true; - }); + }, get_cache_dir().join("matrix").join("events").join(base64_url::encode(event_id))); if(download_result != DownloadResult::OK) return nullptr; rapidjson::Document json_root; rapidjson::ParseResult parse_result = json_root.Parse(response.c_str(), response.size()); if(parse_result.IsError()) { - fprintf(stderr, "Failed to get message by id %s\n", event_id.c_str()); + fprintf(stderr, "Failed to get message by id %s, error: %s\n", event_id.c_str(), response.c_str()); room->fetched_messages_by_event_id.insert(std::make_pair(event_id, nullptr)); return nullptr; } if(!json_root.IsObject()) { - fprintf(stderr, "Failed to get message by id %s\n", event_id.c_str()); + fprintf(stderr, "Failed to get message by id %s, error: %s\n", event_id.c_str(), response.c_str()); room->fetched_messages_by_event_id.insert(std::make_pair(event_id, nullptr)); return nullptr; } const rapidjson::Value &errcode_json = GetMember(json_root, "errcode"); if(errcode_json.IsString() && strcmp(errcode_json.GetString(), "M_FORBIDDEN") == 0) { - fprintf(stderr, "You donm't have permission to access event %s\n", event_id.c_str()); + fprintf(stderr, "You don't have permission to access event %s\n", event_id.c_str()); room->fetched_messages_by_event_id.insert(std::make_pair(event_id, nullptr)); return nullptr; } const rapidjson::Value &error_json = GetMember(json_root, "error"); if(error_json.IsString()) { - fprintf(stderr, "Matrix::get_message_by_id, error: %s\n", error_json.GetString()); + fprintf(stderr, "Matrix::get_message_by_id for event id: %s, error: %s\n", event_id.c_str(), error_json.GetString()); room->fetched_messages_by_event_id.insert(std::make_pair(event_id, nullptr)); return nullptr; } -- cgit v1.2.3