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
@@ -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