summaryrefslogtreecommitdiff
path: root/filecache.cpp
diff options
context:
space:
mode:
authorouwou <26526779+ouwou@users.noreply.github.com>2020-12-11 00:12:43 -0500
committerouwou <26526779+ouwou@users.noreply.github.com>2020-12-11 00:12:43 -0500
commita9e41482f53353629a1ea2d12a0b069a773f4f65 (patch)
tree7601c71abac83420cee064c2b6c00aab9cabbebb /filecache.cpp
parentdf781e0dc6cca6f610de317c91d2bd11530a06d1 (diff)
downloadabaddon-portaudio-a9e41482f53353629a1ea2d12a0b069a773f4f65.tar.gz
abaddon-portaudio-a9e41482f53353629a1ea2d12a0b069a773f4f65.zip
fix #7 and some other "improvements"
Diffstat (limited to 'filecache.cpp')
-rw-r--r--filecache.cpp20
1 files changed, 8 insertions, 12 deletions
diff --git a/filecache.cpp b/filecache.cpp
index 9a7c062..2b454f6 100644
--- a/filecache.cpp
+++ b/filecache.cpp
@@ -1,5 +1,6 @@
#include "abaddon.hpp"
#include "filecache.hpp"
+#include "murmurhash3.h"
constexpr static const int MaxConcurrentCacheHTTP = 10;
@@ -18,15 +19,10 @@ Cache::~Cache() {
fprintf(stderr, "error removing tmp dir\n");
}
-std::string Cache::SanitizeString(std::string str) {
- std::string ret;
- for (const char c : str) {
- if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z'))
- ret += c;
- else
- ret += '_';
- }
- return ret;
+std::string Cache::GetCachedName(std::string str) {
+ uint32_t out;
+ MurmurHash3_x86_32(str.c_str(), str.size(), 0, &out);
+ return std::to_string(out);
}
void Cache::RespondFromPath(std::filesystem::path path, callback_type cb) {
@@ -34,7 +30,7 @@ void Cache::RespondFromPath(std::filesystem::path path, callback_type cb) {
}
void Cache::GetFileFromURL(std::string url, callback_type cb) {
- auto cache_path = m_tmp_path / SanitizeString(url);
+ auto cache_path = m_tmp_path / GetCachedName(url);
if (std::filesystem::exists(cache_path)) {
m_futures.push_back(std::async(std::launch::async, [this, cache_path, cb]() { RespondFromPath(cache_path, cb); }));
return;
@@ -62,7 +58,7 @@ void Cache::GetFileFromURL(std::string url, callback_type cb) {
}
std::string Cache::GetPathIfCached(std::string url) {
- auto cache_path = m_tmp_path / SanitizeString(url);
+ auto cache_path = m_tmp_path / GetCachedName(url);
if (std::filesystem::exists(cache_path)) {
return cache_path.string();
}
@@ -85,7 +81,7 @@ void Cache::OnResponse(const cpr::Response &r) {
if (r.error || r.status_code > 300) return;
std::vector<uint8_t> data(r.text.begin(), r.text.end());
- auto path = m_tmp_path / SanitizeString(static_cast<std::string>(r.url));
+ auto path = m_tmp_path / GetCachedName(static_cast<std::string>(r.url));
FILE *fp = std::fopen(path.string().c_str(), "wb");
if (fp == nullptr)
return;