From 7c2fde0c81b3f543778cafa8f1cf59a3c1d03e56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Mon, 4 Nov 2024 23:43:29 -0500 Subject: [PATCH] proxy list: fallback to cache if download fails Change-Id: I8139cf0f2a344abb55e954a55ed6e360ed4b7593 --- src/jamidht/jamiaccount.cpp | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp index 7e7f82734..239f14584 100644 --- a/src/jamidht/jamiaccount.cpp +++ b/src/jamidht/jamiaccount.cpp @@ -2572,13 +2572,13 @@ JamiAccount::loadCachedUrl(const std::string& url, { dht::ThreadPool::io().run([cb, url, cachePath, cacheDuration, w = weak()]() { try { - std::vector<uint8_t> data; + std::string data; { std::lock_guard lk(dhtnet::fileutils::getFileLock(cachePath)); - data = fileutils::loadCacheFile(cachePath, cacheDuration); + data = fileutils::loadCacheTextFile(cachePath, cacheDuration); } dht::http::Response ret; - ret.body = {data.begin(), data.end()}; + ret.body = std::move(data); ret.status_code = 200; cb(ret); } catch (const std::exception& e) { @@ -2602,10 +2602,26 @@ JamiAccount::loadCachedUrl(const std::string& url, cachePath, ex.what()); } + cb(response); } else { - JAMI_WARN("Failed to download url"); + try { + if (std::filesystem::exists(cachePath)) { + JAMI_WARNING("Failed to download url, using cached data"); + std::string data; + { + std::lock_guard lk(dhtnet::fileutils::getFileLock(cachePath)); + data = fileutils::loadTextFile(cachePath); + } + dht::http::Response ret; + ret.body = std::move(data); + ret.status_code = 200; + cb(ret); + } else + throw std::runtime_error("No cached data"); + } catch (...) { + cb(response); + } } - cb(response); if (auto req = response.request.lock()) if (auto sthis = w.lock()) sthis->requests_.erase(req); -- GitLab