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