Subsonic API: do not report numeric values as strings

This commit is contained in:
emeric
2020-06-21 14:25:23 +02:00
parent 73acd65bb3
commit 03446188ba
4 changed files with 60 additions and 26 deletions
+1 -1
View File
@@ -36,7 +36,7 @@ namespace API::Subsonic::Scan
statusResponse.setAttribute("scanning", scanStatus.currentState == IMediaScanner::State::InProgress ? "true" : "false"); statusResponse.setAttribute("scanning", scanStatus.currentState == IMediaScanner::State::InProgress ? "true" : "false");
if (scanStatus.currentState == IMediaScanner::State::InProgress && scanStatus.inProgressScanStats) if (scanStatus.currentState == IMediaScanner::State::InProgress && scanStatus.inProgressScanStats)
statusResponse.setAttribute("count", std::to_string(scanStatus.inProgressScanStats->processedFiles)); statusResponse.setAttribute("count", scanStatus.inProgressScanStats->processedFiles);
return statusResponse; return statusResponse;
} }
+13 -13
View File
@@ -290,18 +290,18 @@ trackToResponseNode(const Track::pointer& track, Session& dbSession, const User:
trackResponse.setAttribute("isDir", "false"); trackResponse.setAttribute("isDir", "false");
trackResponse.setAttribute("title", track->getName()); trackResponse.setAttribute("title", track->getName());
if (track->getTrackNumber()) if (track->getTrackNumber())
trackResponse.setAttribute("track", std::to_string(*track->getTrackNumber())); trackResponse.setAttribute("track", *track->getTrackNumber());
if (track->getDiscNumber()) if (track->getDiscNumber())
trackResponse.setAttribute("discNumber", std::to_string(*track->getDiscNumber())); trackResponse.setAttribute("discNumber", *track->getDiscNumber());
if (track->getYear()) if (track->getYear())
trackResponse.setAttribute("year", std::to_string(*track->getYear())); trackResponse.setAttribute("year", *track->getYear());
trackResponse.setAttribute("path", getTrackPath(track)); trackResponse.setAttribute("path", getTrackPath(track));
{ {
std::error_code ec; std::error_code ec;
const auto fileSize {std::filesystem::file_size(track->getPath(), ec)}; const auto fileSize {std::filesystem::file_size(track->getPath(), ec)};
if (!ec) if (!ec)
trackResponse.setAttribute("size", std::to_string(fileSize)); trackResponse.setAttribute("size", fileSize);
} }
if (track->getPath().has_extension()) if (track->getPath().has_extension())
@@ -331,7 +331,7 @@ trackToResponseNode(const Track::pointer& track, Session& dbSession, const User:
trackResponse.setAttribute("parent", IdToString({Id::Type::Release, track->getRelease().id()})); trackResponse.setAttribute("parent", IdToString({Id::Type::Release, track->getRelease().id()}));
} }
trackResponse.setAttribute("duration", std::to_string(std::chrono::duration_cast<std::chrono::seconds>(track->getDuration()).count())); trackResponse.setAttribute("duration", std::chrono::duration_cast<std::chrono::seconds>(track->getDuration()).count());
trackResponse.setAttribute("type", "music"); trackResponse.setAttribute("type", "music");
if (user->hasStarredTrack(track)) if (user->hasStarredTrack(track))
@@ -355,7 +355,7 @@ trackBookmarkToResponseNode(const TrackBookmark::pointer& trackBookmark)
{ {
Response::Node trackBookmarkNode; Response::Node trackBookmarkNode;
trackBookmarkNode.setAttribute("position", std::to_string(trackBookmark->getOffset().count())); trackBookmarkNode.setAttribute("position", trackBookmark->getOffset().count());
if (!trackBookmark->getComment().empty()) if (!trackBookmark->getComment().empty())
trackBookmarkNode.setAttribute("comment", trackBookmark->getComment()); trackBookmarkNode.setAttribute("comment", trackBookmark->getComment());
trackBookmarkNode.setAttribute("created", reportedCreatedBookmarkDate); trackBookmarkNode.setAttribute("created", reportedCreatedBookmarkDate);
@@ -374,7 +374,7 @@ releaseToResponseNode(const Release::pointer& release, Session& dbSession, const
if (id3) if (id3)
{ {
albumNode.setAttribute("name", release->getName()); albumNode.setAttribute("name", release->getName());
albumNode.setAttribute("songCount", std::to_string(release->getTracksCount())); albumNode.setAttribute("songCount", release->getTracksCount());
albumNode.setAttribute("duration", std::to_string(std::chrono::duration_cast<std::chrono::seconds>(release->getDuration()).count())); albumNode.setAttribute("duration", std::to_string(std::chrono::duration_cast<std::chrono::seconds>(release->getDuration()).count()));
} }
else else
@@ -387,7 +387,7 @@ releaseToResponseNode(const Release::pointer& release, Session& dbSession, const
albumNode.setAttribute("coverArt", IdToString({Id::Type::Release, release.id()})); albumNode.setAttribute("coverArt", IdToString({Id::Type::Release, release.id()}));
auto releaseYear {release->getReleaseYear()}; auto releaseYear {release->getReleaseYear()};
if (releaseYear) if (releaseYear)
albumNode.setAttribute("year", std::to_string(*releaseYear)); albumNode.setAttribute("year", *releaseYear);
auto artists {release->getReleaseArtists()}; auto artists {release->getReleaseArtists()};
if (artists.empty()) if (artists.empty())
@@ -443,7 +443,7 @@ artistToResponseNode(const User::pointer& user, const Artist::pointer& artist, b
artistNode.setAttribute("name", artist->getName()); artistNode.setAttribute("name", artist->getName());
if (id3) if (id3)
artistNode.setAttribute("albumCount", std::to_string(artist->getReleaseCount())); artistNode.setAttribute("albumCount", artist->getReleaseCount());
if (user->hasStarredArtist(artist)) if (user->hasStarredArtist(artist))
artistNode.setAttribute("starred", reportedStarredDate); artistNode.setAttribute("starred", reportedStarredDate);
@@ -458,8 +458,8 @@ clusterToResponseNode(const Cluster::pointer& cluster)
Response::Node clusterNode; Response::Node clusterNode;
clusterNode.setValue(cluster->getName()); clusterNode.setValue(cluster->getName());
clusterNode.setAttribute("songCount", std::to_string(cluster->getTracksCount())); clusterNode.setAttribute("songCount", cluster->getTracksCount());
clusterNode.setAttribute("albumCount", std::to_string(cluster->getReleasesCount())); clusterNode.setAttribute("albumCount", cluster->getReleasesCount());
return clusterNode; return clusterNode;
} }
@@ -1208,8 +1208,8 @@ tracklistToResponseNode(const TrackList::pointer& tracklist, Session&)
playlistNode.setAttribute("id", IdToString({Id::Type::Playlist, tracklist.id()})); playlistNode.setAttribute("id", IdToString({Id::Type::Playlist, tracklist.id()}));
playlistNode.setAttribute("name", tracklist->getName()); playlistNode.setAttribute("name", tracklist->getName());
playlistNode.setAttribute("songCount", std::to_string(tracklist->getCount())); playlistNode.setAttribute("songCount", tracklist->getCount());
playlistNode.setAttribute("duration", std::to_string(std::chrono::duration_cast<std::chrono::seconds>(tracklist->getDuration()).count())); playlistNode.setAttribute("duration", std::chrono::duration_cast<std::chrono::seconds>(tracklist->getDuration()).count());
playlistNode.setAttribute("public", tracklist->isPublic() ? "true" : "false"); playlistNode.setAttribute("public", tracklist->isPublic() ? "true" : "false");
playlistNode.setAttribute("created", ""); playlistNode.setAttribute("created", "");
playlistNode.setAttribute("owner", tracklist->getUser()->getLoginName()); playlistNode.setAttribute("owner", tracklist->getUser()->getLoginName());
+41 -10
View File
@@ -46,6 +46,15 @@ ResponseFormatToMimeType(ResponseFormat format)
void void
Response::Node::setValue(std::string_view value) Response::Node::setValue(std::string_view value)
{
if (!_children.empty() || !_childrenArrays.empty())
throw LmsException {"Node already has children"};
_value = std::string {value};
}
void
Response::Node::setValue(long long value)
{ {
if (!_children.empty() || !_childrenArrays.empty()) if (!_children.empty() || !_childrenArrays.empty())
throw LmsException {"Node already has children"}; throw LmsException {"Node already has children"};
@@ -55,6 +64,12 @@ Response::Node::setValue(std::string_view value)
void void
Response::Node::setAttribute(std::string_view key, std::string_view value) Response::Node::setAttribute(std::string_view key, std::string_view value)
{
_attributes[std::string {key}] = std::string {value};
}
void
Response::Node::setAttribute(std::string_view key, long long value)
{ {
_attributes[std::string {key}] = value; _attributes[std::string {key}] = value;
} }
@@ -62,7 +77,7 @@ Response::Node::setAttribute(std::string_view key, std::string_view value)
void void
Response::Node::addChild(const std::string& key, Node node) Response::Node::addChild(const std::string& key, Node node)
{ {
if (!_value.empty()) if (_value.valueless_by_exception())
throw LmsException {"Node already has a value"}; throw LmsException {"Node already has a value"};
_children[key].emplace_back(std::move(node)); _children[key].emplace_back(std::move(node));
@@ -71,7 +86,7 @@ Response::Node::addChild(const std::string& key, Node node)
void void
Response::Node::addArrayChild(const std::string& key, Node node) Response::Node::addArrayChild(const std::string& key, Node node)
{ {
if (!_value.empty()) if (_value.valueless_by_exception())
throw LmsException {"Node already has a value"}; throw LmsException {"Node already has a value"};
_childrenArrays[key].emplace_back(std::move(node)); _childrenArrays[key].emplace_back(std::move(node));
@@ -160,11 +175,19 @@ Response::writeXML(std::ostream& os)
boost::property_tree::ptree res; boost::property_tree::ptree res;
for (auto itAttribute : node._attributes) for (auto itAttribute : node._attributes)
res.put("<xmlattr>." + itAttribute.first, itAttribute.second);
if (!node._value.empty())
{ {
res.put_value(node._value); if (std::holds_alternative<std::string>(itAttribute.second))
res.put("<xmlattr>." + itAttribute.first, std::get<std::string>(itAttribute.second));
else if (std::holds_alternative<long long>(itAttribute.second))
res.put("<xmlattr>." + itAttribute.first, std::get<long long>(itAttribute.second));
}
if (node._value.valueless_by_exception())
{
if (std::holds_alternative<std::string>(node._value))
res.put_value(std::get<std::string>(node._value));
else if (std::holds_alternative<long long>(node._value))
res.put_value(std::get<long long>(node._value));
} }
else else
{ {
@@ -200,11 +223,19 @@ Response::writeJSON(std::ostream& os)
Json::Object res; Json::Object res;
for (auto itAttribute : node._attributes) for (auto itAttribute : node._attributes)
res[itAttribute.first] = Json::Value {itAttribute.second};
if (!node._value.empty())
{ {
res["value"] = Json::Value {node._value}; if (std::holds_alternative<std::string>(itAttribute.second))
res[itAttribute.first] = Json::Value {std::get<std::string>(itAttribute.second)};
else if (std::holds_alternative<long long>(itAttribute.second))
res[itAttribute.first] = Json::Value {std::get<long long>(itAttribute.second)};
}
if (node._value.valueless_by_exception())
{
if (std::holds_alternative<std::string>(node._value))
res["value"] = Json::Value {std::get<std::string>(node._value)};
else if (std::holds_alternative<long long>(node._value))
res["value"] = Json::Value {std::get<long long>(node._value)};
} }
else else
{ {
+5 -2
View File
@@ -21,6 +21,7 @@
#include <map> #include <map>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <variant>
#include <vector> #include <vector>
@@ -181,9 +182,11 @@ class Response
{ {
public: public:
void setAttribute(std::string_view key, std::string_view value); void setAttribute(std::string_view key, std::string_view value);
void setAttribute(std::string_view key, long long value);
// A Node has either a value or some children // A Node has either a value or some children
void setValue(std::string_view value); void setValue(std::string_view value);
void setValue(long long value);
Node& createChild(const std::string& key); Node& createChild(const std::string& key);
Node& createArrayChild(const std::string& key); Node& createArrayChild(const std::string& key);
@@ -192,8 +195,8 @@ class Response
private: private:
friend class Response; friend class Response;
std::map<std::string, std::string> _attributes; std::map<std::string, std::variant<std::string, long long>> _attributes;
std::string _value; std::variant<std::string, long long> _value;
std::map<std::string, std::vector<Node>> _children; std::map<std::string, std::vector<Node>> _children;
std::map<std::string, std::vector<Node>> _childrenArrays; std::map<std::string, std::vector<Node>> _childrenArrays;
}; };