aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-05-05 19:12:07 +0200
committerdec05eba <dec05eba@protonmail.com>2020-05-05 19:12:07 +0200
commit737263bc5a123bee889530a2623dfbeb90ee30e9 (patch)
treef1ec22cc26c7dfe86353e21e0443e0a7a74a248e
parenta255719fa63aa52f56c4c9c7f04fb43b89b6b64c (diff)
Fallback to backup image source if image fails to load for manganelo
-rw-r--r--include/StringUtils.hpp3
-rw-r--r--src/QuickMedia.cpp22
-rw-r--r--src/StringUtils.cpp7
3 files changed, 26 insertions, 6 deletions
diff --git a/include/StringUtils.hpp b/include/StringUtils.hpp
index dd4b99b..b2efb1d 100644
--- a/include/StringUtils.hpp
+++ b/include/StringUtils.hpp
@@ -8,7 +8,8 @@ namespace QuickMedia {
using StringSplitCallback = std::function<bool(const char *str, size_t size)>;
void string_split(const std::string &str, char delimiter, StringSplitCallback callback_func);
- void string_replace_all(std::string &str, const std::string &old_str, const std::string &new_str);
+ // Returns the number of replaced substrings
+ size_t string_replace_all(std::string &str, const std::string &old_str, const std::string &new_str);
std::string strip(const std::string &str);
bool string_ends_with(const std::string &str, const std::string &ends_with_str);
} \ No newline at end of file
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) {