1 parent
9f06af6c80
commit
7975f2b38e
19 files changed
+2020
-747
No files matched your search
@@ -1,58 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include "NetworkClient.hpp"
|
||||
#include <NetworkTypes.hpp>
|
||||
#include <TrackTypes.hpp>
|
||||
#include <Types.hpp>
|
||||
#include <Void/ShortUrl/ShortUrl.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <string_view>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
namespace Echoion::Core
|
||||
{
|
||||
|
||||
// URL shortener service for faumaray.ru
|
||||
// Thin Core adapter around the universal Void ShortUrl service. It owns no
|
||||
// provider and performs no HTTP itself: the getter returns a pinned service
|
||||
// instance for each operation, making calls safe across plugin hot reloads.
|
||||
class UrlShortener
|
||||
{
|
||||
public:
|
||||
|
||||
explicit UrlShortener(std::shared_ptr<NetworkClient> networkClient);
|
||||
~UrlShortener();
|
||||
using ProviderGetter = std::function<
|
||||
std::shared_ptr<Void::ShortUrl::IShortener>()>;
|
||||
|
||||
void setProvider(ProviderGetter provider);
|
||||
void setSources(const std::vector<std::string>& sources);
|
||||
|
||||
// Shorten a YouTube URL via faumaray.ru
|
||||
// Returns shortened URL in format: https://faumaray.ru/xxxxx
|
||||
[[nodiscard]] Api::Result<std::string>
|
||||
shortenYouTubeUrl(std::string_view videoId);
|
||||
|
||||
// Shorten any URL via faumaray.ru
|
||||
[[nodiscard]] Api::Result<std::string>
|
||||
shortenUrl(std::string_view originalUrl);
|
||||
|
||||
// Get cached shortened URL if available
|
||||
[[nodiscard]] std::optional<std::string>
|
||||
getCachedUrl(std::string_view originalUrl) const;
|
||||
|
||||
// Clear the URL cache
|
||||
void clearCache();
|
||||
|
||||
// Set cache TTL (default: 24 hours)
|
||||
void setCacheTtl(std::chrono::hours ttl);
|
||||
shortenUrl(std::string_view originalUrl,
|
||||
Api::TrackSource source) const;
|
||||
|
||||
private:
|
||||
|
||||
struct CacheEntry {
|
||||
std::string shortenedUrl;
|
||||
std::chrono::system_clock::time_point timestamp;
|
||||
};
|
||||
[[nodiscard]] bool sourceAllowed(Api::TrackSource source) const;
|
||||
|
||||
std::shared_ptr<NetworkClient> networkClient_;
|
||||
mutable std::mutex cacheMutex_;
|
||||
std::unordered_map<std::string, CacheEntry> cache_;
|
||||
std::chrono::hours cacheTtl_{24};
|
||||
|
||||
// Clean expired entries from cache
|
||||
void cleanExpiredCache();
|
||||
mutable std::mutex mutex_;
|
||||
ProviderGetter provider_;
|
||||
std::unordered_set<Api::TrackSource> sources_ = {
|
||||
Api::TrackSource::YouTube};
|
||||
};
|
||||
|
||||
} // namespace Echoion::Core
|
||||
Reference in new issue
Block a user