diff options
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | include/DownloadUtils.hpp | 1 | ||||
-rw-r--r-- | src/Body.cpp | 3 | ||||
-rw-r--r-- | src/DownloadUtils.cpp | 24 |
4 files changed, 28 insertions, 3 deletions
@@ -6,7 +6,8 @@ Currently supported websites: `youtube`, `manganelo`, `mangadex`, `4chan` and _o **Note:** TOR system service needs to be running (`systemctl start tor.service`).\ Here is an example with YouTube:\ ![QuickMedia Youtube Picture](QuickMediaYoutube.png)\ -Config data, including manga progress is stored under `$HOME/.config/quickmedia` +Config data, including manga progress is stored under `$HOME/.config/quickmedia`.\ +Cache is stored under `$HOME/.cache/quickmedia`. ## Usage ``` usage: QuickMedia <plugin> [--tor] diff --git a/include/DownloadUtils.hpp b/include/DownloadUtils.hpp index 0411236..622f5fe 100644 --- a/include/DownloadUtils.hpp +++ b/include/DownloadUtils.hpp @@ -21,5 +21,6 @@ namespace QuickMedia { }; DownloadResult download_to_string(const std::string &url, std::string &result, const std::vector<CommandArg> &additional_args, bool use_tor); + DownloadResult download_to_string_cache(const std::string &url, std::string &result, const std::vector<CommandArg> &additional_args, bool use_tor); std::vector<CommandArg> create_command_args_from_form_data(const std::vector<FormData> &form_data); }
\ No newline at end of file diff --git a/src/Body.cpp b/src/Body.cpp index 8ccb3cc..3bd2421 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -112,8 +112,7 @@ namespace QuickMedia { loading_thumbnail = true; thumbnail_load_thread = std::thread([this, result, url]() { std::string texture_data; - // TODO: Cache images instead of redownloading them everytime they appear on the screen - if(download_to_string(url, texture_data, {}, program->get_current_plugin()->use_tor) == DownloadResult::OK) { + if(download_to_string_cache(url, texture_data, {}, program->get_current_plugin()->use_tor) == DownloadResult::OK) { if(result->loadFromMemory(texture_data.data(), texture_data.size())) { //result->generateMipmap(); } diff --git a/src/DownloadUtils.cpp b/src/DownloadUtils.cpp index a61d0a1..6f31c2c 100644 --- a/src/DownloadUtils.cpp +++ b/src/DownloadUtils.cpp @@ -1,6 +1,8 @@ #include "../include/DownloadUtils.hpp" #include "../include/Program.h" +#include "../include/Storage.hpp" #include <SFML/System/Clock.hpp> +#include <cppcodec/base64_rfc4648.hpp> static int accumulate_string(char *data, int size, void *userdata) { std::string *str = (std::string*)userdata; @@ -29,6 +31,28 @@ namespace QuickMedia { return DownloadResult::OK; } + DownloadResult download_to_string_cache(const std::string &url, std::string &result, const std::vector<CommandArg> &additional_args, bool use_tor) { + Path media_dir = get_cache_dir().join("media"); + Path media_file_path = Path(media_dir).join(cppcodec::base64_rfc4648::encode(url)); + Path media_finished_path(media_file_path.data + ".finished"); + if(get_file_type(media_finished_path) != FileType::FILE_NOT_FOUND && get_file_type(media_file_path) == FileType::REGULAR) { + if(file_get_content(media_file_path, result) == 0) { + return DownloadResult::OK; + } else { + fprintf(stderr, "Failed to get content of cached media file: %s\n", media_file_path.data.c_str()); + return DownloadResult::ERR; + } + } else { + DownloadResult download_result = download_to_string(url, result, additional_args, use_tor); + if(download_result == DownloadResult::OK) { + if(create_directory_recursive(media_dir) == 0 && file_overwrite(media_file_path, result) == 0) { + create_lock_file(media_finished_path); + } + } + return download_result; + } + } + std::vector<CommandArg> create_command_args_from_form_data(const std::vector<FormData> &form_data) { // TODO: This boundary value might need to change, depending on the content. What if the form data contains the boundary value? const std::string boundary = "-----------------------------119561554312148213571335532670"; |