From 1e8f14430e749c6afdcaef7c3f8ec1080a9610a8 Mon Sep 17 00:00:00 2001 From: casey langen Date: Mon, 15 Jun 2020 10:48:37 -0700 Subject: [PATCH] Fixed directory browsing with respect to custom indexer source plugins. --- src/core/library/track/IndexerTrack.cpp | 11 ++++++++++- src/core/sdk/Filesystem.h | 15 +++++++++++++++ src/plugins/gmedecoder/GmeIndexerSource.cpp | 3 +++ .../libopenmptdecoder/OpenMptIndexerSource.cpp | 8 +++++++- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/core/library/track/IndexerTrack.cpp b/src/core/library/track/IndexerTrack.cpp index a2675274c..6b430c222 100644 --- a/src/core/library/track/IndexerTrack.cpp +++ b/src/core/library/track/IndexerTrack.cpp @@ -845,7 +845,16 @@ bool IndexerTrack::Save(db::Connection &dbConnection, std::string libraryDirecto } ProcessNonStandardMetadata(dbConnection); - SaveDirectory(dbConnection, this->GetString("filename")); + + /* sometimes indexer source plugins save the 'filename' field with a custom + encoded URI. in these cases the plugin can populate a 'directory' field + with the actual directory, if one exists. otherwise, we'll just extract + the directory from the 'filename'. */ + std::string path = this->GetString("directory").size() ? + this->GetString("directory") : this->GetString("filename"); + + SaveDirectory(dbConnection, path); + SaveReplayGain(dbConnection); return true; diff --git a/src/core/sdk/Filesystem.h b/src/core/sdk/Filesystem.h index ad6c6226b..f01655799 100644 --- a/src/core/sdk/Filesystem.h +++ b/src/core/sdk/Filesystem.h @@ -119,6 +119,21 @@ namespace musik { namespace core { namespace sdk { namespace fs { return ""; } + template + static inline std::string getDirectory(const String& filename) { + std::string canonicalized = canonicalizePath(filename); +#ifdef WIN32 + size_t pos = canonicalized.find_last_of("\\"); +#else + size_t pos = canonicalized.find_last_of("/"); +#endif + if (pos != std::string::npos) { + std::string result = canonicalized.substr(0, pos + 1); + return result; + } + return ""; + } + template void scanDirectory( const std::string& path, diff --git a/src/plugins/gmedecoder/GmeIndexerSource.cpp b/src/plugins/gmedecoder/GmeIndexerSource.cpp index ca219b859..03b26c906 100644 --- a/src/plugins/gmedecoder/GmeIndexerSource.cpp +++ b/src/plugins/gmedecoder/GmeIndexerSource.cpp @@ -182,6 +182,8 @@ void GmeIndexerSource::UpdateMetadata( KEY_DEFAULT_TRACK_LENGTH, DEFAULT_TRACK_LENGTH)); + const std::string directory = fs::getDirectory(fn); + for (int i = 0; i < gme_track_count(data); i++) { const std::string externalId = indexer::createExternalId(EXTERNAL_ID_PREFIX, fn, i); const std::string trackNum = std::to_string(i + 1); @@ -190,6 +192,7 @@ void GmeIndexerSource::UpdateMetadata( auto track = indexer->CreateWriter(); track->SetValue("filename", externalId.c_str()); + track->SetValue("directory", directory.c_str()); track->SetValue("filetime", modifiedTimeStr.c_str()); track->SetValue("track", trackNum.c_str()); diff --git a/src/plugins/libopenmptdecoder/OpenMptIndexerSource.cpp b/src/plugins/libopenmptdecoder/OpenMptIndexerSource.cpp index a865bf65c..a3752ec43 100644 --- a/src/plugins/libopenmptdecoder/OpenMptIndexerSource.cpp +++ b/src/plugins/libopenmptdecoder/OpenMptIndexerSource.cpp @@ -166,6 +166,7 @@ void OpenMptIndexerSource::UpdateMetadata( nullptr, nullptr, nullptr, nullptr, nullptr); if (module) { + std::string directory = fs::getDirectory(std::string(fn)); size_t count = openmpt_module_get_num_subsongs(module); const char* keys = openmpt_module_get_metadata_keys(module); if (count > 0) { @@ -179,6 +180,9 @@ void OpenMptIndexerSource::UpdateMetadata( std::string album = readMetadataValue(module, "container_long"); if (!album.size()) { album = readMetadataValue(module, "container").c_str(); + if (!album.size()) { + album = "[unknown mod album]"; + } } std::string title = readMetadataValue(module, "title"); @@ -191,10 +195,12 @@ void OpenMptIndexerSource::UpdateMetadata( artist = "[unknown mod artist]"; } - const std::string duration = std::to_string(openmpt_module_get_duration_seconds(module)); + const std::string duration = std::to_string( + openmpt_module_get_duration_seconds(module)); auto track = indexer->CreateWriter(); track->SetValue("filename", externalId.c_str()); + track->SetValue("directory", directory.c_str()); track->SetValue("filetime", modifiedTimeStr.c_str()); track->SetValue("track", trackNum.c_str()); track->SetValue("album", album.c_str());