aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-11-19 17:03:49 +0100
committerdec05eba <dec05eba@protonmail.com>2020-11-19 17:03:49 +0100
commitd5da6e47e14831b865d418faa32f32df4de5af42 (patch)
treecf14a47cba90642c50dbba6af0922c26c9cb266c
parent9134914f6065c0b248dd1b4317dbf9b16f5f52b5 (diff)
Matrix: fix too long path in event cache, bug when not using error handler..
-rw-r--r--DEV_NOTES.txt1
-rw-r--r--include/DownloadUtils.hpp3
-rw-r--r--include/Path.hpp7
-rw-r--r--src/DownloadUtils.cpp17
-rw-r--r--src/Storage.cpp4
-rw-r--r--src/plugins/Matrix.cpp11
6 files changed, 31 insertions, 12 deletions
diff --git a/DEV_NOTES.txt b/DEV_NOTES.txt
new file mode 100644
index 0000000..d9d552f
--- /dev/null
+++ b/DEV_NOTES.txt
@@ -0,0 +1 @@
+Event id and "since" values can be compared as ascii text to see which one comes first. \ No newline at end of file
diff --git a/include/DownloadUtils.hpp b/include/DownloadUtils.hpp
index 3eff519..6d96cd6 100644
--- a/include/DownloadUtils.hpp
+++ b/include/DownloadUtils.hpp
@@ -1,5 +1,6 @@
#pragma once
+#include "Path.hpp"
#include <string>
#include <vector>
#include <functional>
@@ -21,7 +22,7 @@ namespace QuickMedia {
using DownloadErrorHandler = std::function<bool(std::string&)>;
DownloadResult download_to_string(const std::string &url, std::string &result, const std::vector<CommandArg> &additional_args, bool use_tor, bool use_browser_useragent = false, bool fail_on_error = true);
- DownloadResult download_to_string_cache(const std::string &url, std::string &result, const std::vector<CommandArg> &additional_args, bool use_tor, bool use_browser_useragent = false, DownloadErrorHandler error_handler = nullptr);
+ DownloadResult download_to_string_cache(const std::string &url, std::string &result, const std::vector<CommandArg> &additional_args, bool use_tor, bool use_browser_useragent = false, DownloadErrorHandler error_handler = nullptr, Path cache_path = "");
DownloadResult download_to_file(const std::string &url, const std::string &destination_filepath, const std::vector<CommandArg> &additional_args, bool use_tor, bool use_browser_useragent = false);
DownloadResult download_to_json(const std::string &url, rapidjson::Document &result, const std::vector<CommandArg> &additional_args, bool use_tor, bool use_browser_useragent = false, bool fail_on_error = true);
} \ No newline at end of file
diff --git a/include/Path.hpp b/include/Path.hpp
index 46c8dee..19c8eb9 100644
--- a/include/Path.hpp
+++ b/include/Path.hpp
@@ -37,6 +37,13 @@ namespace QuickMedia {
return "";
}
+ Path parent() {
+ size_t slash_index = data.rfind('/');
+ if(slash_index != std::string::npos || slash_index == 0)
+ return Path(data.substr(0, slash_index));
+ return Path("/");
+ }
+
std::string data;
};
} \ No newline at end of file
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<CommandArg> &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<CommandArg> &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 <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
@@ -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;
}