Added podcast support, only from subsonic API for now, ref #726

This commit is contained in:
emeric
2025-09-13 15:04:13 +02:00
parent 1d584ebcc6
commit 932e7715f5
111 changed files with 4717 additions and 188 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ jobs:
with:
usesh: true
prepare: |
pkg install -y cmake pkgconf boost-libs ffmpeg stb libconfig taglib libarchive xxhash wt googletest
pkg install -y cmake pkgconf boost-libs ffmpeg stb libconfig taglib libarchive xxhash wt googletest pugixml
run: |
mkdir build
+1 -1
View File
@@ -28,7 +28,7 @@ jobs:
name: Install dependencies (cpp)
run: |
sudo apt-get update
sudo apt-get install --yes build-essential cmake libboost-all-dev libconfig++-dev libavcodec-dev libavutil-dev libavformat-dev libstb-dev libtag1-dev libpam0g-dev libgtest-dev libarchive-dev libxxhash-dev
sudo apt-get install --yes build-essential cmake libboost-all-dev libconfig++-dev libavcodec-dev libavutil-dev libavformat-dev libstb-dev libtag1-dev libpam0g-dev libgtest-dev libarchive-dev libxxhash-dev libpugixml-dev
export WT_VERSION=4.11.3
export WT_INSTALL_PREFIX=/usr
git clone https://github.com/emweb/wt.git /tmp/wt
+1
View File
@@ -18,6 +18,7 @@ if(ENABLE_TESTS)
endif()
# Common dependencies
find_package(OpenSSL REQUIRED)
find_package(PkgConfig REQUIRED)
find_package(Threads REQUIRED)
find_package(Filesystem REQUIRED)
+1
View File
@@ -20,6 +20,7 @@ ARG LMS_BUILD_PACKAGES=" \
ffmpeg-dev \
libarchive-dev \
libconfig-dev \
pugixml-dev \
taglib-dev \
stb \
wt-dev \
+1
View File
@@ -12,6 +12,7 @@ ARG BUILD_PACKAGES="\
libconfig \
make \
pkgconfig \
pugixml \
stb \
taglib \
wt \
+2
View File
@@ -22,6 +22,7 @@ ARG BUILD_PACKAGES=" \
curl \
libogg-dev \
opus-dev \
pugixml-dev \
libvorbis-dev \
lame-dev \
cmake \
@@ -164,6 +165,7 @@ ARG RUNTIME_PACKAGES=" \
boost-thread \
libarchive \
libconfig++ \
pugixml \
sqlite-libs"
ARG LMS_USER=lms
+1 -2
View File
@@ -37,13 +37,12 @@ __Notes__:
* a C++20 compiler is needed
* ffmpeg version 4 minimum is required
```sh
apt-get install g++ cmake libboost-program-options-dev libboost-system-dev libavutil-dev libavformat-dev libstb-dev libconfig++-dev ffmpeg libtag1-dev libpam0g-dev libgtest-dev libarchive-dev libxxhash-dev
apt-get install g++ cmake libboost-program-options-dev libboost-system-dev libavutil-dev libavformat-dev libstb-dev libconfig++-dev ffmpeg libtag1-dev libpam0g-dev libpugixml-dev libgtest-dev libarchive-dev libxxhash-dev libssl-dev
```
__Notes__:
* libpam0g-dev is optional (only for using PAM authentication)
* libstb-dev can be replaced by libgraphicsmagick++1-dev (the latter will likely use more RAM)
You also need _Wt4_, which is not packaged on _Debian_. See [installation instructions](https://www.webtoolkit.eu/wt/doc/reference/html/InstallationUnix.html).</br>
No optional requirement is needed, except openSSL if you plan not to deploy behind a reverse proxy (which is not recommended).
### Build
Get the latest stable release and build it:
```sh
+1
View File
@@ -21,6 +21,7 @@ A [demo instance](http://lms-demo.poupon.dev) is available. Note the administrat
* ReplayGain support
* Audio transcoding for compatibility and reduced bandwidth
* User management, with several [authentication backends](INSTALL.md#authentication-backend)
* Podcasts support
* Playlists support
* Lyrics support
+1
View File
@@ -62,6 +62,7 @@ The following extra fields are implemented:
## Supported extensions
* [API Key Authentication](https://opensubsonic.netlify.app/docs/extensions/apikeyauth/)
* [getPodcastEpisode](https://opensubsonic.netlify.app/docs/extensions/getpodcastepisode/)
* [HTTP form POST](https://opensubsonic.netlify.app/docs/extensions/formpost/)
* [Transcode offset](https://opensubsonic.netlify.app/docs/extensions/transcodeoffset/)
* [Song Lyrics](https://opensubsonic.netlify.app/docs/extensions/songlyrics/)
+9
View File
@@ -127,3 +127,12 @@ scanner-parser-read-style = "average";
# Number of threads to use for parallelized tasks (e.g., scanning file metadata). 0 means half the number of logical CPUs.
scanner-thread-count = 0;
# Refresh period for podcast feeds in hours (must be greater or equal than 1)
podcast-refresh-period-hours = 2;
# Automatically download new episodes
podcast-auto-download-episodes = true;
# Max age in days for auto-downloaded episodes before deletion
podcast-auto-download-episodes-max-age-days = 30;
+1
View File
@@ -46,6 +46,7 @@ target_include_directories(lmscore PRIVATE
target_link_libraries(lmscore PRIVATE
PkgConfig::Config++
PkgConfig::Archive
OpenSSL::Crypto
)
target_link_libraries(lmscore PUBLIC
+2
View File
@@ -59,6 +59,8 @@ namespace lms::core::logging
return "MAIN";
case Module::METADATA:
return "METADATA";
case Module::PODCAST:
return "PODCAST";
case Module::REMOTE:
return "REMOTE";
case Module::SCROBBLING:
+1
View File
@@ -67,6 +67,7 @@ namespace lms::core
{ ".jpg", "image/jpeg" },
{ ".jpeg", "image/jpeg" },
{ ".png", "image/png" },
{ ".svg", "image/svg+xml" },
{ ".webp", "image/webp" },
};
+2 -9
View File
@@ -19,22 +19,15 @@
#include "core/Path.hpp"
#include <algorithm>
#include <array>
#include <cassert>
#include <unistd.h>
#include "core/ILogger.hpp"
#include "core/String.hpp"
namespace lms::core::pathUtils
{
bool ensureDirectory(const std::filesystem::path& dir)
{
if (std::filesystem::exists(dir))
return std::filesystem::is_directory(dir);
else
return std::filesystem::create_directory(dir);
}
bool hasFileAnyExtension(const std::filesystem::path& file, std::span<const std::filesystem::path> supportedExtensions)
{
const std::filesystem::path extension{ stringUtils::stringToLower(file.extension().c_str()) };
+121 -5
View File
@@ -20,6 +20,8 @@
#include "core/String.hpp"
#include <algorithm>
#include <charconv>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <utility>
@@ -133,6 +135,64 @@ namespace lms::core::stringUtils
res.push_back(str.substr(currentPos));
return res;
}
static std::optional<std::chrono::minutes> getRFC822ZoneOffset(std::string_view zoneStr)
{
if (zoneStr == "UT" || zoneStr == "GMT" || zoneStr == "Z") return std::chrono::hours{ 0 };
if (zoneStr == "EST") return -std::chrono::hours{ 5 };
if (zoneStr == "EDT") return -std::chrono::hours{ 4 };
if (zoneStr == "CST") return -std::chrono::hours{ 6 };
if (zoneStr == "CDT") return -std::chrono::hours{ 5 };
if (zoneStr == "MST") return -std::chrono::hours{ 7 };
if (zoneStr == "MDT") return -std::chrono::hours{ 6 };
if (zoneStr == "PST") return -std::chrono::hours{ 8 };
if (zoneStr == "PDT") return -std::chrono::hours{ 7 };
if (zoneStr.size() == 1)
{
const char c{ zoneStr[0] };
if (c == 'A') return -std::chrono::hours{ 1 };
if (c == 'M') return -std::chrono::hours{ 12 };
if (c == 'N') return std::chrono::hours{ 1 };
if (c == 'Y') return std::chrono::hours{ 12 };
return std::nullopt;
}
// (+/-)HHMM
if (zoneStr[0] != '+' && zoneStr[0] != '-')
return std::nullopt;
if (zoneStr.size() != 5)
return std::nullopt;
if (!std::all_of(std::cbegin(zoneStr) + 1, std::cend(zoneStr), [](char c) { return std::isdigit(c); }))
return std::nullopt;
int hours{};
const auto [p, ec]{ std::from_chars(zoneStr.data() + 1, zoneStr.data() + 3, hours) };
if (ec != std::errc())
return std::nullopt;
int minutes{};
const auto [p2, ec2] = std::from_chars(zoneStr.data() + 3, zoneStr.data() + 5, minutes);
if (ec2 != std::errc())
return std::nullopt;
std::chrono::minutes res{ std::chrono::hours{ hours } + std::chrono::minutes{ minutes } };
if (zoneStr[0] == '-')
res = -res;
return res;
}
std::optional<unsigned> getRFC822Month(std::string_view monthStr)
{
static const std::array<std::string_view, 12> months{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
const std::string_view* str{ std::find(std::cbegin(months), std::cend(months), monthStr) };
if (str == std::cend(months))
return {};
return std::distance(std::cbegin(months), str) + 1;
}
} // namespace details
template<>
@@ -435,9 +495,9 @@ namespace lms::core::stringUtils
return str.substr(str.length() - ending.length()) == ending;
}
std::optional<std::string> stringFromHex(const std::string& str)
std::optional<std::string> stringFromHex(std::string_view str)
{
static const char lut[]{ "0123456789ABCDEF" };
constexpr char lut[]{ "0123456789ABCDEF" };
if (str.length() % 2 != 0)
return std::nullopt;
@@ -445,13 +505,13 @@ namespace lms::core::stringUtils
std::string res;
res.reserve(str.length() / 2);
auto it{ std::cbegin(str) };
const char* it{ std::cbegin(str) };
while (it != std::cend(str))
{
unsigned val{};
auto itHigh{ std::lower_bound(std::cbegin(lut), std::cend(lut), std::toupper(*(it++))) };
auto itLow{ std::lower_bound(std::cbegin(lut), std::cend(lut), std::toupper(*(it++))) };
const char* itHigh{ std::lower_bound(std::cbegin(lut), std::cend(lut), std::toupper(*(it++))) };
const char* itLow{ std::lower_bound(std::cbegin(lut), std::cend(lut), std::toupper(*(it++))) };
if (itHigh == std::cend(lut) || itLow == std::cend(lut))
return {};
@@ -465,6 +525,21 @@ namespace lms::core::stringUtils
return res;
}
std::string toHexString(std::string_view str)
{
constexpr char lut[]{ "0123456789ABCDEF" };
std::string res;
for (char c : str)
{
res.push_back(lut[(c >> 4) & 0xF]);
res.push_back(lut[c & 0xF]);
}
return res;
}
std::string toISO8601String(const Wt::WDateTime& dateTime)
{
if (dateTime.isValid())
@@ -496,6 +571,47 @@ namespace lms::core::stringUtils
return Wt::WDateTime::fromString(Wt::WString{ std::string{ dateTime } }, "yyyy-MM-ddThh:mm:ss.zzz");
}
Wt::WDateTime fromRFC822String(std::string_view dateTime)
{
// Expect something like "[Sun,] 6 Nov 1994 08:49[:37] GMT"
if (dateTime.size() > 4 && dateTime[3] == ',')
dateTime.remove_prefix(5);
// Extract parts
const std::vector<std::string_view> subParts{ splitString(dateTime, ' ') };
if (subParts.size() != 5)
return {};
const std::string_view dayStr{ subParts[0] };
const std::string_view monthStr{ subParts[1] };
const std::string_view yearStr{ subParts[2] };
std::string timeStr{ subParts[3] };
const std::string_view zoneStr{ subParts[4] };
if (std::count(std::cbegin(timeStr), std::cend(timeStr), ':') == 1)
timeStr += ":00";
// Normalize zone
const std::optional<std::chrono::minutes> offset{ details::getRFC822ZoneOffset(zoneStr) };
if (!offset)
return {};
std::optional<unsigned> month{ details::getRFC822Month(monthStr) };
if (!month)
return {};
std::string datetimeStr;
datetimeStr = dayStr;
datetimeStr += " ";
datetimeStr += std::to_string(month.value());
datetimeStr += " ";
datetimeStr += yearStr;
datetimeStr += " ";
datetimeStr += timeStr;
const Wt::WDateTime res{ Wt::WDateTime::fromString(Wt::WString{ datetimeStr }, "d M yyyy HH:mm:ss") };
return res.addSecs(static_cast<int>(std::chrono::duration_cast<std::chrono::seconds>(offset.value()).count()));
}
std::string formatTimestamp(std::chrono::milliseconds timestamp)
{
using namespace std::chrono;
+5
View File
@@ -36,4 +36,9 @@ namespace lms::core::http
{
_sendQueue.sendRequest(std::make_unique<ClientRequest>(std::move(POSTParams)));
}
void Client::abortAllRequests()
{
_sendQueue.abortAllRequests();
}
} // namespace lms::core::http
+1
View File
@@ -40,6 +40,7 @@ namespace lms::core::http
private:
void sendGETRequest(ClientGETRequestParameters&& request) override;
void sendPOSTRequest(ClientPOSTRequestParameters&& request) override;
void abortAllRequests() override;
SendQueue _sendQueue;
};
+130 -28
View File
@@ -19,16 +19,19 @@
#include "SendQueue.hpp"
#include <latch>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/ssl/error.hpp>
#include "core/Exception.hpp"
#include "core/ILogger.hpp"
#include "core/ITraceLogger.hpp"
#include "core/String.hpp"
#define LOG(sev, message) LMS_LOG(SCROBBLING, sev, "[Http SendQueue] - " << message)
#define LOG(sev, message) LMS_LOG(HTTP, sev, "[Http SendQueue] - " << message)
namespace lms::core::stringUtils
{
@@ -63,7 +66,21 @@ namespace lms::core::http
SendQueue::SendQueue(boost::asio::io_context& ioContext, std::string_view baseUrl)
: _ioContext{ ioContext }
, _baseUrl{ baseUrl }
, _abortAllRequests{ false }
, _state{ State::Idle }
, _client{ _ioContext }
{
_client.setFollowRedirect(true);
_client.setTimeout(std::chrono::seconds{ 5 });
// not very efficient (response bodies are copied for each callback), but Wt's code already makes copies anyway
_client.bodyDataReceived().connect([this](const std::string& data) {
boost::asio::post(boost::asio::bind_executor(_strand, [this, data] {
onClientBodyDataReceived(data);
}));
});
_client.done().connect([this](Wt::AsioWrapper::error_code ec, const Wt::Http::Message& msg) {
boost::asio::post(boost::asio::bind_executor(_strand, [this, ec, msg = std::move(msg)] {
onClientDone(ec, msg);
@@ -73,12 +90,60 @@ namespace lms::core::http
SendQueue::~SendQueue()
{
abortAllRequests();
}
void SendQueue::abortAllRequests()
{
LOG(DEBUG, "Aborting all requests...");
assert(!_abortAllRequests);
_abortAllRequests = true;
std::latch abortLatch{ 1 };
boost::asio::post(boost::asio::bind_executor(_strand, [this, &abortLatch] {
for (auto& [prio, requests] : _sendQueue)
{
while (!requests.empty())
{
std::unique_ptr<ClientRequest> request{ std::move(requests.front()) };
requests.pop_front();
if (request->getParameters().onAbortFunc)
request->getParameters().onAbortFunc();
}
}
if (_state == State::Throttled)
_throttleTimer.cancel();
else if (_state == State::Sending)
_client.abort();
abortLatch.count_down();
}));
abortLatch.wait();
while (_state != State::Idle)
std::this_thread::yield();
_abortAllRequests = false;
LOG(DEBUG, "All requests aborted!");
}
void SendQueue::sendRequest(std::unique_ptr<ClientRequest> request)
{
boost::asio::dispatch(_strand, [this, request = std::move(request)]() mutable {
boost::asio::post(_strand, [this, request = std::move(request)]() mutable {
if (_abortAllRequests)
{
LOG(DEBUG, "Not posting request because abortAllRequests() in progress");
if (request->getParameters().onAbortFunc)
request->getParameters().onAbortFunc();
return;
}
_sendQueue[request->getParameters().priority].emplace_back(std::move(request));
if (_state == State::Idle)
@@ -88,7 +153,7 @@ namespace lms::core::http
void SendQueue::sendNextQueuedRequest()
{
assert(_state == State::Idle);
assert(_strand.running_in_this_thread());
assert(!_currentRequest);
for (auto& [prio, requests] : _sendQueue)
@@ -100,21 +165,31 @@ namespace lms::core::http
requests.pop_front();
if (!sendRequest(*request))
{
if (request->getParameters().onFailureFunc)
request->getParameters().onFailureFunc();
continue;
}
_state = State::Sending;
setState(State::Sending);
_currentRequest = std::move(request);
return;
}
}
setState(State::Idle);
}
bool SendQueue::sendRequest(const ClientRequest& request)
{
assert(_strand.running_in_this_thread());
LMS_SCOPED_TRACE_DETAILED("SendQueue", "SendRequest");
std::string url{ _baseUrl + request.getParameters().relativeUrl };
LOG(DEBUG, "Sending request to url '" << url << "'");
const std::string url{ _baseUrl + request.getParameters().relativeUrl };
LOG(DEBUG, "Sending " << (request.getType() == ClientRequest::Type::GET ? "GET" : "POST") << " request to url '" << url << "'");
_client.setMaximumResponseSize(request.getParameters().onChunkReceived ? 0 : request.getParameters().responseBufferSize);
bool res{};
switch (request.getType())
@@ -134,29 +209,50 @@ namespace lms::core::http
return res;
}
void SendQueue::onClientBodyDataReceived(const std::string& data)
{
assert(_strand.running_in_this_thread());
assert(_currentRequest);
if (_currentRequest->getParameters().onChunkReceived)
{
const auto byteSpan{ std::as_bytes(std::span{ data.data(), data.size() }) };
if (_currentRequest->getParameters().onChunkReceived(byteSpan) == ClientRequestParameters::ChunckReceivedResult::Abort)
_client.abort();
}
}
void SendQueue::onClientDone(Wt::AsioWrapper::error_code ec, const Wt::Http::Message& msg)
{
LMS_SCOPED_TRACE_DETAILED("SendQueue", "OnClientDone");
if (ec == boost::asio::error::operation_aborted)
{
LOG(DEBUG, "Client aborted");
return;
}
assert(_currentRequest);
_state = State::Idle;
LOG(DEBUG, "Client done. status = " << msg.status());
if (ec)
LOG(DEBUG, "Client done. ec = " << ec.category().name() << " - " << ec.message() << " (" << ec.value() << "), status = " << msg.status());
if (_abortAllRequests || ec == boost::asio::error::operation_aborted)
onClientAborted(std::move(_currentRequest));
else if (ec && (ec != boost::asio::ssl::error::stream_truncated))
onClientDoneError(std::move(_currentRequest), ec);
else
onClientDoneSuccess(std::move(_currentRequest), msg);
}
void SendQueue::onClientAborted(std::unique_ptr<ClientRequest> request)
{
assert(_strand.running_in_this_thread());
if (request->getParameters().onAbortFunc)
request->getParameters().onAbortFunc();
sendNextQueuedRequest();
}
void SendQueue::onClientDoneError(std::unique_ptr<ClientRequest> request, Wt::AsioWrapper::error_code ec)
{
LOG(ERROR, "Retry " << request->retryCount << ", client error: '" << ec.message() << "'");
assert(_strand.running_in_this_thread());
LOG(WARNING, "Retry " << request->retryCount << ", client error: '" << ec.message() << "'");
// may be a network error, try again later
throttle(_defaultRetryWaitDuration);
@@ -196,42 +292,48 @@ namespace lms::core::http
if (msg.status() == 200)
{
if (requestParameters.onSuccessFunc)
requestParameters.onSuccessFunc(msg.body());
requestParameters.onSuccessFunc(msg);
}
else
{
LOG(ERROR, "Send error: '" << msg.body() << "'");
LOG(ERROR, "Send error, status = " << msg.status() << ", body = '" << msg.body() << "'");
if (requestParameters.onFailureFunc)
requestParameters.onFailureFunc();
}
}
if (_state == State::Idle)
if (_state != State::Throttled)
sendNextQueuedRequest();
}
void SendQueue::throttle(std::chrono::seconds requestedDuration)
{
assert(_state == State::Idle);
const std::chrono::seconds duration{ clamp(requestedDuration, _minRetryWaitDuration, _maxRetryWaitDuration) };
LOG(DEBUG, "Throttling for " << duration.count() << " seconds");
_throttleTimer.expires_after(duration);
_throttleTimer.async_wait([this](const boost::system::error_code& ec) {
if (ec == boost::asio::error::operation_aborted)
{
LOG(DEBUG, "Throttle aborted");
return;
}
else if (ec)
{
throw LmsException{ "Throttle timer failure: " + std::string{ ec.message() } };
}
_state = State::Idle;
setState(State::Idle);
if (!ec)
sendNextQueuedRequest();
});
_state = State::Throttled;
setState(State::Throttled);
}
void SendQueue::setState(State state)
{
assert(_strand.running_in_this_thread());
if (_state != state)
{
LOG(DEBUG, "Changing state to " << (state == State::Idle ? "Idle" : state == State::Sending ? "Sending" :
"Throttled"));
_state = state;
}
}
} // namespace lms::core::http
+10 -6
View File
@@ -19,9 +19,9 @@
#pragma once
#include <atomic>
#include <deque>
#include <string_view>
#include <vector>
#include <Wt/Http/Client.h>
#include <boost/asio/io_context.hpp>
@@ -44,10 +44,13 @@ namespace lms::core::http
SendQueue& operator=(const SendQueue&&) = delete;
void sendRequest(std::unique_ptr<ClientRequest> request);
void abortAllRequests();
private:
void sendNextQueuedRequest();
bool sendRequest(const ClientRequest& request);
void onClientBodyDataReceived(const std::string& data);
void onClientAborted(std::unique_ptr<ClientRequest> request);
void onClientDone(Wt::AsioWrapper::error_code ec, const Wt::Http::Message& msg);
void onClientDoneError(std::unique_ptr<ClientRequest> request, Wt::AsioWrapper::error_code ec);
void onClientDoneSuccess(std::unique_ptr<ClientRequest> request, const Wt::Http::Message& msg);
@@ -59,9 +62,9 @@ namespace lms::core::http
const std::chrono::seconds _maxRetryWaitDuration{ 300 };
boost::asio::io_context& _ioContext;
boost::asio::io_context::strand _strand{ _ioContext };
boost::asio::io_context::strand _strand{ _ioContext }; // protect _state, _sendQueue and _currentRequest
boost::asio::steady_timer _throttleTimer{ _ioContext };
std::string _baseUrl;
const std::string _baseUrl;
enum class State
{
@@ -69,10 +72,11 @@ namespace lms::core::http
Throttled,
Sending,
};
State _state{ State::Idle };
Wt::Http::Client _client{ _ioContext };
void setState(State state);
std::atomic<bool> _abortAllRequests;
State _state;
Wt::Http::Client _client;
std::map<ClientRequestParameters::Priority, std::deque<std::unique_ptr<ClientRequest>>> _sendQueue;
std::unique_ptr<ClientRequest> _currentRequest;
};
} // namespace lms::core::http
+1
View File
@@ -51,6 +51,7 @@ namespace lms::core::logging
HTTP,
MAIN,
METADATA,
PODCAST,
REMOTE,
SCROBBLING,
SERVICE,
-6
View File
@@ -24,14 +24,8 @@
#include <string>
#include <string_view>
#include <Wt/WDateTime.h>
namespace lms::core::pathUtils
{
// Make sure the given path is a directory
// Create it if needed
bool ensureDirectory(const std::filesystem::path& dir);
// Check if file's extension is one of provided extensions
bool hasFileAnyExtension(const std::filesystem::path& file, std::span<const std::filesystem::path> extensions);
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstddef>
namespace lms::core::literals
{
constexpr std::size_t operator""_KiB(unsigned long long int x)
{
return 1024ULL * x;
}
constexpr std::size_t operator""_MiB(unsigned long long int x)
{
return 1024_KiB * x;
}
constexpr std::size_t operator""_GiB(unsigned long long int x)
{
return 1024_MiB * x;
}
constexpr std::size_t operator""_TiB(unsigned long long int x)
{
return 1024_GiB * x;
}
constexpr std::size_t operator""_PiB(unsigned long long int x)
{
return 1024_TiB * x;
}
} // namespace lms::core::literals
+3 -1
View File
@@ -110,12 +110,14 @@ namespace lms::core::stringUtils
[[nodiscard]] bool stringEndsWith(std::string_view str, std::string_view ending);
[[nodiscard]] std::optional<std::string> stringFromHex(const std::string& str);
[[nodiscard]] std::optional<std::string> stringFromHex(std::string_view str);
[[nodiscard]] std::string toHexString(std::string_view str);
[[nodiscard]] std::string toISO8601String(const Wt::WDateTime& dateTime);
[[nodiscard]] std::string toISO8601String(const Wt::WDate& date);
[[nodiscard]] Wt::WDateTime fromISO8601String(std::string_view dateTime);
[[nodiscard]] Wt::WDateTime fromRFC822String(std::string_view dateTime);
// to "[minutes:seconds.milliseconds]"
std::string formatTimestamp(std::chrono::milliseconds timestamp);
@@ -19,8 +19,9 @@
#pragma once
#include <cstddef>
#include <functional>
#include <string_view>
#include <span>
#include <vector>
#include <Wt/Http/Message.h>
@@ -38,12 +39,27 @@ namespace lms::core::http
Priority priority{ Priority::Normal };
std::string relativeUrl; // relative to baseUrl used by the client
std::size_t responseBufferSize{ 10 * 1024 * 1024 }; // only used if onChunkReceived is not set
using OnSuccessFunc = std::function<void(std::string_view msgBody)>;
// If `onChunkReceived` is set, the response will be streamed in chunks.
// In that case, `onSuccessFunc` is still called at the end (with an empty msgBody).
// If `onChunkReceived` is not set, the response will be fully buffered and passed to `onSuccessFunc`.
enum class ChunckReceivedResult
{
Continue,
Abort,
};
using OnChunkReceived = std::function<ChunckReceivedResult(std::span<const std::byte> chunk)>; // return false to stop (onFailureFunc callback will be called)
OnChunkReceived onChunkReceived;
using OnSuccessFunc = std::function<void(const Wt::Http::Message& msg)>;
OnSuccessFunc onSuccessFunc;
using OnFailureFunc = std::function<void()>;
OnFailureFunc onFailureFunc;
using OnAbortFunc = std::function<void()>;
OnAbortFunc onAbortFunc;
};
struct ClientGETRequestParameters final : public ClientRequestParameters
@@ -27,6 +27,8 @@
namespace lms::core::http
{
// Very simple http client, will handle all requests sequentially.
// User callbacks are dispatched within a strand.
class IClient
{
public:
@@ -34,6 +36,8 @@ namespace lms::core::http
virtual void sendGETRequest(ClientGETRequestParameters&& request) = 0;
virtual void sendPOSTRequest(ClientPOSTRequestParameters&& request) = 0;
virtual void abortAllRequests() = 0;
};
std::unique_ptr<IClient> createClient(boost::asio::io_context& ioContext, std::string_view baseUrl);
+30
View File
@@ -338,6 +338,20 @@ namespace lms::core::stringUtils::tests
EXPECT_EQ(fromISO8601String(""), Wt::WDateTime{});
}
TEST(Stringutils, DateTimeFromRFC822String)
{
EXPECT_EQ(fromRFC822String(""), Wt::WDateTime{});
EXPECT_EQ(fromRFC822String("Mon, 3 Jan 2020 09:08:11 UT"), (Wt::WDateTime{ Wt::WDate{ 2020, 01, 03 }, Wt::WTime{ 9, 8, 11, 0 } }));
EXPECT_EQ(fromRFC822String("3 Jan 2020 09:08:11 UT"), (Wt::WDateTime{ Wt::WDate{ 2020, 01, 03 }, Wt::WTime{ 9, 8, 11, 0 } }));
EXPECT_EQ(fromRFC822String("3 Jan 2020 09:08:11 +0200"), (Wt::WDateTime{ Wt::WDate{ 2020, 01, 03 }, Wt::WTime{ 11, 8, 11, 0 } }));
EXPECT_EQ(fromRFC822String("3 Jan 2020 09:08 +0200"), (Wt::WDateTime{ Wt::WDate{ 2020, 01, 03 }, Wt::WTime{ 11, 8, 00, 0 } }));
EXPECT_EQ(fromRFC822String("3 Jan 2020 09:08:11 -0200"), (Wt::WDateTime{ Wt::WDate{ 2020, 01, 03 }, Wt::WTime{ 7, 8, 11, 0 } }));
EXPECT_EQ(fromRFC822String("3 Jan 2020 01:08:11 -0200"), (Wt::WDateTime{ Wt::WDate{ 2020, 01, 02 }, Wt::WTime{ 23, 8, 11, 0 } }));
EXPECT_EQ(fromRFC822String("3 Jan 2020 01:08:11 -0230"), (Wt::WDateTime{ Wt::WDate{ 2020, 01, 02 }, Wt::WTime{ 22, 38, 11, 0 } }));
EXPECT_EQ(fromRFC822String("3 Jan 2020 10:08:11 CST"), (Wt::WDateTime{ Wt::WDate{ 2020, 01, 03 }, Wt::WTime{ 04, 8, 11, 0 } }));
EXPECT_EQ(fromRFC822String("Sat, 09 Aug 2025 21:34:32 +0200"), (Wt::WDateTime{ Wt::WDate{ 2025, 8, 9 }, Wt::WTime{ 23, 34, 32 } }));
}
TEST(StringUtils, stringEndsWith)
{
EXPECT_TRUE(stringEndsWith("FooBar", "Bar"));
@@ -361,4 +375,20 @@ namespace lms::core::stringUtils::tests
EXPECT_TRUE(stringCaseInsensitiveContains("", ""));
EXPECT_FALSE(stringCaseInsensitiveContains("", "Foo"));
}
TEST(StringUtils, toHexString)
{
EXPECT_EQ(toHexString(""), "");
EXPECT_EQ(toHexString("123"), "313233");
EXPECT_EQ(toHexString("1234"), "31323334");
EXPECT_EQ(toHexString("12345"), "3132333435");
EXPECT_EQ(toHexString("Test"), "54657374");
// test back stringFromHex
EXPECT_EQ(stringFromHex(""), "");
EXPECT_EQ(stringFromHex("313233"), "123");
EXPECT_EQ(stringFromHex("31323334"), "1234");
EXPECT_EQ(stringFromHex("3132333435"), "12345");
EXPECT_EQ(stringFromHex("54657374"), "Test");
}
} // namespace lms::core::stringUtils::tests
+2
View File
@@ -11,6 +11,8 @@ add_library(lmsdatabase STATIC
impl/objects/Medium.cpp
impl/objects/PlayListFile.cpp
impl/objects/PlayQueue.cpp
impl/objects/Podcast.cpp
impl/objects/PodcastEpisode.cpp
impl/objects/TrackArtistLink.cpp
impl/objects/TrackFeatures.cpp
impl/objects/TrackList.cpp
+3
View File
@@ -21,6 +21,7 @@
#include <memory>
#include <Wt/Dbo/FixedSqlConnectionPool.h>
#include <Wt/Dbo/Logger.h>
#include <Wt/Dbo/backend/Sqlite3.h>
#include "core/IConfig.hpp"
@@ -193,6 +194,8 @@ namespace lms::db
// Session living class handling the database and the login
Db::Db(const std::filesystem::path& dbPath, std::size_t connectionCount)
{
Wt::Dbo::logToWt();
std::string checkType{ "quick" };
LMS_LOG(DB, INFO, "Creating connection pool on file " << dbPath);
+55 -1
View File
@@ -34,7 +34,7 @@ namespace lms::db
{
namespace
{
static constexpr Version LMS_DATABASE_VERSION{ 99 };
static constexpr Version LMS_DATABASE_VERSION{ 100 };
}
VersionInfo::VersionInfo()
@@ -1528,6 +1528,59 @@ FROM track)");
utils::executeCommand(*session.getDboSession(), "UPDATE scan_settings SET audio_scan_version = audio_scan_version + 1");
}
void migrateFromV99(Session& session)
{
// Podcast support
utils::executeCommand(*session.getDboSession(), "ALTER TABLE image ADD COLUMN mime_type TEXT NOT NULL DEFAULT ''");
utils::executeCommand(*session.getDboSession(), R"(CREATE TABLE IF NOT EXISTS "podcast" (
"id" integer primary key autoincrement,
"version" integer not null,
"url" text not null,
"delete_requested" boolean not null,
"title" text not null,
"link" text not null,
"description" text not null,
"language" text not null,
"copyright" text not null,
"last_build_date" text,
"author" text not null,
"category" text not null,
"explicit" boolean not null,
"image_url" text not null,
"owner_email" text not null,
"owner_name" text not null,
"subtitle" text not null,
"summary" text not null,
"artwork_id" bigint,
constraint "fk_podcast_artwork" foreign key ("artwork_id") references "artwork" ("id") on delete set null deferrable initially deferred))");
utils::executeCommand(*session.getDboSession(), R"(CREATE TABLE IF NOT EXISTS "podcast_episode" (
"id" integer primary key autoincrement,
"version" integer not null,
"manual_download_state" integer not null,
"audio_relative_file_path" text not null,
"title" text not null,
"link" text not null,
"description" text not null,
"author" text not null,
"category" text not null,
"enclosure_url" text not null,
"enclosure_content_type" text not null,
"enclosure_size" integer not null,
"pub_date" text,
"image_url" text not null,
"subtitle" text not null,
"summary" text not null,
"explicit" boolean not null,
"duration" integer,
"artwork_id" bigint,
"podcast_id" bigint,
constraint "fk_podcast_episode_artwork" foreign key ("artwork_id") references "artwork" ("id") on delete set null deferrable initially deferred,
constraint "fk_podcast_episode_podcast" foreign key ("podcast_id") references "podcast" ("id") on delete cascade deferrable initially deferred))");
}
bool doDbMigration(Session& session)
{
constexpr std::string_view outdatedMsg{ "Outdated database, please rebuild it (delete the .db file and restart)" };
@@ -1603,6 +1656,7 @@ FROM track)");
{ 96, migrateFromV96 },
{ 97, migrateFromV97 },
{ 98, migrateFromV98 },
{ 99, migrateFromV99 },
};
bool migrationPerformed{};
+1 -1
View File
@@ -23,7 +23,7 @@
namespace lms::db
{
void ObjectPtrBase::checkWriteTransaction(Wt::Dbo::Session& session)
void ObjectPtrBase::checkWriteTransaction([[maybe_unused]] Wt::Dbo::Session& session)
{
#if LMS_CHECK_TRANSACTION_ACCESSES
TransactionChecker::checkWriteTransaction(session);
+4
View File
@@ -36,6 +36,8 @@
#include "database/objects/Medium.hpp"
#include "database/objects/PlayListFile.hpp"
#include "database/objects/PlayQueue.hpp"
#include "database/objects/Podcast.hpp"
#include "database/objects/PodcastEpisode.hpp"
#include "database/objects/RatedArtist.hpp"
#include "database/objects/RatedRelease.hpp"
#include "database/objects/RatedTrack.hpp"
@@ -86,6 +88,8 @@ namespace lms::db
_session.mapClass<Medium>("medium");
_session.mapClass<PlayListFile>("playlist_file");
_session.mapClass<PlayQueue>("playqueue");
_session.mapClass<Podcast>("podcast");
_session.mapClass<PodcastEpisode>("podcast_episode");
_session.mapClass<RatedArtist>("rated_artist");
_session.mapClass<RatedRelease>("rated_release");
_session.mapClass<RatedTrack>("rated_track");
@@ -80,6 +80,18 @@ namespace lms::db
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<Artwork>>("SELECT a FROM artwork a").where("a.image_id = ?").bind(id));
}
Artwork::UnderlyingId Artwork::getUnderlyingId() const
{
Artwork::UnderlyingId res;
if (const TrackEmbeddedImageId embeddedImageId{ _trackEmbeddedImage.id() }; embeddedImageId.isValid())
res = embeddedImageId;
else if (const ImageId imageId{ _image.id() }; imageId.isValid())
res = imageId;
return res;
}
Wt::WDateTime Artwork::getLastWrittenTime() const
{
auto query{ session()->query<Wt::WDateTime>("SELECT MAX(COALESCE(image.file_last_write, track.file_last_write)) AS last_written_datetime FROM artwork") };
@@ -104,4 +116,14 @@ namespace lms::db
return utils::fetchQuerySingleResult(query);
}
ObjectPtr<Image> Artwork::getImage() const
{
return _image;
}
ImageId Artwork::getImageId() const
{
return _image.id();
}
} // namespace lms::db
@@ -0,0 +1,87 @@
/*
* Copyright (C) 2020 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "database/objects/Podcast.hpp"
#include <Wt/Dbo/Impl.h>
#include <Wt/Dbo/WtSqlTraits.h>
#include "database/Session.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/PodcastEpisode.hpp"
#include "Utils.hpp"
#include "traits/IdTypeTraits.hpp"
#include "traits/StringViewTraits.hpp"
DBO_INSTANTIATE_TEMPLATES(lms::db::Podcast)
namespace lms::db
{
Podcast::Podcast(std::string_view url)
: _url{ url }
{
}
Podcast::pointer Podcast::create(Session& session, std::string_view url)
{
return session.getDboSession()->add(std::unique_ptr<Podcast>{ new Podcast{ url } });
}
std::size_t Podcast::getCount(Session& session)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<int>("SELECT COUNT(*) FROM podcast"));
}
Podcast::pointer Podcast::find(Session& session, PodcastId id)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<Podcast>>("SELECT p from podcast p").where("p.id = ?").bind(id));
}
Podcast::pointer Podcast::find(Session& session, std::string_view url)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<Podcast>>("SELECT p from podcast p").where("p.url = ?").bind(url));
}
void Podcast::find(Session& session, std::function<void(const pointer&)> func)
{
session.checkReadTransaction();
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<Podcast>>("SELECT p from podcast p") };
utils::forEachQueryResult(query, func);
}
ObjectPtr<Artwork> Podcast::getArtwork() const
{
return _artwork;
}
ArtworkId Podcast::getArtworkId() const
{
return _artwork.id();
}
void Podcast::setArtwork(ObjectPtr<Artwork> artwork)
{
_artwork = getDboPtr(artwork);
}
} // namespace lms::db
@@ -0,0 +1,115 @@
/*
* Copyright (C) 2020 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "database/objects/PodcastEpisode.hpp"
#include <Wt/Dbo/Impl.h>
#include <Wt/Dbo/WtSqlTraits.h>
#include "database/Session.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/Podcast.hpp"
#include "Utils.hpp"
#include "traits/IdTypeTraits.hpp"
#include "traits/PathTraits.hpp"
DBO_INSTANTIATE_TEMPLATES(lms::db::PodcastEpisode)
namespace lms::db
{
namespace
{
Wt::Dbo::Query<Wt::Dbo::ptr<PodcastEpisode>> createQuery(Session& session, const PodcastEpisode::FindParameters& params)
{
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<PodcastEpisode>>("SELECT p_e from podcast_episode p_e") };
if (params.manualDownloadState.has_value())
query.where("p_e.manual_download_state = ?").bind(static_cast<int>(params.manualDownloadState.value()));
if (params.podcast.isValid())
query.where("p_e.podcast_id = ?").bind(params.podcast);
switch (params.sortMode)
{
case PodcastEpisodeSortMode::None:
break;
case PodcastEpisodeSortMode::PubDateAsc:
query.orderBy("p_e.pub_date ASC");
break;
case PodcastEpisodeSortMode::PubDateDesc:
query.orderBy("p_e.pub_date DESC");
break;
}
return query;
}
} // namespace
PodcastEpisode::PodcastEpisode(ObjectPtr<Podcast> podcast)
: _podcast{ getDboPtr(podcast) }
{
}
PodcastEpisode::pointer PodcastEpisode::create(Session& session, ObjectPtr<Podcast> podcast)
{
return session.getDboSession()->add(std::unique_ptr<PodcastEpisode>{ new PodcastEpisode{ podcast } });
}
std::size_t PodcastEpisode::getCount(Session& session)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<int>("SELECT COUNT(*) FROM podcast_episode"));
}
PodcastEpisode::pointer PodcastEpisode::find(Session& session, PodcastEpisodeId id)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<PodcastEpisode>>("SELECT p_e from podcast_episode p_e").where("p_e.id = ?").bind(id));
}
PodcastEpisode::pointer PodcastEpisode::findNewtestEpisode(Session& session, PodcastId podcastId)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<PodcastEpisode>>("SELECT p_e from podcast_episode p_e").where("p_e.podcast_id = ?").bind(podcastId).orderBy("p_e.pub_date DESC").limit(1));
}
void PodcastEpisode::find(Session& session, const FindParameters& params, std::function<void(const pointer&)> func)
{
session.checkReadTransaction();
auto query{ createQuery(session, params) };
utils::forEachQueryRangeResult(query, params.range, func);
}
ObjectPtr<Artwork> PodcastEpisode::getArtwork() const
{
return _artwork;
}
ArtworkId PodcastEpisode::getArtworkId() const
{
return _artwork.id();
}
void PodcastEpisode::setArtwork(ObjectPtr<Artwork> artwork)
{
_artwork = getDboPtr(artwork);
}
} // namespace lms::db
@@ -82,7 +82,7 @@ namespace lms::db
template<typename Object>
void destroy(typename Object::IdType id)
{
destroy(std::span{ &id, 1 });
destroy<Object>(std::span{ &id, 1 });
}
template<typename Object>
@@ -163,6 +163,13 @@ namespace lms::db
PositionAsc,
};
enum class PodcastEpisodeSortMode
{
None,
PubDateAsc,
PubDateDesc,
};
enum class ReleaseSortMethod
{
None,
@@ -20,6 +20,7 @@
#pragma once
#include <filesystem>
#include <variant>
#include <Wt/Dbo/Field.h>
#include <Wt/WDateTime.h>
@@ -47,10 +48,12 @@ namespace lms::db
static pointer find(Session& session, ImageId id);
// getters
TrackEmbeddedImageId getTrackEmbeddedImageId() const { return _trackEmbeddedImage.id(); }
ImageId getImageId() const { return _image.id(); }
using UnderlyingId = std::variant<std::monostate, TrackEmbeddedImageId, ImageId>;
UnderlyingId getUnderlyingId() const;
Wt::WDateTime getLastWrittenTime() const;
std::filesystem::path getAbsoluteFilePath() const;
ObjectPtr<Image> getImage() const;
ImageId getImageId() const;
template<class Action>
void persist(Action& a)
@@ -85,6 +85,7 @@ namespace lms::db
std::size_t getFileSize() const { return _fileSize; }
std::size_t getWidth() const { return _width; }
std::size_t getHeight() const { return _height; }
std::string_view getMimeType() const { return _mimeType; }
// setters
void setAbsoluteFilePath(const std::filesystem::path& p);
@@ -92,6 +93,7 @@ namespace lms::db
void setFileSize(std::size_t fileSize) { _fileSize = fileSize; }
void setWidth(std::size_t width) { _width = width; }
void setHeight(std::size_t height) { _height = height; }
void setMimeType(std::string_view mimeType) { _mimeType = mimeType; }
void setDirectory(const ObjectPtr<Directory>& directory) { _directory = getDboPtr(directory); }
template<class Action>
@@ -104,6 +106,7 @@ namespace lms::db
Wt::Dbo::field(a, _width, "width");
Wt::Dbo::field(a, _height, "height");
Wt::Dbo::field(a, _mimeType, "mime_type");
Wt::Dbo::belongsTo(a, _directory, "directory", Wt::Dbo::OnDeleteCascade);
}
@@ -119,6 +122,7 @@ namespace lms::db
int _fileSize{};
int _width{};
int _height{};
std::string _mimeType;
Wt::Dbo::ptr<Directory> _directory;
};
@@ -0,0 +1,146 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <functional>
#include <string>
#include <string_view>
#include <Wt/Dbo/Field.h>
#include <Wt/Dbo/collection.h>
#include <Wt/WDateTime.h>
#include "database/Object.hpp"
#include "database/objects/ArtworkId.hpp"
#include "database/objects/PodcastId.hpp"
namespace lms::db
{
class Artwork;
class PodcastEpisode;
class Session;
class Podcast final : public Object<Podcast, PodcastId>
{
public:
static const std::size_t maxMediaLength{ 64 };
Podcast() = default;
static std::size_t getCount(Session& session);
static pointer find(Session& session, PodcastId id);
static pointer find(Session& session, std::string_view url);
static void find(Session& session, std::function<void(const pointer&)> func);
// getters
std::string_view getUrl() const { return _url; }
bool isDeleteRequested() const { return _deleteRequested; }
std::string_view getTitle() const { return _title; }
std::string_view getLink() const { return _link; }
std::string_view getDescription() const { return _description; }
std::string_view getLanguage() const { return _language; }
std::string_view getCopyright() const { return _copyright; }
Wt::WDateTime getLastBuildDate() const { return _lastBuildDate; }
std::string_view getAuthor() const { return _author; }
std::string_view getCategory() const { return _category; }
bool isExplicit() const { return _explicit; }
std::string_view getImageUrl() const { return _imageUrl; }
std::string_view getOwnerEmail() const { return _ownerEmail; }
std::string_view getOwnerName() const { return _ownerName; }
std::string_view getSubtitle() const { return _subtitle; }
std::string_view getSummary() const { return _summary; }
ObjectPtr<Artwork> getArtwork() const;
ArtworkId getArtworkId() const;
// setters
void setUrl(std::string_view url) { _url = url; }
void setDeleteRequested(bool deleteRequested) { _deleteRequested = deleteRequested; }
void setTitle(std::string_view title) { _title = title; }
void setLink(std::string_view link) { _link = link; }
void setDescription(std::string_view description) { _description = description; }
void setLanguage(std::string_view language) { _language = language; }
void setCopyright(std::string_view copyright) { _copyright = copyright; }
void setLastBuildDate(const Wt::WDateTime& lastBuildDate) { _lastBuildDate = lastBuildDate; }
void setAuthor(std::string_view author) { _author = author; }
void setCategory(std::string_view category) { _category = category; }
void setExplicit(bool explicit_) { _explicit = explicit_; }
void setImageUrl(std::string_view imageUrl) { _imageUrl = imageUrl; }
void setOwnerEmail(std::string_view ownerEmail) { _ownerEmail = ownerEmail; }
void setOwnerName(std::string_view ownerName) { _ownerName = ownerName; }
void setSubtitle(std::string_view subtitle) { _subtitle = subtitle; }
void setSummary(std::string_view summary) { _summary = summary; }
void setArtwork(ObjectPtr<Artwork> artwork);
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _url, "url");
Wt::Dbo::field(a, _deleteRequested, "delete_requested");
Wt::Dbo::field(a, _title, "title");
Wt::Dbo::field(a, _link, "link");
Wt::Dbo::field(a, _description, "description");
Wt::Dbo::field(a, _language, "language");
Wt::Dbo::field(a, _copyright, "copyright");
Wt::Dbo::field(a, _lastBuildDate, "last_build_date");
Wt::Dbo::field(a, _author, "author");
Wt::Dbo::field(a, _category, "category");
Wt::Dbo::field(a, _explicit, "explicit");
Wt::Dbo::field(a, _imageUrl, "image_url");
Wt::Dbo::field(a, _ownerEmail, "owner_email");
Wt::Dbo::field(a, _ownerName, "owner_name");
Wt::Dbo::field(a, _subtitle, "subtitle");
Wt::Dbo::field(a, _summary, "summary");
Wt::Dbo::belongsTo(a, _artwork, "artwork", Wt::Dbo::OnDeleteSetNull);
Wt::Dbo::hasMany(a, _episodes, Wt::Dbo::ManyToOne, "podcast");
}
private:
friend class Session;
Podcast(std::string_view url);
static pointer create(Session& session, std::string_view url);
std::string _url;
bool _deleteRequested{};
std::string _title;
std::string _link;
std::string _description;
std::string _language;
std::string _copyright;
Wt::WDateTime _lastBuildDate;
// itunes fields
std::string _author;
std::string _category;
bool _explicit{};
std::string _imageUrl;
std::string _ownerEmail;
std::string _ownerName;
std::string _subtitle;
std::string _summary;
Wt::Dbo::ptr<Artwork> _artwork;
Wt::Dbo::collection<Wt::Dbo::ptr<PodcastEpisode>> _episodes;
};
} // namespace lms::db
@@ -0,0 +1,186 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <chrono>
#include <filesystem>
#include <optional>
#include <string>
#include <string_view>
#include <Wt/Dbo/Field.h>
#include <Wt/Dbo/collection.h>
#include <Wt/WDateTime.h>
#include "database/Object.hpp"
#include "database/Types.hpp"
#include "database/objects/ArtworkId.hpp"
#include "database/objects/PodcastEpisodeId.hpp"
#include "database/objects/PodcastId.hpp"
namespace lms::db
{
class Artwork;
class Podcast;
class Session;
class PodcastEpisode final : public Object<PodcastEpisode, PodcastEpisodeId>
{
public:
enum class ManualDownloadState
{
None = 0,
DownloadRequested = 1,
DeleteRequested = 3,
};
struct FindParameters
{
PodcastEpisodeSortMode sortMode = PodcastEpisodeSortMode::None;
db::PodcastId podcast; // if set, only episodes from this podcast
std::optional<Range> range;
std::optional<ManualDownloadState> manualDownloadState; // if set, only episodes that matches one of these states
FindParameters& setSortMode(PodcastEpisodeSortMode _sortMode)
{
sortMode = _sortMode;
return *this;
}
FindParameters& setPodcast(db::PodcastId _podcast)
{
podcast = _podcast;
return *this;
}
FindParameters& setRange(const std::optional<Range>& _range)
{
range = _range;
return *this;
}
FindParameters& setManualDownloadState(std::optional<ManualDownloadState> state)
{
manualDownloadState = state;
return *this;
}
};
PodcastEpisode() = default;
static std::size_t getCount(Session& session);
static pointer find(Session& session, PodcastEpisodeId id);
static pointer findNewtestEpisode(Session& session, PodcastId id);
static void find(Session& session, const FindParameters& params, std::function<void(const pointer&)> func);
// getters
ManualDownloadState getManualDownloadState() const { return _manualDownloadState; }
const std::filesystem::path& getAudioRelativeFilePath() const { return _audioRelativeFilePath; }
std::string_view getTitle() const { return _title; }
std::string_view getLink() const { return _link; }
std::string_view getDescription() const { return _description; }
std::string_view getAuthor() const { return _author; }
std::string_view getCategory() const { return _category; }
std::string_view getEnclosureUrl() const { return _enclosureUrl; }
std::string_view getEnclosureContentType() const { return _enclosureContentType; }
std::int64_t getEnclosureLength() const { return _enclosureLength; }
const Wt::WDateTime& getPubDate() const { return _pubDate; }
std::string_view getImageUrl() const { return _imageUrl; }
std::string_view getSubtitle() const { return _subtitle; }
std::string_view getSummary() const { return _summary; }
bool isExplicit() const { return _explicit; }
std::chrono::duration<int, std::milli> getDuration() const { return _duration; }
ObjectPtr<Podcast> getPodcast() const { return _podcast; }
PodcastId getPodcastId() const { return _podcast.id(); }
ObjectPtr<Artwork> getArtwork() const;
ArtworkId getArtworkId() const;
// setters
void setManualDownloadState(ManualDownloadState state) { _manualDownloadState = state; }
void setAudioRelativeFilePath(const std::filesystem::path& relativeFilePath) { _audioRelativeFilePath = relativeFilePath; }
void setTitle(std::string_view title) { _title = title; }
void setLink(std::string_view link) { _link = link; }
void setDescription(std::string_view description) { _description = description; }
void setAuthor(std::string_view author) { _author = author; }
void setCategory(std::string_view category) { _category = category; }
void setEnclosureUrl(std::string_view enclosureUrl) { _enclosureUrl = enclosureUrl; }
void setEnclosureContentType(std::string_view enclosureContentType) { _enclosureContentType = enclosureContentType; }
void setEnclosureLength(uint64_t enclosureLength) { _enclosureLength = enclosureLength; }
void setPubDate(const Wt::WDateTime& pubDate) { _pubDate = pubDate; }
void setImageUrl(std::string_view imageUrl) { _imageUrl = imageUrl; }
void setSubtitle(std::string_view subtitle) { _subtitle = subtitle; }
void setSummary(std::string_view summary) { _summary = summary; }
void setExplicit(bool explicit_) { _explicit = explicit_; }
void setDuration(std::chrono::duration<int, std::milli> duration) { _duration = duration; }
void setArtwork(ObjectPtr<Artwork> artwork);
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _manualDownloadState, "manual_download_state");
Wt::Dbo::field(a, _audioRelativeFilePath, "audio_relative_file_path");
Wt::Dbo::field(a, _title, "title");
Wt::Dbo::field(a, _link, "link");
Wt::Dbo::field(a, _description, "description");
Wt::Dbo::field(a, _author, "author");
Wt::Dbo::field(a, _category, "category");
Wt::Dbo::field(a, _enclosureUrl, "enclosure_url");
Wt::Dbo::field(a, _enclosureContentType, "enclosure_content_type");
Wt::Dbo::field(a, _enclosureLength, "enclosure_size");
Wt::Dbo::field(a, _pubDate, "pub_date");
Wt::Dbo::field(a, _imageUrl, "image_url");
Wt::Dbo::field(a, _subtitle, "subtitle");
Wt::Dbo::field(a, _summary, "summary");
Wt::Dbo::field(a, _explicit, "explicit");
Wt::Dbo::field(a, _duration, "duration");
Wt::Dbo::belongsTo(a, _artwork, "artwork", Wt::Dbo::OnDeleteSetNull);
Wt::Dbo::belongsTo(a, _podcast, "podcast", Wt::Dbo::OnDeleteCascade);
}
private:
friend class Session;
PodcastEpisode(ObjectPtr<Podcast> podcast);
static pointer create(Session& session, ObjectPtr<Podcast> podcast);
ManualDownloadState _manualDownloadState{ ManualDownloadState::None };
std::filesystem::path _audioRelativeFilePath; // relative to cache dir, only set if downloaded
std::string _url;
std::string _title;
std::string _link;
std::string _description;
std::string _author;
std::string _category;
std::string _enclosureUrl;
std::string _enclosureContentType;
int _enclosureLength{ 0 };
Wt::WDateTime _pubDate;
// itunes fields
std::string _imageUrl;
std::string _subtitle;
std::string _summary;
bool _explicit{};
std::chrono::duration<int, std::milli> _duration{ 0 };
Wt::Dbo::ptr<Artwork> _artwork;
Wt::Dbo::ptr<Podcast> _podcast;
};
} // namespace lms::db
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "database/IdType.hpp"
LMS_DECLARE_IDTYPE(PodcastEpisodeId)
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "database/IdType.hpp"
LMS_DECLARE_IDTYPE(PodcastId)
+23
View File
@@ -84,4 +84,27 @@ namespace lms::db::tests
EXPECT_EQ(artwork.get()->getAbsoluteFilePath(), "/tmp/foo");
}
}
TEST_F(DatabaseFixture, Artwork_underlyingId)
{
ScopedImage image1{ session, "/MyImage" };
ScopedArtwork artwork1{ session, image1.lockAndGet() };
ScopedTrackEmbeddedImage image2{ session };
ScopedArtwork artwork2{ session, image2.lockAndGet() };
{
auto transaction{ session.createReadTransaction() };
const auto underlyingId{ artwork1.get()->getUnderlyingId() };
ASSERT_TRUE(std::holds_alternative<db::ImageId>(underlyingId));
EXPECT_EQ(std::get<db::ImageId>(underlyingId), image1.getId());
}
{
auto transaction{ session.createReadTransaction() };
const auto underlyingId{ artwork2.get()->getUnderlyingId() };
ASSERT_TRUE(std::holds_alternative<db::TrackEmbeddedImageId>(underlyingId));
EXPECT_EQ(std::get<db::TrackEmbeddedImageId>(underlyingId), image2.getId());
}
}
} // namespace lms::db::tests
+1
View File
@@ -13,6 +13,7 @@ add_executable(test-database
Medium.cpp
Migration.cpp
PlayListFile.cpp
Podcast.cpp
RatedArtist.cpp
RatedRelease.cpp
RatedTrack.cpp
+4
View File
@@ -28,6 +28,8 @@
#include "database/objects/Medium.hpp"
#include "database/objects/PlayListFile.hpp"
#include "database/objects/PlayQueue.hpp"
#include "database/objects/Podcast.hpp"
#include "database/objects/PodcastEpisode.hpp"
#include "database/objects/RatedArtist.hpp"
#include "database/objects/RatedRelease.hpp"
#include "database/objects/RatedTrack.hpp"
@@ -359,6 +361,8 @@ VALUES
EXPECT_FALSE(Listen::find(session, ListenId{}));
EXPECT_FALSE(PlayListFile::find(session, PlayListFileId{}));
EXPECT_FALSE(PlayQueue::find(session, PlayQueueId{}));
EXPECT_FALSE(Podcast::find(session, PodcastId{}));
EXPECT_FALSE(PodcastEpisode::find(session, PodcastEpisodeId{}));
EXPECT_FALSE(RatedArtist::find(session, RatedArtistId{}));
EXPECT_FALSE(RatedRelease::find(session, RatedReleaseId{}));
EXPECT_FALSE(RatedTrack::find(session, RatedTrackId{}));
+99
View File
@@ -0,0 +1,99 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Common.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/Podcast.hpp"
namespace lms::db::tests
{
using ScopedDirectory = ScopedEntity<db::Directory>;
using ScopedPodcast = ScopedEntity<db::Podcast>;
TEST_F(DatabaseFixture, Podcast)
{
{
auto transaction{ session.createReadTransaction() };
EXPECT_EQ(Podcast::getCount(session), 0);
}
ScopedPodcast podcast{ session, "podcastUrl" };
{
auto transaction{ session.createReadTransaction() };
EXPECT_EQ(Podcast::getCount(session), 1);
Podcast::pointer p{ Podcast::find(session, podcast.getId()) };
ASSERT_NE(p, Podcast::pointer{});
EXPECT_EQ(p->getUrl(), "podcastUrl");
EXPECT_EQ(p->getTitle(), "");
EXPECT_EQ(p->getLink(), "");
EXPECT_EQ(p->getDescription(), "");
EXPECT_EQ(p->getLanguage(), "");
EXPECT_EQ(p->getCopyright(), "");
EXPECT_EQ(p->getLastBuildDate(), Wt::WDateTime());
EXPECT_EQ(p->getAuthor(), "");
EXPECT_EQ(p->getCategory(), "");
EXPECT_EQ(p->isExplicit(), false);
EXPECT_EQ(p->getImageUrl(), "");
EXPECT_EQ(p->getOwnerEmail(), "");
}
{
auto transaction{ session.createWriteTransaction() };
Podcast::pointer p{ Podcast::find(session, podcast.getId()) };
ASSERT_NE(p, Podcast::pointer{});
p.modify()->setUrl("newPodcastUrl");
p.modify()->setTitle("newTitle");
p.modify()->setLink("newLink");
p.modify()->setDescription("newDescription");
p.modify()->setLanguage("newLanguage");
p.modify()->setCopyright("newCopyright");
p.modify()->setLastBuildDate(Wt::WDateTime::currentDateTime());
p.modify()->setAuthor("newAuthor");
p.modify()->setCategory("newCategory");
p.modify()->setExplicit(true);
p.modify()->setImageUrl("newImageUrl");
p.modify()->setOwnerEmail("newOwnerEmail");
p.modify()->setOwnerName("newOwnerName");
}
{
auto transaction{ session.createReadTransaction() };
Podcast::pointer img{ Podcast::find(session, podcast.getId()) };
ASSERT_NE(img, Podcast::pointer{});
EXPECT_EQ(img->getUrl(), "newPodcastUrl");
EXPECT_EQ(img->getTitle(), "newTitle");
EXPECT_EQ(img->getLink(), "newLink");
EXPECT_EQ(img->getDescription(), "newDescription");
EXPECT_EQ(img->getLanguage(), "newLanguage");
EXPECT_EQ(img->getCopyright(), "newCopyright");
EXPECT_TRUE(img->getLastBuildDate().isValid());
EXPECT_EQ(img->getAuthor(), "newAuthor");
EXPECT_EQ(img->getCategory(), "newCategory");
EXPECT_TRUE(img->isExplicit());
EXPECT_EQ(img->getImageUrl(), "newImageUrl");
EXPECT_EQ(img->getOwnerEmail(), "newOwnerEmail");
EXPECT_EQ(img->getOwnerName(), "newOwnerName");
}
}
} // namespace lms::db::tests
+4 -4
View File
@@ -74,9 +74,9 @@ namespace lms::image
} // namespace
std::unique_ptr<IEncodedImage> readImage(const std::filesystem::path& path)
std::unique_ptr<IEncodedImage> readImage(const std::filesystem::path& path, std::string_view mimeType)
{
return std::make_unique<EncodedImage>(path);
return std::make_unique<EncodedImage>(path, mimeType);
}
std::unique_ptr<IEncodedImage> readImage(std::span<const std::byte> encodedData, std::string_view mimeType)
@@ -96,8 +96,8 @@ namespace lms::image
{
}
EncodedImage::EncodedImage(const std::filesystem::path& p)
: EncodedImage::EncodedImage{ fileToBuffer(p), extensionToMimeType(p.extension()) }
EncodedImage::EncodedImage(const std::filesystem::path& p, std::string_view mimeType)
: EncodedImage::EncodedImage{ fileToBuffer(p), mimeType.empty() ? extensionToMimeType(p.extension()) : mimeType }
{
}
} // namespace lms::image
+1 -1
View File
@@ -29,7 +29,7 @@ namespace lms::image
class EncodedImage : public IEncodedImage
{
public:
EncodedImage(const std::filesystem::path& path);
EncodedImage(const std::filesystem::path& path, std::string_view mimeType = "");
EncodedImage(std::vector<std::byte>&& data, std::string_view mimeType);
EncodedImage(std::span<const std::byte> data, std::string_view mimeType);
~EncodedImage() override = default;
+1 -1
View File
@@ -39,7 +39,7 @@ namespace lms::image
std::unique_ptr<IRawImage> decodeImage(const std::filesystem::path& path);
std::unique_ptr<IEncodedImage> readImage(std::span<const std::byte> encodedData, std::string_view mimeType);
std::unique_ptr<IEncodedImage> readImage(const std::filesystem::path& path);
std::unique_ptr<IEncodedImage> readImage(const std::filesystem::path& path, std::string_view mimeType = ""); // mimeType may already been known, otherwise, it is guessed based on the file extension
std::unique_ptr<IEncodedImage> encodeToJPEG(const IRawImage& rawImage, unsigned quality);
} // namespace lms::image
+1
View File
@@ -1,6 +1,7 @@
add_subdirectory(artwork)
add_subdirectory(auth)
add_subdirectory(feedback)
add_subdirectory(podcast)
add_subdirectory(recommendation)
add_subdirectory(scanner)
add_subdirectory(scrobbling)
@@ -63,7 +63,7 @@ namespace lms::artwork
ArtworkService::~ArtworkService() = default;
std::unique_ptr<image::IEncodedImage> ArtworkService::getFromImageFile(const std::filesystem::path& p, std::optional<image::ImageSize> width) const
std::unique_ptr<image::IEncodedImage> ArtworkService::getFromImageFile(const std::filesystem::path& p, std::string_view mimeType, std::optional<image::ImageSize> width) const
{
std::unique_ptr<image::IEncodedImage> image;
@@ -71,7 +71,7 @@ namespace lms::artwork
{
if (!width)
{
image = image::readImage(p);
image = image::readImage(p, mimeType);
}
else
{
@@ -177,8 +177,7 @@ namespace lms::artwork
if (image)
return image;
db::TrackEmbeddedImageId trackEmbeddedImageId;
db::ImageId imageId;
db::Artwork::UnderlyingId underlyingArtworkId;
{
db::Session& session{ _db.getTLSSession() };
@@ -186,16 +185,13 @@ namespace lms::artwork
db::Artwork::pointer artwork{ db::Artwork::find(session, artworkId) };
if (artwork)
{
trackEmbeddedImageId = artwork->getTrackEmbeddedImageId();
imageId = artwork->getImageId();
}
underlyingArtworkId = artwork->getUnderlyingId();
}
if (trackEmbeddedImageId.isValid())
image = getTrackEmbeddedImage(trackEmbeddedImageId, width);
else if (imageId.isValid())
image = getImage(imageId, width);
if (const auto* trackEmbeddedImageId = std::get_if<db::TrackEmbeddedImageId>(&underlyingArtworkId))
image = getTrackEmbeddedImage(*trackEmbeddedImageId, width);
else if (const auto* imageId = std::get_if<db::ImageId>(&underlyingArtworkId))
image = getImage(*imageId, width);
if (image)
_cache.addImage(cacheEntryDesc, image);
@@ -206,6 +202,7 @@ namespace lms::artwork
std::shared_ptr<image::IEncodedImage> ArtworkService::getImage(db::ImageId imageId, std::optional<image::ImageSize> width)
{
std::filesystem::path imageFile;
std::string mimeType;
{
db::Session& session{ _db.getTLSSession() };
auto transaction{ session.createReadTransaction() };
@@ -215,9 +212,10 @@ namespace lms::artwork
return nullptr;
imageFile = image->getAbsoluteFilePath();
mimeType = image->getMimeType();
}
return getFromImageFile(imageFile, width);
return getFromImageFile(imageFile, mimeType, width);
}
std::shared_ptr<image::IEncodedImage> ArtworkService::getTrackEmbeddedImage(db::TrackEmbeddedImageId trackEmbeddedImageId, std::optional<image::ImageSize> width)
@@ -62,7 +62,7 @@ namespace lms::artwork
std::shared_ptr<image::IEncodedImage> getImage(db::ImageId imageId, std::optional<image::ImageSize> width);
std::shared_ptr<image::IEncodedImage> getTrackEmbeddedImage(db::TrackEmbeddedImageId trackEmbeddedImageId, std::optional<image::ImageSize> width);
std::unique_ptr<image::IEncodedImage> getFromImageFile(const std::filesystem::path& p, std::optional<image::ImageSize> width) const;
std::unique_ptr<image::IEncodedImage> getFromImageFile(const std::filesystem::path& p, std::string_view mimeType, std::optional<image::ImageSize> width) const;
std::unique_ptr<image::IEncodedImage> getTrackImage(const std::filesystem::path& path, std::size_t index, std::optional<image::ImageSize> width) const;
db::IDb& _db;
@@ -133,7 +133,7 @@ namespace lms::feedback::listenBrainz
request.message.addBodyText(Wt::Json::serialize(root));
request.message.addHeader("Content-Type", "application/json");
request.onSuccessFunc = [this, type, starredTrackId](std::string_view /*msgBody*/) {
request.onSuccessFunc = [this, type, starredTrackId](const Wt::Http::Message&) {
boost::asio::post(boost::asio::bind_executor(_strand, [this, type, starredTrackId] {
onFeedbackSent(type, starredTrackId);
}));
@@ -321,8 +321,8 @@ namespace lms::feedback::listenBrainz
request.priority = core::http::ClientRequestParameters::Priority::Low;
request.relativeUrl = "/1/validate-token";
request.headers = { { "Authorization", "Token " + std::string{ listenBrainzToken->getAsString() } } };
request.onSuccessFunc = [this, &context](std::string_view msgBody) {
context.listenBrainzUserName = utils::parseValidateToken(msgBody);
request.onSuccessFunc = [this, &context](const Wt::Http::Message& msg) {
context.listenBrainzUserName = utils::parseValidateToken(msg.body());
if (context.listenBrainzUserName.empty())
{
onSyncEnded(context);
@@ -344,8 +344,8 @@ namespace lms::feedback::listenBrainz
core::http::ClientGETRequestParameters request;
request.relativeUrl = "/1/feedback/user/" + std::string{ context.listenBrainzUserName } + "/get-feedback?score=1&count=0";
request.priority = core::http::ClientRequestParameters::Priority::Low;
request.onSuccessFunc = [this, &context](std::string_view msgBody) {
std::string msgBodyCopy{ msgBody };
request.onSuccessFunc = [this, &context](const Wt::Http::Message& msg) {
std::string msgBodyCopy{ msg.body() };
boost::asio::post(boost::asio::bind_executor(_strand, [this, msgBodyCopy, &context] {
LOG(DEBUG, "Current feedback count = " << (context.feedbackCount ? *context.feedbackCount : 0) << " for user '" << context.listenBrainzUserName << "'");
@@ -376,8 +376,8 @@ namespace lms::feedback::listenBrainz
core::http::ClientGETRequestParameters request;
request.relativeUrl = "/1/feedback/user/" + context.listenBrainzUserName + "/get-feedback?offset=" + std::to_string(context.fetchedFeedbackCount);
request.priority = core::http::ClientRequestParameters::Priority::Low;
request.onSuccessFunc = [this, &context](std::string_view msgBody) {
std::string msgBodyCopy{ msgBody };
request.onSuccessFunc = [this, &context](const Wt::Http::Message& msg) {
std::string msgBodyCopy{ msg.body() };
boost::asio::post(boost::asio::bind_executor(_strand, [this, msgBodyCopy, &context] {
const std::size_t fetchedFeedbackCount{ processGetFeedbacks(msgBodyCopy, context) };
if (fetchedFeedbackCount == 0 // no more thing available on server
+40
View File
@@ -0,0 +1,40 @@
pkg_check_modules(PUGIXML REQUIRED IMPORTED_TARGET pugixml)
add_library(lmspodcast STATIC
impl/steps/CheckForMissingFilesStep.cpp
impl/steps/ClearTmpDirectoryStep.cpp
impl/steps/DownloadEpisodeArtworksStep.cpp
impl/steps/DownloadEpisodesStep.cpp
impl/steps/DownloadPodcastArtworksStep.cpp
impl/steps/RefreshPodcastsStep.cpp
impl/steps/RemoveEpisodesStep.cpp
impl/steps/RemovePodcastsStep.cpp
impl/steps/Utils.cpp
impl/Executor.cpp
impl/PodcastParsing.cpp
impl/PodcastService.cpp
)
target_include_directories(lmspodcast INTERFACE
include
)
target_include_directories(lmspodcast PRIVATE
include
impl
${PUGIXML_INCLUDE_DIRS}
)
target_link_libraries(lmspodcast PRIVATE
lmscore
lmsimage
PkgConfig::PUGIXML
)
target_link_libraries(lmspodcast PUBLIC
lmsdatabase
)
if(BUILD_TESTING)
add_subdirectory(test)
endif()
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "core/Exception.hpp"
namespace lms::podcast
{
class Exception : public core::LmsException
{
public:
using LmsException::LmsException;
};
} // namespace lms::podcast
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Executor.hpp"
#include <boost/asio/io_context.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/post.hpp>
namespace lms::podcast
{
Executor::Executor(boost::asio::io_context& ioContext)
: _strand{ ioContext }
{
}
void Executor::post(std::function<void()> callback)
{
boost::asio::post(boost::asio::bind_executor(_strand, std::move(callback)));
}
} // namespace lms::podcast
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <boost/asio/io_context.hpp>
#include <boost/asio/strand.hpp>
namespace lms::podcast
{
class Executor
{
public:
Executor(boost::asio::io_context& ioContext);
void post(std::function<void()> callback);
private:
boost::asio::io_context::strand _strand;
};
} // namespace lms::podcast
@@ -0,0 +1,202 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "PodcastParsing.hpp"
#include <charconv>
#include <optional>
#include <pugixml.hpp>
#include "core/ILogger.hpp"
#include "core/String.hpp"
namespace lms::podcast
{
namespace
{
std::optional<std::chrono::seconds> parseDuration(std::string_view str)
{
auto parse_int{ [](std::string_view sv) -> std::optional<int> {
int value{};
const auto [ptr, ec]{ std::from_chars(sv.data(), sv.data() + sv.size(), value) };
return (ec == std::errc()) ? std::optional{ value } : std::nullopt;
} };
std::array<int, 3> parts{ 0, 0, 0 };
int index{ 3 };
while (!str.empty() && --index >= 0)
{
const std::size_t pos{ str.rfind(':') };
const std::string_view token{ (pos == std::string_view::npos) ? str : str.substr(pos + 1) };
const auto val{ parse_int(token) };
if (!val)
return std::nullopt;
parts[index] = *val;
if (pos == std::string_view::npos)
break;
str.remove_suffix(str.size() - pos);
}
return std::chrono::hours{ parts[0] } + std::chrono::minutes{ parts[1] } + std::chrono::seconds{ parts[2] };
}
std::optional<std::chrono::seconds> getDuration(const pugi::xml_node& node, const char* tag)
{
std::optional<std::chrono::seconds> res;
if (const pugi::xml_node child{ node.child(tag) })
{
std::string_view value{ child.child_value() };
res = parseDuration(value);
}
return res;
}
std::optional<bool> getBool(const pugi::xml_node& node, const char* tag)
{
std::optional<bool> res;
if (const pugi::xml_node child{ node.child(tag) })
{
std::string_view value{ child.child_value() };
if (value == "true" || value == "1" || value == "on" || value == "yes")
res = true;
else if (value == "false" || value == "0" || value == "off" || value == "no")
res = false;
}
return res;
}
std::string_view getText(const pugi::xml_node& node, const char* tag)
{
std::string_view res;
if (const pugi::xml_node child{ node.child(tag) })
res = child.child_value();
return res;
}
std::string getRawText(const pugi::xml_node& node, const char* tag)
{
std::string res;
if (const pugi::xml_node child{ node.child(tag) })
{
std::ostringstream oss;
for (const pugi::xml_node& n : child.children())
n.print(oss, "", pugi::format_raw);
res = oss.str();
}
return res;
}
std::string_view getAttribute(const pugi::xml_node& node, const char* tag, const char* attribute)
{
std::string_view res;
if (const pugi::xml_node child{ node.child(tag) })
res = child.attribute(attribute).value();
return res;
}
} // namespace
Podcast parsePodcastRssFeed(std::string_view rssXml)
{
Podcast podcast;
pugi::xml_document doc;
pugi::xml_parse_result result{ doc.load_buffer(rssXml.data(), rssXml.size()) };
if (!result)
{
LMS_LOG(METADATA, ERROR, "Cannot read xml: " << result.description());
throw ParseException{ result.description() };
}
const pugi::xml_node channel{ doc.child("rss").child("channel") };
if (!channel)
throw ParseException{ "No <channel> element found in podcast XML" };
podcast.title = getText(channel, "title");
podcast.link = getText(channel, "link");
podcast.description = getRawText(channel, "description");
podcast.language = getText(channel, "language");
podcast.copyright = getText(channel, "copyright");
podcast.lastBuildDate = core::stringUtils::fromRFC822String(getText(channel, "lastBuildDate"));
// itunes fields
podcast.newUrl = getText(channel, "itunes:new-feed-url");
podcast.author = getText(channel, "itunes:author");
podcast.category = getAttribute(channel, "itunes:category", "text");
podcast.imageUrl = getText(channel, "itunes:image");
if (podcast.imageUrl.empty())
{
if (const pugi::xml_node image{ channel.child("image") })
podcast.imageUrl = getText(image, "url");
}
if (const pugi::xml_node owner{ channel.child("itunes:owner") })
{
podcast.ownerEmail = getText(owner, "itunes:email");
podcast.ownerName = getText(owner, "itunes:name");
}
podcast.subtitle = getText(channel, "itunes:subtitle");
podcast.summary = getRawText(channel, "itunes:summary");
podcast.explicitContent = getBool(channel, "itunes:explicit");
// parse nested episodes
for (pugi::xml_node episode{ channel.child("item") }; episode; episode = episode.next_sibling("item"))
{
PodcastEpisode e;
e.title = getText(episode, "title");
// <enclosure url="https://proxycast.radiofrance.fr/e2a713a6-aba0-4d2a-a4a1-d135d98f1f8a/13940-09.08.2025-ITEMA_24214125-2021F22805S0364-NET_MFI_F7191B05-DD5B-4AFE-BB96-3BD8ADB3240A-22.mp3" length="51842568" type="audio/mpeg"/>
if (const pugi::xml_node enclosure{ episode.child("enclosure") })
e.url = getText(enclosure, "url");
e.pubDate = core::stringUtils::fromRFC822String(getText(episode, "pubDate"));
e.description = getRawText(episode, "description");
e.link = getText(episode, "link");
e.author = getText(episode, "itunes:author");
if (e.author.empty())
e.author = getText(episode, "author");
e.enclosureUrl.url = getAttribute(episode, "enclosure", "url");
e.enclosureUrl.length = core::stringUtils::readAs<std::size_t>(getAttribute(episode, "enclosure", "length")).value_or(0);
e.enclosureUrl.type = getAttribute(episode, "enclosure", "type");
e.category = getAttribute(episode, "itunes:category", "text");
e.duration = getDuration(episode, "itunes:duration").value_or(std::chrono::seconds::zero());
e.guid = getText(episode, "guid");
e.imageUrl = getAttribute(episode, "itunes:image", "href");
e.explicitContent = getBool(episode, "itunes:explicit");
podcast.episodes.push_back(std::move(e));
}
return podcast;
}
} // namespace lms::podcast
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string_view>
#include "Exception.hpp"
#include "PodcastTypes.hpp"
namespace lms::podcast
{
class ParseException : public Exception
{
public:
using Exception::Exception;
};
Podcast parsePodcastRssFeed(std::string_view rssXml);
} // namespace lms::podcast
@@ -0,0 +1,330 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "PodcastService.hpp"
#include <filesystem>
#include "core/IConfig.hpp"
#include "core/ILogger.hpp"
#include "core/ITraceLogger.hpp"
#include "core/Service.hpp"
#include "core/http/IClient.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Podcast.hpp"
#include "database/objects/PodcastEpisode.hpp"
#include "steps/CheckForMissingFilesStep.hpp"
#include "steps/ClearTmpDirectoryStep.hpp"
#include "steps/DownloadEpisodeArtworksStep.hpp"
#include "steps/DownloadEpisodesStep.hpp"
#include "steps/DownloadPodcastArtworksStep.hpp"
#include "steps/RefreshPodcastsStep.hpp"
#include "steps/RemoveEpisodesStep.hpp"
#include "steps/RemovePodcastsStep.hpp"
#include "Exception.hpp"
namespace lms::podcast
{
std::unique_ptr<IPodcastService> createPodcastService(boost::asio::io_context& ioContext, db::IDb& db, const std::filesystem::path& cachePath)
{
return std::make_unique<PodcastService>(ioContext, db, cachePath);
}
PodcastService::PodcastService(boost::asio::io_context& ioContext, db::IDb& db, const std::filesystem::path& cachePath)
: _executor{ ioContext }
, _refreshTimer(ioContext)
, _httpClient{ core::http::createClient(ioContext, "") }
, _refreshContext{ _executor, db, *_httpClient, cachePath }
, _refreshPeriod{ core::Service<core::IConfig>::get()->getULong("podcast-refresh-period-hours", 2) }
, _refreshInProgress{ false }
, _abortRequested{ false }
, _refreshStepIndex{ 0 }
{
if (_refreshPeriod.count() < 1)
{
LMS_LOG(PODCAST, ERROR, "Podcast refresh period must be at least 1 hour");
throw Exception{ "Podcast refresh period must be at least 1 hour" };
}
setupSteps();
std::filesystem::create_directories(_refreshContext.cachePath);
std::filesystem::create_directories(_refreshContext.tmpCachePath);
LMS_LOG(PODCAST, INFO, "Starting service...");
scheduleRefresh(std::chrono::seconds{ 1 });
LMS_LOG(PODCAST, INFO, "Service started!");
}
PodcastService::~PodcastService()
{
std::unique_lock lock{ _controlMutex };
abortCurrentRefresh(lock);
LMS_LOG(PODCAST, INFO, "Service stopped!");
}
std::filesystem::path PodcastService::getCachePath() const
{
return _refreshContext.cachePath;
}
db::PodcastId PodcastService::addPodcast(std::string_view url)
{
std::unique_lock lock{ _controlMutex };
abortCurrentRefresh(lock);
db::PodcastId podcastId;
{
db::Session& session{ _refreshContext.db.getTLSSession() };
auto transaction{ session.createWriteTransaction() };
db::Podcast::pointer podcast{ db::Podcast::find(session, url) };
if (!podcast)
podcast = session.create<db::Podcast>(url);
podcastId = podcast->getId();
}
allowRefresh();
scheduleRefresh();
return podcastId;
}
bool PodcastService::removePodcast(db::PodcastId podcastId)
{
bool res{};
std::unique_lock lock{ _controlMutex };
abortCurrentRefresh(lock);
{
db::Session& session{ _refreshContext.db.getTLSSession() };
auto transaction{ session.createWriteTransaction() };
db::Podcast::pointer podcast{ db::Podcast::find(session, podcastId) };
if (podcast)
{
podcast.modify()->setDeleteRequested(true);
res = true;
}
}
allowRefresh();
scheduleRefresh();
return res;
}
void PodcastService::refreshPodcasts()
{
std::unique_lock lock{ _controlMutex };
abortCurrentRefresh(lock);
allowRefresh();
scheduleRefresh();
}
bool PodcastService::downloadPodcastEpisode(db::PodcastEpisodeId episodeId)
{
bool res{};
std::unique_lock lock{ _controlMutex };
abortCurrentRefresh(lock);
{
db::Session& session{ _refreshContext.db.getTLSSession() };
auto transaction{ session.createWriteTransaction() };
db::PodcastEpisode::pointer episode{ db::PodcastEpisode::find(session, episodeId) };
if (episode)
{
episode.modify()->setManualDownloadState(db::PodcastEpisode::ManualDownloadState::DownloadRequested);
res = true;
}
}
allowRefresh();
scheduleRefresh();
return res;
}
bool PodcastService::deletePodcastEpisode(db::PodcastEpisodeId episodeId)
{
bool res{};
std::unique_lock lock{ _controlMutex };
abortCurrentRefresh(lock);
{
db::Session& session{ _refreshContext.db.getTLSSession() };
auto transaction{ session.createWriteTransaction() };
db::PodcastEpisode::pointer episode{ db::PodcastEpisode::find(session, episodeId) };
if (episode)
{
episode.modify()->setManualDownloadState(db::PodcastEpisode::ManualDownloadState::DeleteRequested);
res = true;
}
}
allowRefresh();
scheduleRefresh();
return res;
}
bool PodcastService::hasPodcasts() const
{
db::Session& session{ _refreshContext.db.getTLSSession() };
auto transaction{ session.createReadTransaction() };
return db::Podcast::getCount(session) > 0;
}
void PodcastService::abortCurrentRefresh(std::unique_lock<std::mutex>& lock)
{
LMS_LOG(PODCAST, DEBUG, "Aborting current refresh...");
_abortRequested = true;
for (auto& step : _refreshSteps)
step->requestAbort(true);
_httpClient->abortAllRequests();
_refreshTimer.cancel();
_controlCv.wait(lock, [this] {
return !_refreshInProgress;
});
LMS_LOG(PODCAST, DEBUG, "Current refresh aborted!");
}
void PodcastService::allowRefresh()
{
assert(!_refreshInProgress);
assert(_abortRequested);
_abortRequested = false;
for (auto& step : _refreshSteps)
step->requestAbort(false);
}
void PodcastService::scheduleRefresh(std::chrono::seconds fromNow)
{
if (!hasPodcasts())
{
LMS_LOG(PODCAST, DEBUG, "No podcast: not scheduling refresh");
return;
}
LMS_LOG(PODCAST, DEBUG, "Scheduled podcast refresh in " << fromNow.count() << " seconds...");
_refreshTimer.expires_after(fromNow);
_refreshTimer.async_wait([this](const boost::system::error_code& ec) {
if (ec == boost::asio::error::operation_aborted)
return;
if (ec)
throw Exception{ "Steady timer failure: " + std::string{ ec.message() } };
_executor.post([this] { startRefresh(); });
});
}
void PodcastService::startRefresh()
{
LMS_LOG(PODCAST, DEBUG, "Starting podcast refresh");
_refreshInProgress = true;
_refreshStepIndex = 0;
runStep(_refreshStepIndex);
}
void PodcastService::setupSteps()
{
auto onDoneCallback{ [this](bool success) {
onCurrentStepDone(success);
} };
_refreshSteps.clear();
// order is important, each step is done only when the previous one is done
_refreshSteps.emplace_back(std::make_unique<ClearTmpDirectoryStep>(_refreshContext, onDoneCallback));
_refreshSteps.emplace_back(std::make_unique<CheckForMissingFilesStep>(_refreshContext, onDoneCallback));
_refreshSteps.emplace_back(std::make_unique<RefreshPodcastsStep>(_refreshContext, onDoneCallback));
_refreshSteps.emplace_back(std::make_unique<RemovePodcastsStep>(_refreshContext, onDoneCallback));
_refreshSteps.emplace_back(std::make_unique<RemoveEpisodesStep>(_refreshContext, onDoneCallback));
_refreshSteps.emplace_back(std::make_unique<DownloadPodcastArtworksStep>(_refreshContext, onDoneCallback));
_refreshSteps.emplace_back(std::make_unique<DownloadEpisodeArtworksStep>(_refreshContext, onDoneCallback));
_refreshSteps.emplace_back(std::make_unique<DownloadEpisodesStep>(_refreshContext, onDoneCallback));
}
void PodcastService::onCurrentStepDone(bool success)
{
LMS_LOG(PODCAST, DEBUG, "Step '" << _refreshSteps[_refreshStepIndex]->getName() << "' done: " << (success ? "success" : _abortRequested ? "aborted" :
"failure"));
if (success && !_abortRequested)
runNextStep();
else
onRefreshDone();
}
void PodcastService::runNextStep()
{
if (++_refreshStepIndex < _refreshSteps.size())
runStep(_refreshStepIndex);
else
onRefreshDone();
}
void PodcastService::runStep(std::size_t stepIndex)
{
_refreshContext.executor.post([stepIndex, this] {
assert(stepIndex < _refreshSteps.size());
RefreshStep& step{ *_refreshSteps[stepIndex] };
LMS_LOG(PODCAST, DEBUG, "Running step '" << step.getName() << "'");
{
LMS_SCOPED_TRACE_OVERVIEW("Podcast", step.getName());
step.run();
}
});
}
void PodcastService::onRefreshDone()
{
LMS_LOG(PODCAST, DEBUG, "Refresh done" << (_abortRequested ? " (aborted)" : ""));
const bool rescheduleRefresh{ !_abortRequested };
_refreshInProgress = false;
_controlCv.notify_all();
if (rescheduleRefresh)
scheduleRefresh(_refreshPeriod);
}
} // namespace lms::podcast
@@ -0,0 +1,91 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <boost/asio/steady_timer.hpp>
#include "database/objects/PodcastId.hpp"
#include "services/podcast/IPodcastService.hpp"
#include "Executor.hpp"
#include "RefreshContext.hpp"
namespace lms::core::http
{
class IClient;
}
namespace lms::podcast
{
class RefreshStep;
class PodcastService : public IPodcastService
{
public:
PodcastService(boost::asio::io_context& ioContext, db::IDb& db, const std::filesystem::path& cachePath);
~PodcastService() override;
PodcastService(const PodcastService&) = delete;
PodcastService& operator=(const PodcastService&) = delete;
private:
std::filesystem::path getCachePath() const override;
db::PodcastId addPodcast(std::string_view url) override;
bool removePodcast(db::PodcastId podcast) override;
void refreshPodcasts() override;
bool downloadPodcastEpisode(db::PodcastEpisodeId episode) override;
bool deletePodcastEpisode(db::PodcastEpisodeId episode) override;
bool hasPodcasts() const;
void abortCurrentRefresh(std::unique_lock<std::mutex>& lock);
void allowRefresh();
void scheduleRefresh(std::chrono::seconds fromNow = std::chrono::seconds::zero());
void startRefresh();
void onRefreshDone();
void setupSteps();
void onCurrentStepDone(bool success);
void runNextStep();
void runStep(std::size_t stepIndex);
Executor _executor;
boost::asio::steady_timer _refreshTimer;
std::unique_ptr<core::http::IClient> _httpClient;
RefreshContext _refreshContext;
const std::chrono::hours _refreshPeriod;
std::mutex _controlMutex;
std::condition_variable _controlCv;
std::atomic<bool> _refreshInProgress;
std::atomic<bool> _abortRequested;
std::vector<std::unique_ptr<RefreshStep>> _refreshSteps;
std::size_t _refreshStepIndex;
};
} // namespace lms::podcast
@@ -0,0 +1,78 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <chrono>
#include <optional>
#include <string>
#include <vector>
#include <Wt/WDateTime.h>
namespace lms::podcast
{
struct EnclosureUrl
{
std::string url;
std::size_t length;
std::string type;
};
struct PodcastEpisode
{
std::string url;
std::string title;
std::string link;
std::string description;
Wt::WDateTime pubDate;
std::string author;
std::string category;
std::optional<bool> explicitContent;
std::string imageUrl;
std::string ownerEmail;
std::string guid;
EnclosureUrl enclosureUrl;
std::chrono::milliseconds duration{ 0 };
};
struct Podcast
{
std::string title;
std::string link;
std::string description;
std::string language;
std::string copyright;
Wt::WDateTime lastBuildDate;
// itunes fields
std::string newUrl;
std::string author;
std::string category;
std::optional<bool> explicitContent;
std::string imageUrl;
std::string ownerEmail;
std::string ownerName;
std::string subtitle;
std::string summary;
std::vector<PodcastEpisode> episodes; // List of episodes in the podcast
};
} // namespace lms::podcast
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <filesystem>
namespace lms
{
namespace db
{
class IDb;
}
namespace core::http
{
class IClient;
}
} // namespace lms
namespace lms::podcast
{
class Executor;
struct RefreshContext
{
RefreshContext(Executor& executor, db::IDb& db, core::http::IClient& client, const std::filesystem::path& cachePath)
: executor{ executor }
, client{ client }
, db{ db }
, cachePath{ cachePath }
, tmpCachePath{ cachePath / "tmp" }
{
}
~RefreshContext() = default;
RefreshContext(const RefreshContext&) = delete;
RefreshContext& operator=(const RefreshContext&) = delete;
Executor& executor;
core::http::IClient& client;
db::IDb& db;
const std::filesystem::path cachePath;
const std::filesystem::path tmpCachePath;
};
} // namespace lms::podcast
@@ -0,0 +1,144 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CheckForMissingFilesStep.hpp"
#include <vector>
#include "core/ILogger.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/Image.hpp"
#include "database/objects/Podcast.hpp"
#include "database/objects/PodcastEpisode.hpp"
namespace lms::podcast
{
namespace
{
bool fileExists(const std::string& path)
{
std::error_code ec;
bool res{ std::filesystem::exists(path, ec) };
if (ec)
{
LMS_LOG(PODCAST, ERROR, "Error checking file existence for path " << path << ": " << ec.message());
return false;
}
return res;
}
bool checkArtworkFile(const db::Artwork::pointer& artwork)
{
assert(std::holds_alternative<db::ImageId>(artwork->getUnderlyingId())); // these artworks can only be an image
const std::filesystem::path filePath{ artwork->getAbsoluteFilePath() };
if (!fileExists(filePath.string()))
{
LMS_LOG(PODCAST, DEBUG, "Artwork file is missing: " << filePath);
return false;
}
return true;
}
} // namespace
core::LiteralString CheckForMissingFilesStep::getName() const
{
return "Check for missing files";
}
void CheckForMissingFilesStep::run()
{
checkMissingImages();
checkMissingEpisodes();
onDone();
}
void CheckForMissingFilesStep::checkMissingImages()
{
std::vector<db::ImageId> missingImages;
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
db::Podcast::find(session, [&](const db::Podcast::pointer& podcast) {
if (const db::Artwork::pointer artwork{ podcast->getArtwork() })
{
if (!checkArtworkFile(artwork))
missingImages.push_back(artwork->getImageId());
}
});
db::PodcastEpisode::find(session, db::PodcastEpisode::FindParameters{}, [&](const db::PodcastEpisode::pointer& episode) {
if (const db::Artwork::pointer artwork{ episode->getArtwork() })
{
if (!checkArtworkFile(artwork))
missingImages.push_back(artwork->getImageId());
}
});
}
if (!missingImages.empty())
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createWriteTransaction() };
session.destroy<db::Image>(missingImages); // will propagate to artworks and podcasts/episodes
}
}
void CheckForMissingFilesStep::checkMissingEpisodes()
{
std::vector<db::PodcastEpisodeId> missingEpisodes;
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
db::PodcastEpisode::find(session, db::PodcastEpisode::FindParameters{}, [&](const db::PodcastEpisode::pointer& episode) {
if (episode->getAudioRelativeFilePath().empty())
return;
const std::filesystem::path filePath{ getCachePath() / episode->getAudioRelativeFilePath() };
if (!fileExists(filePath))
{
LMS_LOG(PODCAST, INFO, "Episode file " << filePath << " is missing for episode '" << episode->getTitle() << "'");
missingEpisodes.push_back(episode->getId());
}
});
}
if (!missingEpisodes.empty())
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createWriteTransaction() };
for (const auto& episodeId : missingEpisodes)
{
db::PodcastEpisode::pointer episode{ db::PodcastEpisode::find(session, episodeId) };
episode.modify()->setAudioRelativeFilePath({});
}
}
}
} // namespace lms::podcast
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "RefreshStep.hpp"
namespace lms::podcast
{
class CheckForMissingFilesStep : public RefreshStep
{
using RefreshStep::RefreshStep;
private:
core::LiteralString getName() const override;
void run() override;
void checkMissingImages();
void checkMissingEpisodes();
};
} // namespace lms::podcast
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ClearTmpDirectoryStep.hpp"
#include "core/ILogger.hpp"
namespace lms::podcast
{
namespace
{
bool clearDirectory(const std::filesystem::path& _rootPath)
{
for (const auto& entry : std::filesystem::directory_iterator{ _rootPath })
{
std::error_code ec;
std::filesystem::remove_all(entry, ec);
if (ec)
{
LMS_LOG(PODCAST, ERROR, "Failed to remove " << entry << ": " << ec.message());
return false;
}
}
return true;
}
} // namespace
core::LiteralString ClearTmpDirectoryStep::getName() const
{
return "Clear tmp Directory";
}
void ClearTmpDirectoryStep::run()
{
if (!clearDirectory(getTmpCachePath()))
{
LMS_LOG(PODCAST, ERROR, "Failed to delete tmp directory " << getTmpCachePath() << ": aborting refresh");
onAbort();
return;
}
onDone();
}
} // namespace lms::podcast
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "RefreshStep.hpp"
namespace lms::podcast
{
class ClearTmpDirectoryStep : public RefreshStep
{
public:
using RefreshStep::RefreshStep;
private:
core::LiteralString getName() const override;
void run() override;
};
} // namespace lms::podcast
@@ -0,0 +1,154 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DownloadEpisodeArtworksStep.hpp"
#include <filesystem>
#include <fstream>
#include <system_error>
#include "core/ILogger.hpp"
#include "core/http/IClient.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/Image.hpp"
#include "database/objects/PodcastEpisode.hpp"
#include "Executor.hpp"
#include "Utils.hpp"
namespace lms::podcast
{
namespace
{
void createEpisodeArtwork(db::Session& session, db::PodcastEpisodeId episodeId, const std::filesystem::path& filePath, std::string_view contentType)
{
auto transaction{ session.createWriteTransaction() };
db::PodcastEpisode::pointer episode{ db::PodcastEpisode::find(session, episodeId) };
if (!episode)
return;
if (db::Artwork::pointer artwork{ utils::createArtworkFromImage(session, filePath, contentType) })
episode.modify()->setArtwork(artwork);
}
} // namespace
core::LiteralString DownloadEpisodeArtworksStep::getName() const
{
return "Download episode artworks";
}
void DownloadEpisodeArtworksStep::run()
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
_episodeArtworksToDownload.clear();
db::PodcastEpisode::find(session, db::PodcastEpisode::FindParameters{}, [&](const db::PodcastEpisode::pointer& episode) {
if (episode->getImageUrl().empty())
return;
if (episode->getArtworkId().isValid())
return;
_episodeArtworksToDownload.push_back(episode->getId());
});
processNext();
}
void DownloadEpisodeArtworksStep::processNext()
{
if (abortRequested())
{
onAbort();
return;
}
getExecutor().post([this] {
if (_episodeArtworksToDownload.empty())
{
onDone();
return;
}
const db::PodcastEpisodeId podcastEpisodeId{ _episodeArtworksToDownload.front() };
_episodeArtworksToDownload.pop_front();
process(podcastEpisodeId);
});
}
void DownloadEpisodeArtworksStep::process(db::PodcastEpisodeId episodeId)
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
const auto episode{ db::PodcastEpisode::find(getDb().getTLSSession(), episodeId) };
if (!episode)
{
LMS_LOG(PODCAST, DEBUG, "Cannot find episode: removed?");
processNext();
return;
}
const std::string url{ episode->getImageUrl() };
const std::filesystem::path finalFilePath{ getCachePath() / utils::generateRandomFileName() };
core::http::ClientGETRequestParameters params;
params.relativeUrl = episode->getImageUrl();
params.onFailureFunc = [this, episode] {
LMS_LOG(PODCAST, ERROR, "Failed to download episode image from '" << episode->getImageUrl() << "'");
processNext();
};
params.onSuccessFunc = [=, this](const Wt::Http::Message& msg) {
const std::string body{ msg.body() }; // API enforces a copy here
std::ofstream file{ finalFilePath, std::ios::binary | std::ios::trunc };
if (!file)
{
std::error_code ec{ errno, std::generic_category() };
LMS_LOG(PODCAST, ERROR, "Failed to open file " << finalFilePath << " for writing: " << ec.message());
processNext();
return;
}
file.write(body.data(), body.size());
if (!file)
{
std::error_code ec{ errno, std::generic_category() };
LMS_LOG(PODCAST, ERROR, "Failed to write to file " << finalFilePath << ": " << ec.message());
processNext();
return;
}
const std::string* contentType{ msg.getHeader("Content-Type") };
LMS_LOG(PODCAST, INFO, "Downloaded episode artwork for episode '" << episode->getTitle() << "' to " << finalFilePath << " with content type '" << (contentType ? *contentType : "unknown") << "', size = " << body.size() << " bytes");
createEpisodeArtwork(getDb().getTLSSession(), episodeId, finalFilePath, contentType ? *contentType : "application/octet-stream");
processNext();
};
params.onAbortFunc = [this] {
onAbort();
};
getClient().sendGETRequest(std::move(params));
}
} // namespace lms::podcast
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <deque>
#include "database/objects/PodcastEpisodeId.hpp"
#include "RefreshStep.hpp"
namespace lms::podcast
{
class DownloadEpisodeArtworksStep : public RefreshStep
{
using RefreshStep::RefreshStep;
private:
core::LiteralString getName() const override;
void run() override;
void processNext();
void process(db::PodcastEpisodeId episodeId);
std::deque<db::PodcastEpisodeId> _episodeArtworksToDownload;
};
} // namespace lms::podcast
@@ -0,0 +1,196 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DownloadEpisodesStep.hpp"
#include <fstream>
#include "core/IConfig.hpp"
#include "core/ILogger.hpp"
#include "core/Service.hpp"
#include "core/http/IClient.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Podcast.hpp"
#include "database/objects/PodcastEpisode.hpp"
#include "Executor.hpp"
#include "Utils.hpp"
namespace lms::podcast
{
namespace
{
void updateEpisode(db::Session& session, db::PodcastEpisodeId episodeId, const std::filesystem::path& relativeFilePath)
{
auto transaction{ session.createWriteTransaction() };
db::PodcastEpisode::pointer dbEpisode{ db::PodcastEpisode::find(session, episodeId) };
if (!dbEpisode)
return; // may have been deleted by admin
dbEpisode.modify()->setAudioRelativeFilePath(relativeFilePath);
}
} // namespace
DownloadEpisodesStep::DownloadEpisodesStep(RefreshContext& context, OnDoneCallback callback)
: RefreshStep{ context, std::move(callback) }
, _autoDownloadEpisodes{ core::Service<core::IConfig>::get()->getBool("podcast-auto-download-episodes", true) }
, _autoDownloadEpisodesMaxAge{ core::Service<core::IConfig>::get()->getULong("podcast-auto-download-episodes-max-age-days", 30) }
{
}
core::LiteralString DownloadEpisodesStep::getName() const
{
return "Download episodes";
}
void DownloadEpisodesStep::run()
{
collectEpisodes();
processNext();
}
void DownloadEpisodesStep::collectEpisodes()
{
const Wt::WDateTime now{ Wt::WDateTime::currentDateTime() };
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
db::PodcastEpisode::FindParameters params;
_episodesToDownload.clear();
db::PodcastEpisode::find(session, params, [&](const db::PodcastEpisode::pointer& episode) {
if (!episode->getAudioRelativeFilePath().empty())
return; // already downloaded
switch (episode->getManualDownloadState())
{
case db::PodcastEpisode::ManualDownloadState::DownloadRequested:
LMS_LOG(PODCAST, DEBUG, "Adding episode '" << episode->getTitle() << "' from podcast '" << episode->getPodcast()->getTitle() << "' to download queue (manually requested)");
_episodesToDownload.push_back(episode->getId());
break;
case db::PodcastEpisode::ManualDownloadState::None:
if (_autoDownloadEpisodes && now < episode->getPubDate().addDays(_autoDownloadEpisodesMaxAge.count()))
{
LMS_LOG(PODCAST, DEBUG, "Adding episode '" << episode->getTitle() << "' from podcast '" << episode->getPodcast()->getTitle() << "' to download queue (auto-download enabled)");
_episodesToDownload.push_back(episode->getId());
}
break;
case db::PodcastEpisode::ManualDownloadState::DeleteRequested:
break;
}
});
}
void DownloadEpisodesStep::processNext()
{
getExecutor().post([this] {
if (_episodesToDownload.empty())
{
LMS_LOG(PODCAST, DEBUG, "All pending episodes downloaded");
onDone();
return;
}
const db::PodcastEpisodeId podcastEpisodeId{ _episodesToDownload.front() };
_episodesToDownload.pop_front();
process(podcastEpisodeId);
});
}
void DownloadEpisodesStep::process(db::PodcastEpisodeId episodeId)
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
const db::PodcastEpisode::pointer episode{ db::PodcastEpisode::find(session, episodeId) };
if (!episode)
{
LMS_LOG(PODCAST, DEBUG, "Cannot find episode: removed?"); // TODO if removed, need to keep it in the db to check for new episodes...
processNext();
return;
}
const std::string randomName{ utils::generateRandomFileName() };
const std::filesystem::path tmpFilePath{ getTmpCachePath() / randomName };
const std::filesystem::path finalFilePath{ getCachePath() / randomName };
LMS_LOG(PODCAST, DEBUG, "Downloading episode '" << episode->getTitle() << "' from '" << episode->getEnclosureUrl() << "' in tmp file '" << tmpFilePath << "'");
core::http::ClientGETRequestParameters params;
const std::string url{ episode->getEnclosureUrl() };
params.relativeUrl = url;
params.onFailureFunc = [this, episode] {
LMS_LOG(PODCAST, ERROR, "Failed to download podcast episode from '" << episode->getEnclosureUrl() << "'");
processNext();
};
params.onChunkReceived = [url, tmpFilePath](std::span<const std::byte> chunk) {
std::ofstream file{ tmpFilePath, std::ios::binary | std::ios::app };
if (!file)
{
std::error_code ec{ errno, std::generic_category() };
LMS_LOG(PODCAST, ERROR, "Failed to open file '" << tmpFilePath << "' for writing: " << ec.message());
return core::http::ClientGETRequestParameters::ChunckReceivedResult::Abort;
}
// check write status
file.write(reinterpret_cast<const char*>(chunk.data()), chunk.size());
if (!file)
{
std::error_code ec{ errno, std::generic_category() };
LMS_LOG(PODCAST, ERROR, "Failed to write to file '" << tmpFilePath << "': " << ec.message());
return core::http::ClientGETRequestParameters::ChunckReceivedResult::Abort;
}
return core::http::ClientGETRequestParameters::ChunckReceivedResult::Continue;
};
params.onSuccessFunc = [=, this]([[maybe_unused]] const Wt::Http::Message& msg) {
assert(msg.body().empty());
getExecutor().post([=, this] {
LMS_LOG(PODCAST, DEBUG, "Download episode from '" << url << "' complete");
LMS_LOG(PODCAST, DEBUG, "Renaming temp file " << tmpFilePath << " to " << finalFilePath);
std::error_code ec;
std::filesystem::rename(tmpFilePath, finalFilePath, ec);
if (ec)
LMS_LOG(PODCAST, ERROR, "Failed to rename temp file " << tmpFilePath << " to " << finalFilePath << ": " << ec.message());
else
updateEpisode(getDb().getTLSSession(), episodeId, randomName);
// TODO: now the file is complete, should we attempt to read it and get the real information like duration and size?
LMS_LOG(PODCAST, INFO, "Successfully downloaded episode '" << episode->getTitle() << "'");
processNext();
});
};
params.onAbortFunc = [this] {
onAbort();
};
LMS_LOG(PODCAST, DEBUG, "Downloading episode from '" << url << "'...");
getClient().sendGETRequest(std::move(params));
}
} // namespace lms::podcast
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <deque>
#include "database/objects/PodcastEpisodeId.hpp"
#include "RefreshStep.hpp"
namespace lms::podcast
{
class DownloadEpisodesStep : public RefreshStep
{
public:
DownloadEpisodesStep(RefreshContext& context, OnDoneCallback callback);
private:
core::LiteralString getName() const override;
void run() override;
void collectEpisodes();
void processNext();
void process(db::PodcastEpisodeId episodeId);
const bool _autoDownloadEpisodes;
const std::chrono::days _autoDownloadEpisodesMaxAge;
std::deque<db::PodcastEpisodeId> _episodesToDownload;
};
} // namespace lms::podcast
@@ -0,0 +1,153 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DownloadPodcastArtworksStep.hpp"
#include <filesystem>
#include <fstream>
#include <system_error>
#include "core/ILogger.hpp"
#include "core/http/IClient.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/Podcast.hpp"
#include "Executor.hpp"
#include "Utils.hpp"
namespace lms::podcast
{
namespace
{
void createPodcastArtwork(db::Session& session, db::PodcastId podcastId, const std::filesystem::path& filePath, std::string_view contentType)
{
auto transaction{ session.createWriteTransaction() };
db::Podcast::pointer dbPodcast{ db::Podcast::find(session, podcastId) };
if (!dbPodcast)
return; // may have been deleted by admin
if (db::Artwork::pointer artwork{ utils::createArtworkFromImage(session, filePath, contentType) })
dbPodcast.modify()->setArtwork(artwork);
}
} // namespace
core::LiteralString DownloadPodcastArtworksStep::getName() const
{
return "Download podcast artworks";
}
void DownloadPodcastArtworksStep::run()
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
_podcastArtworksToDownload.clear();
db::Podcast::find(session, [&](const db::Podcast::pointer& podcast) {
if (podcast->getImageUrl().empty() || podcast->getTitle().empty())
return;
if (podcast->getArtworkId().isValid())
return;
_podcastArtworksToDownload.push_back(podcast->getId());
});
processNext();
}
void DownloadPodcastArtworksStep::processNext()
{
if (abortRequested())
{
onAbort();
return;
}
getExecutor().post([this] {
if (_podcastArtworksToDownload.empty())
{
onDone();
return;
}
const db::PodcastId podcastId{ _podcastArtworksToDownload.front() };
_podcastArtworksToDownload.pop_front();
process(podcastId);
});
}
void DownloadPodcastArtworksStep::process(db::PodcastId podcastId)
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
const auto podcast{ db::Podcast::find(getDb().getTLSSession(), podcastId) };
if (!podcast)
{
LMS_LOG(PODCAST, DEBUG, "Cannot find podcast: removed?");
processNext();
return;
}
const std::string url{ podcast->getImageUrl() };
const std::filesystem::path finalFilePath{ getCachePath() / utils::generateRandomFileName() };
core::http::ClientGETRequestParameters params;
params.relativeUrl = podcast->getImageUrl();
params.onFailureFunc = [this, podcast] {
LMS_LOG(PODCAST, ERROR, "Failed to download podcast image from '" << podcast->getImageUrl() << "'");
processNext();
};
params.onSuccessFunc = [=, this](const Wt::Http::Message& msg) {
const std::string body{ msg.body() }; // API enforces a copy here
std::ofstream file{ finalFilePath, std::ios::binary | std::ios::app };
if (!file)
{
std::error_code ec{ errno, std::generic_category() };
LMS_LOG(PODCAST, ERROR, "Failed to open file " << finalFilePath << " for writing: " << ec.message());
processNext();
return;
}
file.write(body.data(), body.size());
if (!file)
{
std::error_code ec{ errno, std::generic_category() };
LMS_LOG(PODCAST, ERROR, "Failed to write to file " << finalFilePath << ": " << ec.message());
processNext();
return;
}
const std::string* contentType{ msg.getHeader("Content-Type") };
LMS_LOG(PODCAST, INFO, "Downloaded podcast artwork for podcast '" << podcast->getTitle() << "' to " << finalFilePath << " with content type '" << (contentType ? *contentType : "unknown") << "', size = " << body.size());
createPodcastArtwork(getDb().getTLSSession(), podcastId, finalFilePath, contentType ? *contentType : "application/octet-stream");
processNext();
};
params.onAbortFunc = [this] {
onAbort();
};
getClient().sendGETRequest(std::move(params));
}
} // namespace lms::podcast
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <deque>
#include "database/objects/PodcastId.hpp"
#include "RefreshStep.hpp"
namespace lms::podcast
{
class DownloadPodcastArtworksStep : public RefreshStep
{
using RefreshStep::RefreshStep;
private:
core::LiteralString getName() const override;
void run() override;
void processNext();
void process(db::PodcastId podcastId);
std::deque<db::PodcastId> _podcastArtworksToDownload;
};
} // namespace lms::podcast
@@ -0,0 +1,204 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "RefreshPodcastsStep.hpp"
#include "core/ILogger.hpp"
#include "core/http/IClient.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/Image.hpp"
#include "database/objects/Podcast.hpp"
#include "database/objects/PodcastEpisode.hpp"
#include "Executor.hpp"
#include "PodcastParsing.hpp"
#include "PodcastTypes.hpp"
namespace lms::podcast
{
namespace
{
void removeArtwork(db::Session& session, const db::Artwork::pointer& artwork)
{
const auto underlyingImageId{ artwork->getUnderlyingId() };
const auto* imageId{ std::get_if<db::ImageId>(&underlyingImageId) };
assert(imageId); // these artworks can only be an image
std::error_code ec;
std::filesystem::remove(artwork->getAbsoluteFilePath(), ec);
if (ec)
LMS_LOG(PODCAST, WARNING, "Failed to remove old podcast artwork file '" << artwork->getAbsoluteFilePath() << "': " << ec.message());
session.destroy<db::Image>(*imageId);
}
void updatePodcast(db::Session& session, db::PodcastId podcastId, const Podcast& podcast)
{
auto transaction{ session.createWriteTransaction() };
db::Podcast::pointer dbPodcast{ db::Podcast::find(session, podcastId) };
if (!dbPodcast)
return; // may have been deleted by admin
LMS_LOG(PODCAST, DEBUG, "Refreshing podcast '" << podcast.title << "' received from '" << dbPodcast->getUrl() << "'");
// force update the podcast data
if (!podcast.newUrl.empty() && podcast.newUrl != dbPodcast->getUrl())
{
LMS_LOG(PODCAST, INFO, "Podcast '" << podcast.title << "' : URL changed from '" << dbPodcast->getUrl() << "' to '" << podcast.newUrl << "'");
dbPodcast.modify()->setUrl(podcast.newUrl);
}
dbPodcast.modify()->setAuthor(podcast.author);
dbPodcast.modify()->setCategory(podcast.category);
dbPodcast.modify()->setCopyright(podcast.copyright);
dbPodcast.modify()->setDescription(podcast.description);
dbPodcast.modify()->setExplicit(podcast.explicitContent ? *podcast.explicitContent : false);
dbPodcast.modify()->setLanguage(podcast.language);
dbPodcast.modify()->setLastBuildDate(podcast.lastBuildDate);
dbPodcast.modify()->setLink(podcast.link);
dbPodcast.modify()->setOwnerEmail(podcast.ownerEmail);
dbPodcast.modify()->setOwnerName(podcast.ownerName);
dbPodcast.modify()->setSubtitle(podcast.subtitle);
dbPodcast.modify()->setSummary(podcast.summary);
dbPodcast.modify()->setTitle(podcast.title);
if (dbPodcast->getImageUrl() != podcast.imageUrl)
{
LMS_LOG(PODCAST, INFO, "Podcast '" << podcast.title << "' : image url changed from '" << dbPodcast->getImageUrl() << "' to '" << podcast.imageUrl << "'");
if (db::Artwork::pointer currentArtwork{ dbPodcast->getArtwork() })
removeArtwork(session, currentArtwork);
dbPodcast.modify()->setImageUrl(podcast.imageUrl);
}
// Only create episodes if they are new, do not modify/update existing entries for now
// TODO: update existing episodes, remove artwork if url changed
Wt::WDateTime previousNewestEpisodeDateTime{};
if (db::PodcastEpisode::pointer dbEpisode{ db::PodcastEpisode::findNewtestEpisode(session, podcastId) })
previousNewestEpisodeDateTime = dbEpisode->getPubDate();
// TODO: mark for deletion old episodes that are no longer referenced!!
for (const auto& episode : podcast.episodes)
{
if (previousNewestEpisodeDateTime.isValid() && episode.pubDate <= previousNewestEpisodeDateTime)
continue; // consider already in db
LMS_LOG(PODCAST, DEBUG, "Adding episode '" << episode.title << "' to podcast '" << podcast.title << "'");
auto dbEpisode{ session.create<db::PodcastEpisode>(dbPodcast) };
dbEpisode.modify()->setAuthor(episode.author);
dbEpisode.modify()->setCategory(episode.category);
dbEpisode.modify()->setDescription(episode.description);
dbEpisode.modify()->setEnclosureUrl(episode.enclosureUrl.url);
dbEpisode.modify()->setEnclosureContentType(episode.enclosureUrl.type);
dbEpisode.modify()->setEnclosureLength(episode.enclosureUrl.length);
dbEpisode.modify()->setExplicit(episode.explicitContent ? *episode.explicitContent : false);
dbEpisode.modify()->setLink(episode.link);
dbEpisode.modify()->setPubDate(episode.pubDate);
dbEpisode.modify()->setTitle(episode.title);
dbEpisode.modify()->setImageUrl(episode.imageUrl);
dbEpisode.modify()->setDuration(episode.duration);
}
}
} // namespace
core::LiteralString RefreshPodcastsStep::getName() const
{
return "Refresh podcasts";
}
void RefreshPodcastsStep::run()
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
db::Podcast::find(session, [this](const db::Podcast::pointer& podcast) {
LMS_LOG(PODCAST, DEBUG, "Found podcast to refresh at '" << podcast->getUrl() << "'");
podcastsToRefresh.push(podcast->getId());
});
refreshNextPodcast();
}
void RefreshPodcastsStep::refreshNextPodcast()
{
if (abortRequested())
{
onAbort();
return;
}
getExecutor().post([this] {
if (podcastsToRefresh.empty())
{
LMS_LOG(PODCAST, DEBUG, "All podcasts refreshed");
onDone();
return;
}
const db::PodcastId podcastId{ podcastsToRefresh.front() };
podcastsToRefresh.pop();
refreshPodcast(podcastId);
});
}
void RefreshPodcastsStep::refreshPodcast(db::PodcastId podcastId)
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
const db::Podcast::pointer podcast{ db::Podcast::find(session, podcastId) };
if (!podcast)
{
refreshNextPodcast(); // maybe removed in the meantime by admin
return;
}
LMS_LOG(PODCAST, DEBUG, "Syncing podcast from '" << podcast->getUrl() << "'");
const std::string url{ podcast->getUrl() };
core::http::ClientGETRequestParameters params;
params.relativeUrl = podcast->getUrl();
params.onFailureFunc = [this, podcast] {
LMS_LOG(PODCAST, ERROR, "Failed to sync podcast from '" << podcast->getUrl() << "'");
refreshNextPodcast();
};
params.onSuccessFunc = [this, podcast, podcastId](const Wt::Http::Message& msg) {
getExecutor().post([this, podcast, podcastId, msgBody = msg.body()] {
try
{
const auto podcast{ parsePodcastRssFeed(msgBody) };
updatePodcast(getDb().getTLSSession(), podcastId, podcast);
}
catch (const ParseException& e)
{
LMS_LOG(PODCAST, ERROR, "Failed to parse rss feed from '" << podcast->getUrl() << "': " << e.what());
}
refreshNextPodcast();
});
};
params.onAbortFunc = [this] {
onAbort();
};
getClient().sendGETRequest(std::move(params));
}
} // namespace lms::podcast
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <queue>
#include "database/objects/PodcastId.hpp"
#include "RefreshStep.hpp"
namespace lms::podcast
{
class RefreshPodcastsStep : public RefreshStep
{
using RefreshStep::RefreshStep;
private:
core::LiteralString getName() const override;
void run() override;
void refreshNextPodcast();
void refreshPodcast(db::PodcastId podcastId);
std::queue<db::PodcastId> podcastsToRefresh;
};
} // namespace lms::podcast
@@ -0,0 +1,99 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <atomic>
#include <filesystem>
#include <functional>
#include "core/LiteralString.hpp"
#include "RefreshContext.hpp"
namespace lms::podcast
{
class RefreshStep
{
public:
using OnDoneCallback = std::function<void(bool success)>;
RefreshStep(RefreshContext& context, OnDoneCallback callback)
: _context{ context }
, _onDoneCallback{ std::move(callback) } {}
virtual ~RefreshStep() = default;
virtual core::LiteralString getName() const = 0;
virtual void run() = 0;
void requestAbort(bool value)
{
_abortRequested = value;
}
protected:
bool abortRequested() const
{
return _abortRequested;
}
// Called by the step implementation when done
void onDone()
{
_onDoneCallback(true);
}
// Called by the step implementation when it wants to abort the whole refresh process
void onAbort()
{
_onDoneCallback(false);
}
Executor& getExecutor()
{
return _context.executor;
}
db::IDb& getDb()
{
return _context.db;
}
const std::filesystem::path& getCachePath() const
{
return _context.cachePath;
}
const std::filesystem::path& getTmpCachePath() const
{
return _context.tmpCachePath;
}
core::http::IClient& getClient()
{
return _context.client;
}
private:
RefreshContext& _context;
OnDoneCallback _onDoneCallback;
std::atomic<bool> _abortRequested;
};
} // namespace lms::podcast
@@ -0,0 +1,108 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "RemoveEpisodesStep.hpp"
#include <filesystem>
#include <vector>
#include "core/IConfig.hpp"
#include "core/ILogger.hpp"
#include "core/Service.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/PodcastEpisode.hpp"
#include "Utils.hpp"
namespace lms::podcast
{
core::LiteralString RemoveEpisodesStep::getName() const
{
return "Remove podcast episodes";
}
RemoveEpisodesStep::RemoveEpisodesStep(RefreshContext& context, OnDoneCallback callback)
: RefreshStep{ context, std::move(callback) }
, _autoDownloadEpisodesMaxAge{ core::Service<core::IConfig>::get()->getULong("podcast-auto-download-episodes-max-age-days", 30) }
{
}
void RemoveEpisodesStep::run()
{
std::vector<db::PodcastEpisodeId> episodesToRemove;
std::vector<db::ImageId> imagesToRemove;
// Step 1 collect the episodes to remove
{
auto removePodcastFile{ [&](const db::PodcastEpisode::pointer& episode) {
// We keep the artwork of the episode (TODO, not if the episode is no longer referenced by the podcast?)
assert(!episode->getAudioRelativeFilePath().empty());
utils::removeFile(getCachePath() / episode->getAudioRelativeFilePath());
episodesToRemove.emplace_back(episode->getId());
} };
const Wt::WDateTime now{ Wt::WDateTime::currentDateTime() };
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
db::PodcastEpisode::find(session, db::PodcastEpisode::FindParameters{}, [&](const db::PodcastEpisode::pointer& episode) {
switch (episode->getManualDownloadState())
{
case db::PodcastEpisode::ManualDownloadState::None:
if (!episode->getAudioRelativeFilePath().empty() && now > episode->getPubDate().addDays(_autoDownloadEpisodesMaxAge.count())) // TODO make this configurable per podcast
{
LMS_LOG(PODCAST, INFO, "Removing episode '" << episode->getTitle() << "' because it is older than " << _autoDownloadEpisodesMaxAge.count() << " days");
removePodcastFile(episode);
}
break;
case db::PodcastEpisode::ManualDownloadState::DownloadRequested:
// always keep the manually downloaded episodes
break;
case db::PodcastEpisode::ManualDownloadState::DeleteRequested:
if (!episode->getAudioRelativeFilePath().empty())
{
LMS_LOG(PODCAST, DEBUG, "Removing episode '" << episode->getTitle() << "' because it was manually deleted");
removePodcastFile(episode);
}
break;
}
});
}
// second step, remove the database entries (must be consistent with first step!)
if (!episodesToRemove.empty())
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createWriteTransaction() };
for (db::PodcastEpisodeId episodeId : episodesToRemove)
{
if (db::PodcastEpisode::pointer episode{ db::PodcastEpisode::find(session, episodeId) })
episode.modify()->setAudioRelativeFilePath(std::filesystem::path{});
}
}
onDone();
}
} // namespace lms::podcast
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <chrono>
#include "RefreshStep.hpp"
namespace lms::podcast
{
class RemoveEpisodesStep : public RefreshStep
{
public:
RemoveEpisodesStep(RefreshContext& context, OnDoneCallback callback);
private:
core::LiteralString getName() const override;
void run() override;
const std::chrono::days _autoDownloadEpisodesMaxAge;
};
} // namespace lms::podcast
@@ -0,0 +1,95 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "RemovePodcastsStep.hpp"
#include <filesystem>
#include <vector>
#include "core/ILogger.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/Image.hpp"
#include "database/objects/Podcast.hpp"
#include "database/objects/PodcastEpisode.hpp"
#include "Utils.hpp"
namespace lms::podcast
{
core::LiteralString RemovePodcastsStep::getName() const
{
return "Remove podcasts";
}
void RemovePodcastsStep::run()
{
std::vector<db::PodcastId> podcastsToRemove;
std::vector<db::ImageId> imagesToRemove;
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createReadTransaction() };
db::Podcast::find(session, [&](const db::Podcast::pointer& podcast) {
if (!podcast->isDeleteRequested())
return;
LMS_LOG(PODCAST, DEBUG, "Removing podcast '" << podcast->getUrl() << "'. Title: '" << podcast->getTitle() << "'");
// remove podcast artwork
if (const db::Artwork::pointer artwork{ podcast->getArtwork() })
{
utils::removeFile(artwork->getAbsoluteFilePath());
imagesToRemove.emplace_back(std::get<db::ImageId>(artwork->getUnderlyingId()));
}
db::PodcastEpisode::FindParameters params;
params.setPodcast(podcast->getId());
db::PodcastEpisode::find(session, params, [&](const db::PodcastEpisode::pointer& episode) {
if (const db::Artwork::pointer artwork{ episode->getArtwork() })
{
utils::removeFile(artwork->getAbsoluteFilePath());
imagesToRemove.emplace_back(std::get<db::ImageId>(artwork->getUnderlyingId()));
}
if (!episode->getAudioRelativeFilePath().empty())
utils::removeFile(getCachePath() / episode->getAudioRelativeFilePath());
});
podcastsToRemove.emplace_back(podcast->getId());
});
}
// second step, remove the database entries (must be consistent with first step!)
if (!podcastsToRemove.empty() || !imagesToRemove.empty())
{
auto& session{ getDb().getTLSSession() };
auto transaction{ session.createWriteTransaction() };
session.destroy<db::Podcast>(podcastsToRemove); // will propagate to episodes
session.destroy<db::Image>(imagesToRemove); // will propagate to artworks
}
onDone();
}
} // namespace lms::podcast
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "RefreshStep.hpp"
namespace lms::podcast
{
class RemovePodcastsStep : public RefreshStep
{
using RefreshStep::RefreshStep;
private:
core::LiteralString getName() const override;
void run() override;
};
} // namespace lms::podcast
@@ -0,0 +1,93 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Utils.hpp"
#include <cassert>
#include <system_error>
#include "core/ILogger.hpp"
#include "core/UUID.hpp"
#include "database/Session.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/Image.hpp"
#include "database/objects/Podcast.hpp"
#include "image/Exception.hpp"
#include "image/Image.hpp"
#include "image/Types.hpp"
namespace lms::podcast::utils
{
std::filesystem::path getPodcastRelativePath(const db::Podcast::pointer& podcast)
{
assert(podcast);
return podcast->getId().toString();
}
static std::optional<image::ImageProperties> probeImage(const std::filesystem::path& path)
{
try
{
return image::probeImage(path);
}
catch (const image::Exception& e)
{
LMS_LOG(PODCAST, WARNING, "Failed to probe artwork image " << path << ": " << e.what());
return std::nullopt;
}
}
db::Artwork::pointer createArtworkFromImage(db::Session& session, const std::filesystem::path& filePath, std::string_view mimeType)
{
std::error_code ec;
const auto fileSize{ std::filesystem::file_size(filePath, ec) };
if (ec)
{
LMS_LOG(PODCAST, ERROR, "Failed to get file size of " << filePath << ": " << ec.message());
return db::Artwork::pointer{};
}
db::Image::pointer image{ session.create<db::Image>(filePath) };
image.modify()->setFileSize(static_cast<std::size_t>(fileSize));
if (const std::optional<image::ImageProperties> imageProperties{ probeImage(filePath) })
{
image.modify()->setWidth(imageProperties->width);
image.modify()->setHeight(imageProperties->height);
}
image.modify()->setLastWriteTime(Wt::WDateTime::currentDateTime());
image.modify()->setMimeType(mimeType);
return session.create<db::Artwork>(image);
}
std::string generateRandomFileName()
{
return std::string{ core::UUID::generate().getAsString() };
}
void removeFile(const std::filesystem::path& filePath)
{
std::error_code ec;
std::filesystem::remove(filePath, ec);
if (ec)
LMS_LOG(PODCAST, WARNING, "Failed to remove file " << filePath << ": " << ec.message());
else
LMS_LOG(PODCAST, DEBUG, "Removed file " << filePath);
}
} // namespace lms::podcast::utils
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <filesystem>
#include <string>
#include "database/Object.hpp"
namespace lms::db
{
class Artwork;
class Session;
} // namespace lms::db
namespace lms::podcast::utils
{
db::ObjectPtr<db::Artwork> createArtworkFromImage(db::Session& session, const std::filesystem::path& filePath, std::string_view mimeType);
std::string generateRandomFileName();
void removeFile(const std::filesystem::path& filePath);
} // namespace lms::podcast::utils
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <filesystem>
#include <memory>
#include <boost/asio/io_context.hpp>
#include "database/objects/PodcastEpisodeId.hpp"
#include "database/objects/PodcastId.hpp"
namespace lms::db
{
class IDb;
}
namespace lms::podcast
{
class IPodcastService
{
public:
virtual ~IPodcastService() = default;
virtual std::filesystem::path getCachePath() const = 0;
virtual db::PodcastId addPodcast(std::string_view url) = 0;
virtual bool removePodcast(db::PodcastId podcast) = 0;
virtual void refreshPodcasts() = 0;
virtual bool downloadPodcastEpisode(db::PodcastEpisodeId episode) = 0;
virtual bool deletePodcastEpisode(db::PodcastEpisodeId episode) = 0;
};
std::unique_ptr<IPodcastService> createPodcastService(boost::asio::io_context& ioContext, db::IDb& db, const std::filesystem::path& cachePath);
} // namespace lms::podcast
@@ -0,0 +1,19 @@
add_executable(test-podcast
PodcastParser.cpp
PodcastService.cpp
)
target_link_libraries(test-podcast PRIVATE
lmscore
lmspodcast
GTest::GTest
)
target_include_directories(test-podcast PRIVATE
../impl
)
if (NOT CMAKE_CROSSCOMPILING)
gtest_discover_tests(test-podcast)
endif()
@@ -0,0 +1,151 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include <chrono>
#include <Wt/WDate.h>
#include <Wt/WTime.h>
#include <gtest/gtest.h>
#include "PodcastParsing.hpp"
namespace lms::podcast::tests
{
TEST(Podcast, PodcastParsing)
{
constexpr std::string_view xmlData{ R"(<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:pa="http://podcastaddict.com" xmlns:podcastRF="http://radiofrance.fr/Lancelot/Podcast#" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0" version="2.0">
<channel>
<title>Affaires sensibles</title>
<link>https://www.franceinter.fr/emission-affaires-sensibles</link>
<description>Les grandes affaires, les aventures et les procès qui ont marqué les cinquante dernières années. Vous aimez ce podcast ? Pour écouter tous les épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_podcast&at_medium=lien_RSS">Radio France</a>.</description>
<language>fr</language>
<copyright>Radio France</copyright>
<lastBuildDate>Sat, 09 Aug 2025 21:34:32 +0200</lastBuildDate>
<generator>Radio France</generator>
<image>
<url>https://www.radiofrance.fr/s3/cruiser-production/2023/05/5f197ac5-b950-4149-8578-ae12aa6695b0/1400x1400_sc_rf_omm_0000038645_ite.jpg</url>
<title>Affaires sensibles</title>
<link>https://www.franceinter.fr/emission-affaires-sensibles</link>
</image>
<itunes:author>France Inter</itunes:author>
<itunes:category text="Society & Culture"/>
<itunes:explicit>no</itunes:explicit>
<itunes:image href="https://www.radiofrance.fr/s3/cruiser-production/2023/05/5f197ac5-b950-4149-8578-ae12aa6695b0/1400x1400_sc_rf_omm_0000038645_ite.jpg"/>
<itunes:owner>
<itunes:email>podcast@radiofrance.com</itunes:email>
<itunes:name>Radio France</itunes:name>
</itunes:owner>
<itunes:subtitle>Affaires sensibles</itunes:subtitle>
<itunes:summary>Les grandes affaires, les aventures et les procès qui ont marqué les cinquante dernières années. Vous aimez ce podcast ? Pour écouter tous les épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_podcast&at_medium=lien_RSS">Radio France</a>.</itunes:summary>
<itunes:new-feed-url>https://radiofrance-podcast.net/podcast09/35099478-7c72-4f9e-a6de-1b928400e9e5/rss_13940.xml</itunes:new-feed-url>
<pa:new-feed-url>https://radiofrance-podcast.net/podcast09/d4463877-caa3-4507-9399-f5eb00fde027/rss_13940.xml</pa:new-feed-url>
<podcastRF:originStation>1</podcastRF:originStation>
<googleplay:block>yes</googleplay:block>
<item>
<title>Apollo 13 ou les naufragés de lespace</title>
<link>https://www.radiofrance.fr/franceinter/podcasts/affaires-sensibles/affaires-sensibles-du-jeudi-30-decembre-2021-4186094</link>
<description>durée : 00:53:58 - Affaires sensibles - par : Christophe Barreyre, Fabrice Drouelle - C'est une épopée qui réunit héroïsme, génie technologique, audace diplomatique, sens du tragique. Lodyssée d'Apollo 13 cest un vaisseau spatial en perdition, -haut, flirtant avec la lune mais qui au dernier moment se dérobe pour une vulgaire panne électrique dans la soute du vaisseau. - réalisé par : Flora BERNARD, Marion Le Lay, Stéphane COSME Vous aimez ce podcast ? Pour écouter tous les autres épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_episode&at_medium=lien_RSS">Radio France</a>.</description>
<author>podcast@radiofrance.com (Radio France)</author>
<category>Society & Culture</category>
<enclosure url="https://proxycast.radiofrance.fr/e2a713a6-aba0-4d2a-a4a1-d135d98f1f8a/13940-09.08.2025-ITEMA_24214125-2021F22805S0364-NET_MFI_F7191B05-DD5B-4AFE-BB96-3BD8ADB3240A-22.mp3" length="51842568" type="audio/mpeg"/>
<guid isPermaLink="false">9f587824-01f5-443d-aa72-a56519d25857-NET_MFI_F7191B05-DD5B-4AFE-BB96-3BD8ADB3240A</guid>
<pubDate>Sat, 09 Aug 2025 15:59:59 +0200</pubDate>
<podcastRF:businessReference>22805</podcastRF:businessReference>
<podcastRF:magnetothequeID>2021F22805S0364</podcastRF:magnetothequeID>
<itunes:title>Apollo 13 ou les naufragés de lespace</itunes:title>
<itunes:image href="https://www.radiofrance.fr/s3/cruiser-production/2021/04/ec0f1c5d-ecfa-4ec4-a5a5-30f446d25aea/1400x1400_affaires_sensibles.jpg"/>
<itunes:author>Christophe Barreyre, Fabrice Drouelle</itunes:author>
<itunes:explicit>no</itunes:explicit>
<itunes:keywords>Apollo,13,ou,les,naufragés,de,lespace</itunes:keywords>
<itunes:subtitle>Apollo 13 ou les naufragés de lespace</itunes:subtitle>
<itunes:summary>durée : 00:53:58 - Affaires sensibles - par : Christophe Barreyre, Fabrice Drouelle - C'est une épopée qui réunit héroïsme, génie technologique, audace diplomatique, sens du tragique. Lodyssée d'Apollo 13 cest un vaisseau spatial en perdition, -haut, flirtant avec la lune mais qui au dernier moment se dérobe pour une vulgaire panne électrique dans la soute du vaisseau. - réalisé par : Flora BERNARD, Marion Le Lay, Stéphane COSME Vous aimez ce podcast ? Pour écouter tous les autres épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_episode&at_medium=lien_RSS">Radio France</a>.</itunes:summary>
<itunes:duration>00:53:58</itunes:duration>
<googleplay:block>yes</googleplay:block>
</item>
<item>
<title>Stéphane Breitwieser, le pilleur de musées</title>
<link>https://www.radiofrance.fr/franceinter/podcasts/affaires-sensibles/affaires-sensibles-du-mercredi-02-avril-2025-9048848</link>
<description>durée : 00:47:45 - Affaires sensibles - par : Fabrice Drouelle, Franck COGNARD - Aujourdhui dans Affaires Sensibles : laffaire Stéphane Breitwieser, le pilleur de musées - réalisé par : Etienne BERTIN Vous aimez ce podcast ? Pour écouter tous les autres épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_episode&at_medium=lien_RSS">Radio France</a>.</description>
<author>podcast@radiofrance.com (Radio France)</author>
<category>Society & Culture</category>
<enclosure url="https://proxycast.radiofrance.fr/d0895b0b-a99c-4e9d-9d99-13a029960d04/13940-08.08.2025-ITEMA_24213067-2025F22805S0092-NET_MFI_FF964E00-DFB6-45AC-A9D2-2D1211F37586-22.mp3" length="45869054" type="audio/mpeg"/>
<guid isPermaLink="false">719c21be-3832-4b67-8d65-9d07b3715d7b-NET_MFI_FF964E00-DFB6-45AC-A9D2-2D1211F37586</guid>
<pubDate>Fri, 08 Aug 2025 20:59:59 +0200</pubDate>
<podcastRF:businessReference>22805</podcastRF:businessReference>
<podcastRF:magnetothequeID>2025F22805S0092</podcastRF:magnetothequeID>
<itunes:title>Stéphane Breitwieser, le pilleur de musées</itunes:title>
<itunes:image href="https://www.radiofrance.fr/s3/cruiser-production/2023/04/7b50cf5f-f5bd-4dc4-8b1d-b08666768dcf/1400x1400_sc_affaires-sensibles.jpg"/>
<itunes:author>Fabrice Drouelle, Franck COGNARD</itunes:author>
<itunes:explicit>no</itunes:explicit>
<itunes:keywords>Stéphane,Breitwieser,,le,pilleur,de,musées</itunes:keywords>
<itunes:subtitle>Stéphane Breitwieser, le pilleur de musées</itunes:subtitle>
<itunes:summary>durée : 00:47:45 - Affaires sensibles - par : Fabrice Drouelle, Franck COGNARD - Aujourdhui dans Affaires Sensibles : laffaire Stéphane Breitwieser, le pilleur de musées - réalisé par : Etienne BERTIN Vous aimez ce podcast ? Pour écouter tous les autres épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_episode&at_medium=lien_RSS">Radio France</a>.</itunes:summary>
<itunes:duration>00:47:45</itunes:duration>
<googleplay:block>yes</googleplay:block>
</item>
</channel>
</rss>)" };
const Podcast podcast{ parsePodcastRssFeed(xmlData) };
EXPECT_EQ(podcast.title, "Affaires sensibles");
EXPECT_EQ(podcast.description, R"(Les grandes affaires, les aventures et les procès qui ont marqué les cinquante dernières années. Vous aimez ce podcast ? Pour écouter tous les épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_podcast&amp;at_medium=lien_RSS">Radio France</a>.)");
EXPECT_EQ(podcast.author, "France Inter");
EXPECT_EQ(podcast.link, "https://www.franceinter.fr/emission-affaires-sensibles");
EXPECT_EQ(podcast.language, "fr");
EXPECT_EQ(podcast.copyright, "Radio France");
EXPECT_EQ(podcast.lastBuildDate, (Wt::WDateTime{ Wt::WDate{ 2025, 8, 9 }, Wt::WTime{ 23, 34, 32 } }));
EXPECT_EQ(podcast.imageUrl, "https://www.radiofrance.fr/s3/cruiser-production/2023/05/5f197ac5-b950-4149-8578-ae12aa6695b0/1400x1400_sc_rf_omm_0000038645_ite.jpg");
// itunes
EXPECT_EQ(podcast.copyright, "Radio France");
EXPECT_EQ(podcast.author, "France Inter");
EXPECT_EQ(podcast.category, "Society & Culture");
EXPECT_EQ(podcast.explicitContent, false);
EXPECT_EQ(podcast.ownerEmail, "podcast@radiofrance.com");
EXPECT_EQ(podcast.ownerName, "Radio France");
EXPECT_EQ(podcast.subtitle, "Affaires sensibles");
EXPECT_EQ(podcast.summary, R"(Les grandes affaires, les aventures et les procès qui ont marqué les cinquante dernières années. Vous aimez ce podcast ? Pour écouter tous les épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_podcast&amp;at_medium=lien_RSS">Radio France</a>.)");
ASSERT_EQ(podcast.episodes.size(), 2);
EXPECT_EQ(podcast.episodes[0].title, R"(Apollo 13 ou les naufragés de lespace)");
EXPECT_EQ(podcast.episodes[0].description, R"(durée : 00:53:58 - Affaires sensibles - par : Christophe Barreyre, Fabrice Drouelle - C'est une épopée qui réunit héroïsme, génie technologique, audace diplomatique, sens du tragique. Lodyssée d'Apollo 13 cest un vaisseau spatial en perdition, là-haut, flirtant avec la lune mais qui au dernier moment se dérobe pour une vulgaire panne électrique dans la soute du vaisseau. - réalisé par : Flora BERNARD, Marion Le Lay, Stéphane COSME Vous aimez ce podcast ? Pour écouter tous les autres épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_episode&amp;at_medium=lien_RSS">Radio France</a>.)");
EXPECT_EQ(podcast.episodes[0].author, "Christophe Barreyre, Fabrice Drouelle");
EXPECT_EQ(podcast.episodes[0].explicitContent, false);
EXPECT_EQ(podcast.episodes[0].imageUrl, "https://www.radiofrance.fr/s3/cruiser-production/2021/04/ec0f1c5d-ecfa-4ec4-a5a5-30f446d25aea/1400x1400_affaires_sensibles.jpg");
EXPECT_EQ(podcast.episodes[0].link, "https://www.radiofrance.fr/franceinter/podcasts/affaires-sensibles/affaires-sensibles-du-jeudi-30-decembre-2021-4186094");
EXPECT_EQ(podcast.episodes[0].pubDate, (Wt::WDateTime{ Wt::WDate{ 2025, 8, 9 }, Wt::WTime{ 17, 59, 59 } }));
EXPECT_EQ(podcast.episodes[0].guid, "9f587824-01f5-443d-aa72-a56519d25857-NET_MFI_F7191B05-DD5B-4AFE-BB96-3BD8ADB3240A");
EXPECT_EQ(podcast.episodes[0].enclosureUrl.url, "https://proxycast.radiofrance.fr/e2a713a6-aba0-4d2a-a4a1-d135d98f1f8a/13940-09.08.2025-ITEMA_24214125-2021F22805S0364-NET_MFI_F7191B05-DD5B-4AFE-BB96-3BD8ADB3240A-22.mp3");
EXPECT_EQ(podcast.episodes[0].enclosureUrl.length, 51842568);
EXPECT_EQ(podcast.episodes[0].enclosureUrl.type, "audio/mpeg");
EXPECT_EQ(podcast.episodes[0].duration, std::chrono::minutes{ 53 } + std::chrono::seconds{ 58 });
EXPECT_EQ(podcast.episodes[1].title, R"(Stéphane Breitwieser, le pilleur de musées)");
EXPECT_EQ(podcast.episodes[1].description, R"(durée : 00:47:45 - Affaires sensibles - par : Fabrice Drouelle, Franck COGNARD - Aujourdhui dans Affaires Sensibles : laffaire Stéphane Breitwieser, le pilleur de musées - réalisé par : Etienne BERTIN Vous aimez ce podcast ? Pour écouter tous les autres épisodes sans limite, rendez-vous sur <a href="https://www.franceinter.fr/emission-affaires-sensibles?at_campaign=desc_episode&amp;at_medium=lien_RSS">Radio France</a>.)");
EXPECT_EQ(podcast.episodes[1].author, "Fabrice Drouelle, Franck COGNARD");
EXPECT_EQ(podcast.episodes[1].explicitContent, false);
EXPECT_EQ(podcast.episodes[1].imageUrl, "https://www.radiofrance.fr/s3/cruiser-production/2023/04/7b50cf5f-f5bd-4dc4-8b1d-b08666768dcf/1400x1400_sc_affaires-sensibles.jpg");
EXPECT_EQ(podcast.episodes[1].link, "https://www.radiofrance.fr/franceinter/podcasts/affaires-sensibles/affaires-sensibles-du-mercredi-02-avril-2025-9048848");
EXPECT_EQ(podcast.episodes[1].pubDate, (Wt::WDateTime{ Wt::WDate{ 2025, 8, 8 }, Wt::WTime{ 22, 59, 59 } }));
EXPECT_EQ(podcast.episodes[1].guid, "719c21be-3832-4b67-8d65-9d07b3715d7b-NET_MFI_FF964E00-DFB6-45AC-A9D2-2D1211F37586");
EXPECT_EQ(podcast.episodes[1].enclosureUrl.url, "https://proxycast.radiofrance.fr/d0895b0b-a99c-4e9d-9d99-13a029960d04/13940-08.08.2025-ITEMA_24213067-2025F22805S0092-NET_MFI_FF964E00-DFB6-45AC-A9D2-2D1211F37586-22.mp3");
EXPECT_EQ(podcast.episodes[1].enclosureUrl.length, 45869054);
EXPECT_EQ(podcast.episodes[1].enclosureUrl.type, "audio/mpeg");
EXPECT_EQ(podcast.episodes[1].duration, std::chrono::minutes{ 47 } + std::chrono::seconds{ 45 });
}
} // namespace lms::podcast::tests
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gtest/gtest.h>
#include "core/ILogger.hpp"
#include "core/Service.hpp"
int main(int argc, char** argv)
{
using namespace lms;
core::Service<core::logging::ILogger> logger{ core::logging::createLogger(core::logging::Severity::ERROR) };
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
@@ -153,14 +153,15 @@ namespace lms::scanner
} // namespace
std::unique_ptr<IScannerService> createScannerService(db::IDb& db)
std::unique_ptr<IScannerService> createScannerService(db::IDb& db, const std::filesystem::path& cachePath)
{
return std::make_unique<ScannerService>(db);
return std::make_unique<ScannerService>(db, cachePath);
}
ScannerService::ScannerService(db::IDb& db)
ScannerService::ScannerService(db::IDb& db, const std::filesystem::path& cachePath)
: _db{ db }
, _jobScheduler{ core::createJobScheduler("Scanner", getScannerThreadCount()) }
, _cachePath{ cachePath }
{
_ioService.setThreadCount(1);
@@ -496,6 +497,7 @@ namespace lms::scanner
.abortScan = _abortScan,
.db = _db,
.fileScanners = _fileScanners,
.cachePath = _cachePath
};
// Order is important: steps are sequential
@@ -31,11 +31,12 @@
#include <Wt/WIOService.h>
#include <Wt/WSignal.h>
#include "FileScanners.hpp"
#include "ScannerSettings.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "services/scanner/IScannerService.hpp"
#include "FileScanners.hpp"
#include "ScannerSettings.hpp"
#include "steps/IScanStep.hpp"
namespace lms::core
@@ -53,7 +54,7 @@ namespace lms::scanner
class ScannerService : public IScannerService
{
public:
ScannerService(db::IDb& db);
ScannerService(db::IDb& db, const std::filesystem::path& cachePath);
~ScannerService() override;
ScannerService(const ScannerService&) = delete;
ScannerService& operator=(const ScannerService&) = delete;
@@ -89,6 +90,7 @@ namespace lms::scanner
db::IDb& _db;
std::unique_ptr<core::IJobScheduler> _jobScheduler;
const std::filesystem::path _cachePath;
FileScanners _fileScanners;
std::vector<std::unique_ptr<IScanStep>> _scanSteps;
@@ -30,6 +30,7 @@ namespace lms::scanner
, _db{ initParams.db }
, _jobScheduler{ initParams.jobScheduler }
, _fileScanners(initParams.fileScanners)
, _cachePath{ initParams.cachePath }
, _lastScanSettings{ initParams.lastScanSettings }
{
}
@@ -55,6 +55,7 @@ namespace lms::scanner
bool& abortScan;
db::IDb& db;
const FileScanners& fileScanners;
const std::filesystem::path& cachePath;
};
ScanStepBase(InitParams& initParams);
~ScanStepBase() override;
@@ -65,6 +66,7 @@ namespace lms::scanner
core::IJobScheduler& getJobScheduler() { return _jobScheduler; };
const ScannerSettings* getLastScanSettings() const { return _lastScanSettings; }
const FileScanners& getFileScanners() const { return _fileScanners; }
const std::filesystem::path& getCachePath() const { return _cachePath; }
void addError(ScanContext& context, std::shared_ptr<ScanError> error);
@@ -83,6 +85,7 @@ namespace lms::scanner
private:
core::IJobScheduler& _jobScheduler;
const FileScanners& _fileScanners;
const std::filesystem::path& _cachePath;
const ScannerSettings* _lastScanSettings{};
ScanErrorLogger _scanErrorLogger;
@@ -63,6 +63,9 @@ namespace lms::scanner
{
}
CheckForRemovedFilesJob(const CheckForRemovedFilesJob&) = delete;
CheckForRemovedFilesJob& operator=(const CheckForRemovedFilesJob&) = delete;
std::size_t getProcessedCount() const { return _processedCount; }
std::span<const IdType> getObjectsToRemove() const { return _objectsToRemove; }
@@ -90,8 +93,7 @@ namespace lms::scanner
return false;
}
// For each track, make sure the the file still exists
// and still belongs to a media directory
// For file, make sure the the file still exists, is a regular file, is in a media directory and is of a supported format
if (!fileEntry.exists() || !fileEntry.is_regular_file())
{
LMS_LOG(DBUPDATER, DEBUG, "Removing " << p << ": missing");
@@ -151,7 +153,7 @@ namespace lms::scanner
}
template<typename Object>
bool fetchNextFilesToCheck(db::Session& session, typename Object::IdType& lastCheckedId, std::vector<FileToCheck<typename Object::IdType>>& filesToCheck)
bool fetchNextFilesToCheck(db::Session& session, typename Object::IdType& lastCheckedId, const std::filesystem::path& cachepath, std::vector<FileToCheck<typename Object::IdType>>& filesToCheck)
{
constexpr std::size_t batchSize{ 200 };
@@ -164,6 +166,10 @@ namespace lms::scanner
{
const typename Object::IdType previousLastCheckedId{ lastCheckedId };
Object::findAbsoluteFilePath(session, lastCheckedId, batchSize, [&](Object::IdType objectId, const std::filesystem::path& filePath) {
// Do not consider files in the cache directory as they are not managed by the scanner itself
if (core::pathUtils::isPathInRootPath(filePath, cachepath))
return;
// special case for track lyrics, only check external lyrics
if constexpr (std::is_same_v<Object, db::TrackLyrics>)
{
@@ -242,12 +248,11 @@ namespace lms::scanner
ObjectIdType lastCheckedId;
std::vector<FileToCheck<ObjectIdType>> filesToCheck;
while (fetchNextFilesToCheck<Object>(session, lastCheckedId, filesToCheck))
while (fetchNextFilesToCheck<Object>(session, lastCheckedId, getCachePath(), filesToCheck))
queue.push(std::make_unique<CheckForRemovedFilesJob<ObjectIdType>>(_settings, getFileScanners(), filesToCheck));
}
// process all remaining objects
context.stats.deletions += removeObjects<Object>(session, objectIdsToRemove, false);
}
} // namespace lms::scanner
@@ -19,6 +19,7 @@
#pragma once
#include <filesystem>
#include <optional>
#include "ScannerEvents.hpp"
@@ -61,5 +62,5 @@ namespace lms::scanner
virtual Events& getEvents() = 0;
};
std::unique_ptr<IScannerService> createScannerService(db::IDb& db);
std::unique_ptr<IScannerService> createScannerService(db::IDb& db, const std::filesystem::path& cachePath);
} // namespace lms::scanner
@@ -27,7 +27,6 @@
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/post.hpp>
#include "ListensParser.hpp"
#include "core/IConfig.hpp"
#include "core/Service.hpp"
#include "core/http/IClient.hpp"
@@ -40,6 +39,7 @@
#include "database/objects/User.hpp"
#include "services/scrobbling/Exception.hpp"
#include "ListensParser.hpp"
#include "Utils.hpp"
namespace lms::scrobbling::listenBrainz
@@ -246,7 +246,7 @@ namespace lms::scrobbling::listenBrainz
saveListen(timedListen, db::SyncState::PendingAdd);
request.priority = core::http::ClientRequestParameters::Priority::Normal;
request.onSuccessFunc = [this, timedListen](std::string_view) {
request.onSuccessFunc = [this, timedListen](const Wt::Http::Message&) {
boost::asio::post(boost::asio::bind_executor(_strand, [this, timedListen] {
if (saveListen(timedListen, db::SyncState::Synchronized))
{
@@ -456,8 +456,8 @@ namespace lms::scrobbling::listenBrainz
request.priority = core::http::ClientRequestParameters::Priority::Low;
request.relativeUrl = "/1/validate-token";
request.headers = { { "Authorization", "Token " + std::string{ listenBrainzToken->getAsString() } } };
request.onSuccessFunc = [this, &context](std::string_view msgBody) {
context.listenBrainzUserName = utils::parseValidateToken(msgBody);
request.onSuccessFunc = [this, &context](const Wt::Http::Message& msg) {
context.listenBrainzUserName = utils::parseValidateToken(msg.body());
if (context.listenBrainzUserName.empty())
{
onSyncEnded(context);
@@ -479,8 +479,8 @@ namespace lms::scrobbling::listenBrainz
core::http::ClientGETRequestParameters request;
request.relativeUrl = "/1/user/" + std::string{ context.listenBrainzUserName } + "/listen-count";
request.priority = core::http::ClientRequestParameters::Priority::Low;
request.onSuccessFunc = [this, &context](std::string_view msgBody) {
const auto listenCount{ parseListenCount(msgBody) };
request.onSuccessFunc = [this, &context](const Wt::Http::Message& msg) {
const auto listenCount{ parseListenCount(msg.body()) };
boost::asio::post(boost::asio::bind_executor(_strand, [this, listenCount, &context] {
if (listenCount)
LOG(DEBUG, "Listen count for listenbrainz user '" << context.listenBrainzUserName << "' = " << *listenCount);
@@ -512,8 +512,8 @@ namespace lms::scrobbling::listenBrainz
core::http::ClientGETRequestParameters request;
request.relativeUrl = "/1/user/" + context.listenBrainzUserName + "/listens?max_ts=" + std::to_string(context.maxDateTime.toTime_t());
request.priority = core::http::ClientRequestParameters::Priority::Low;
request.onSuccessFunc = [this, &context](std::string_view msgBody) {
processGetListensResponse(msgBody, context);
request.onSuccessFunc = [this, &context](const Wt::Http::Message& msg) {
processGetListensResponse(msg.body(), context);
if (context.fetchedListenCount >= _maxSyncListenCount || !context.maxDateTime.isValid())
{
onSyncEnded(context);
@@ -23,7 +23,6 @@
#include "core/ILogger.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Track.hpp"
#include "TranscodingResourceHandler.hpp"
@@ -64,20 +63,17 @@ namespace lms::transcoding
{
av::InputParameters avInputParams;
std::optional<std::size_t> estimatedContentLength;
{
auto& session{ _db.getTLSSession() };
auto transaction{ session.createReadTransaction() };
db::Track::pointer track{ db::Track::find(session, inputParameters.trackId) };
if (!track)
return nullptr;
avInputParams.file = track->getAbsoluteFilePath();
avInputParams.file = inputParameters.filePath;
avInputParams.offset = inputParameters.offset;
avInputParams.streamIndex = inputParameters.streamIndex;
if (estimateContentLength)
estimatedContentLength = doEstimateContentLength(outputParameters.bitrate, track->getDuration());
{
if (inputParameters.offset < inputParameters.duration)
estimatedContentLength = doEstimateContentLength(outputParameters.bitrate, inputParameters.duration - inputParameters.offset);
else
LMS_LOG(TRANSCODING, WARNING, "Offset " << inputParameters.offset << " is greater than audio file duration " << inputParameters.duration << ": not estimating content length");
}
return std::make_unique<TranscodingResourceHandler>(avInputParams, toAv(outputParameters), estimatedContentLength);
@@ -19,12 +19,11 @@
#pragma once
#include <chrono>
#include <filesystem>
#include <memory>
#include <optional>
#include "database/objects/TrackId.hpp"
namespace lms
{
namespace core
@@ -43,9 +42,10 @@ namespace lms::transcoding
{
struct InputParameters
{
db::TrackId trackId;
std::chrono::milliseconds offset{}; // Offset in the track file to start transcoding from
std::optional<std::size_t> streamIndex; // Index of the stream to be transcoded (select "best" audio stream if not set)
std::filesystem::path filePath;
std::chrono::milliseconds duration{}; // Duration of the audio file
std::chrono::milliseconds offset{}; // Offset in the audio file to start transcoding from
std::optional<std::size_t> streamIndex; // Index of the stream to be transcoded (select the "best" audio stream if not set)
};
enum class OutputFormat
+3
View File
@@ -7,6 +7,7 @@ add_library(lmssubsonic STATIC
impl/endpoints/MediaLibraryScanning.cpp
impl/endpoints/MediaRetrieval.cpp
impl/endpoints/Playlists.cpp
impl/endpoints/Podcast.cpp
impl/endpoints/Searching.cpp
impl/endpoints/System.cpp
impl/endpoints/UserManagement.cpp
@@ -21,6 +22,7 @@ add_library(lmssubsonic STATIC
impl/responses/Genre.cpp
impl/responses/Lyrics.cpp
impl/responses/Playlist.cpp
impl/responses/Podcast.cpp
impl/responses/RecordLabel.cpp
impl/responses/ReplayGain.cpp
impl/responses/Song.cpp
@@ -49,6 +51,7 @@ target_link_libraries(lmssubsonic PRIVATE
lmsav
lmsdatabase
lmsfeedback
lmspodcast
lmsrecommendation
lmsscanner
lmsscrobbling
@@ -34,7 +34,6 @@ namespace lms::api::subsonic
};
static inline constexpr ProtocolVersion defaultServerProtocolVersion{ 1, 16, 0 };
static inline constexpr std::string_view serverVersion{ "8" };
} // namespace lms::api::subsonic
namespace lms::core::stringUtils
+42 -2
View File
@@ -19,8 +19,6 @@
#include "SubsonicId.hpp"
#include "core/String.hpp"
namespace lms::api::subsonic
{
std::string idToString(db::ArtistId id)
@@ -33,6 +31,16 @@ namespace lms::api::subsonic
return "dir-" + id.toString();
}
std::string idToString(db::PodcastEpisodeId id)
{
return "podep-" + id.toString();
}
std::string idToString(db::PodcastId id)
{
return "pod-" + id.toString();
}
std::string idToString(db::ReleaseId id)
{
return "al-" + id.toString();
@@ -92,6 +100,38 @@ namespace lms::core::stringUtils
return std::nullopt;
}
template<>
std::optional<db::PodcastEpisodeId> readAs(std::string_view str)
{
std::vector<std::string_view> values{ core::stringUtils::splitString(str, '-') };
if (values.size() != 2)
return std::nullopt;
if (values[0] != "podep")
return std::nullopt;
if (const auto value{ core::stringUtils::readAs<db::PodcastEpisodeId::ValueType>(values[1]) })
return db::PodcastEpisodeId{ *value };
return std::nullopt;
}
template<>
std::optional<db::PodcastId> readAs(std::string_view str)
{
std::vector<std::string_view> values{ core::stringUtils::splitString(str, '-') };
if (values.size() != 2)
return std::nullopt;
if (values[0] != "pod")
return std::nullopt;
if (const auto value{ core::stringUtils::readAs<db::PodcastId::ValueType>(values[1]) })
return db::PodcastId{ *value };
return std::nullopt;
}
template<>
std::optional<db::ReleaseId> readAs(std::string_view str)
{
+10
View File
@@ -23,6 +23,8 @@
#include "database/objects/ArtistId.hpp"
#include "database/objects/DirectoryId.hpp"
#include "database/objects/MediaLibraryId.hpp"
#include "database/objects/PodcastEpisodeId.hpp"
#include "database/objects/PodcastId.hpp"
#include "database/objects/ReleaseId.hpp"
#include "database/objects/TrackId.hpp"
#include "database/objects/TrackListId.hpp"
@@ -31,6 +33,8 @@ namespace lms::api::subsonic
{
std::string idToString(db::ArtistId id);
std::string idToString(db::DirectoryId id);
std::string idToString(db::PodcastEpisodeId id);
std::string idToString(db::PodcastId id);
std::string idToString(db::ReleaseId id);
std::string idToString(db::TrackId id);
std::string idToString(db::TrackListId id);
@@ -48,6 +52,12 @@ namespace lms::core::stringUtils
template<>
std::optional<db::MediaLibraryId> readAs(std::string_view str);
template<>
std::optional<db::PodcastEpisodeId> readAs(std::string_view str);
template<>
std::optional<db::PodcastId> readAs(std::string_view str);
template<>
std::optional<db::ReleaseId> readAs(std::string_view str);
+20 -9
View File
@@ -46,6 +46,7 @@
#include "endpoints/MediaLibraryScanning.hpp"
#include "endpoints/MediaRetrieval.hpp"
#include "endpoints/Playlists.hpp"
#include "endpoints/Podcast.hpp"
#include "endpoints/Searching.hpp"
#include "endpoints/System.hpp"
#include "endpoints/UserManagement.hpp"
@@ -97,8 +98,13 @@ namespace lms::api::subsonic
std::string res;
bool firstParameter{ true };
for (const auto& [type, values] : parameterMap)
{
if (!firstParameter)
res += ", ";
firstParameter = false;
res += "{" + type + "=";
if (values.size() == 1)
{
@@ -107,14 +113,18 @@ namespace lms::api::subsonic
else
{
res += "{";
bool firstValue{ true };
for (const std::string& value : values)
{
res += redactValueIfNeeded(type, value);
if (!firstValue)
res += ',';
firstValue = false;
res += redactValueIfNeeded(type, value);
}
res += "}";
}
res += "}, ";
res += "}";
}
return res;
@@ -210,13 +220,14 @@ namespace lms::api::subsonic
{ "/deleteShare", { handleNotImplemented } },
// Podcast
{ "/getPodcasts", { handleNotImplemented } },
{ "/getNewestPodcasts", { handleNotImplemented } },
{ "/refreshPodcasts", { handleNotImplemented } },
{ "/createPodcastChannel", { handleNotImplemented } },
{ "/deletePodcastChannel", { handleNotImplemented } },
{ "/deletePodcastEpisode", { handleNotImplemented } },
{ "/downloadPodcastEpisode", { handleNotImplemented } },
{ "/getPodcasts", { handleGetPodcasts } },
{ "/getNewestPodcasts", { handleGetNewestPodcasts } },
{ "/refreshPodcasts", { handleRefreshPodcasts, AuthenticationMode::Authenticated, { db::UserType::ADMIN } } },
{ "/createPodcastChannel", { handleCreatePodcastChannel, AuthenticationMode::Authenticated, { db::UserType::ADMIN } } },
{ "/deletePodcastChannel", { handleDeletePodcastChannel, AuthenticationMode::Authenticated, { db::UserType::ADMIN } } },
{ "/deletePodcastEpisode", { handleDeletePodcastEpisode, AuthenticationMode::Authenticated, { db::UserType::ADMIN } } },
{ "/downloadPodcastEpisode", { handleDownloadPodcastEpisode, AuthenticationMode::Authenticated, { db::UserType::ADMIN } } },
{ "/getPodcastEpisode", { handleGetPodcastEpisode } },
// Jukebox
{ "/jukeboxControl", { handleNotImplemented } },

Some files were not shown because too many files have changed in this diff Show More