From 737263bc5a123bee889530a2623dfbeb90ee30e9 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 5 May 2020 19:12:07 +0200 Subject: Fallback to backup image source if image fails to load for manganelo --- src/QuickMedia.cpp | 22 +++++++++++++++++++--- src/StringUtils.cpp | 7 +++++-- 2 files changed, 24 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 8e6f04e..d7c15b1 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -1052,9 +1052,25 @@ namespace QuickMedia { return true; std::string image_content; - if(download_to_string(url, image_content, {}, current_plugin->use_tor) != DownloadResult::OK) { - show_notification("Manganelo", "Failed to download image: " + url, Urgency::CRITICAL); - return false; + if(download_to_string(url, image_content, {}, current_plugin->use_tor) != DownloadResult::OK || image_content.size() <= 255) { + bool try_backup_url = false; + std::string new_url = url; + if(string_replace_all(new_url, "s3.mkklcdnv3.com", "bu.mkklcdnbuv1.com") > 0) { + try_backup_url = true; + } else { + try_backup_url = (string_replace_all(new_url, "s41.mkklcdnv41.com", "bu.mkklcdnbuv1.com") > 0); + } + + if(try_backup_url) { + image_content.clear(); + if(download_to_string(new_url, image_content, {}, current_plugin->use_tor) != DownloadResult::OK || image_content.size() <= 255) { + show_notification("Manganelo", "Failed to download image: " + new_url, Urgency::CRITICAL); + return false; + } + } else { + show_notification("Manganelo", "Failed to download image: " + url, Urgency::CRITICAL); + return false; + } } if(file_overwrite(image_filepath, image_content) != 0) { diff --git a/src/StringUtils.cpp b/src/StringUtils.cpp index 981b227..7668df7 100644 --- a/src/StringUtils.cpp +++ b/src/StringUtils.cpp @@ -16,14 +16,17 @@ namespace QuickMedia { } } - void string_replace_all(std::string &str, const std::string &old_str, const std::string &new_str) { + size_t string_replace_all(std::string &str, const std::string &old_str, const std::string &new_str) { + size_t num_replaced_substrings = 0; size_t index = 0; while(true) { index = str.find(old_str, index); if(index == std::string::npos) - return; + break; str.replace(index, old_str.size(), new_str); + ++num_replaced_substrings; } + return num_replaced_substrings; } static bool is_whitespace(char c) { -- cgit v1.2.3