From 16f0c1aed051e46f49019c5cdd54f92b89ff58f9 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 8 Aug 2019 21:31:13 -0400 Subject: [PATCH] Minor changes to metadata reading --- Controllers/Managers/SongManager.cs | 74 +++++---------- Controllers/Utilities/MetadataRetriever.cs | 102 +-------------------- Controllers/v1/SongController.cs | 20 ++-- Database/Repositories/SongRepository.cs | 25 ++--- Libs/include/metadata_retriever.h | 2 - Libs/include/models.h | 11 ++- Libs/src/metadata_retriever.cpp | 67 ++------------ 7 files changed, 54 insertions(+), 247 deletions(-) diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index 2b45a22..7b4a4fc 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -58,11 +58,6 @@ namespace Icarus.Controllers.Managers public SongManager() { } - - public SongManager(Song song) - { - _song = song; - } public SongManager(IConfiguration config) { _config = config; @@ -232,6 +227,7 @@ namespace Icarus.Controllers.Managers Album = song.AlbumTitle, Genre = song.Genre, Year = song.Year.Value, + Duration = song.Duration, SongPath = song.SongPath }; } @@ -245,6 +241,7 @@ namespace Icarus.Controllers.Managers AlbumTitle = song.Album, Genre = song.Genre, Year = song.Year, + Duration = song.Duration, SongPath = song.SongPath }; } @@ -261,54 +258,22 @@ namespace Icarus.Controllers.Managers } private async Task SaveSongTemp(IFormFile songFile, string filePath) { - var song = new Song(); - - using (var filestream = new FileStream(filePath, FileMode.Create)) { _logger.Info("Saving song to temporary directory"); await songFile.CopyToAsync(filestream); } - //MetadataRetriever meta = new MetadataRetriever(); - //song = meta.RetrieveMetaData(filePath); - Console.WriteLine("try"); - unsafe - { - Song *sng = new Sng(); + var sng = new Sng(); MetadataRetriever.retrieve_metadata(ref sng, filePath); - Console.WriteLine("Hmmmm"); - Console.WriteLine($"sng title {sng.Title}"); - song = ConvertSngToSong(sng); - } + var song = ConvertSngToSong(sng); _logger.Info("Assigning song filename"); song.Filename = songFile.FileName; - Console.WriteLine("what?"); return song; } - /** - private async Task SaveSongToFileSystemTemp(IFormFile song, string filePath) - { - using (var fileStream = new FileStream(filePath, FileMode.Create)) - { - Console.WriteLine("Retrieving song and storing it in memory"); - _logger.Info("Retrieving song and storing it in memory"); - await song.CopyToAsync(fileStream); - Console.WriteLine($"Retrieving metadata of song from filepath {filePath}"); - _logger.Info($"Retrieving metadata of song from filepath {filePath}"); - MetadataRetriever meta = new MetadataRetriever(); - _song = meta.RetrieveMetaData(filePath); - - Console.WriteLine("Assigning song filename"); - _song.Filename = song.FileName; - Console.WriteLine($"Song filename retrieved: {song.FileName}"); - } - } - */ - private bool SongRecordChanged(Song currentSong, Song songUpdates) { var currentTitle = currentSong.Title; @@ -338,23 +303,24 @@ namespace Icarus.Controllers.Managers var info = "Saving Song to DB"; Console.WriteLine(info); _logger.Info(info); + songStore.SaveSong(song); } private void SaveAlbumToDatabase(ref Song song, AlbumRepository albumStore) { _logger.Info("Starting process to save the album record of the song to the database"); - var album = new Album(); - - album.Title = song.AlbumTitle; - album.AlbumArtist = song.Artist; + var album = new Album() + { + Title = song.AlbumTitle, + AlbumArtist = song.Artist + }; if (!albumStore.DoesAlbumExist(song)) { album.SongCount = 1; albumStore.SaveAlbum(album); album = albumStore.GetAlbum(song); - Console.WriteLine($"Album Id {album.AlbumId}"); } else { @@ -371,10 +337,11 @@ namespace Icarus.Controllers.Managers { _logger.Info("Starting process to save the artist record of the song to the database"); - var artist = new Artist(); - - artist.Name = song.Artist; - artist.SongCount = 1; + var artist = new Artist + { + Name = song.Artist, + SongCount = 1 + }; if (!artistStore.DoesArtistExist(song)) { @@ -794,16 +761,17 @@ namespace Icarus.Controllers.Managers public struct Sng { public int Id; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)] + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)] public string Title; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)] + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)] public string Artist; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)] + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)] public string Album; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)] + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)] public string Genre; public int Year; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)] + public int Duration; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)] public string SongPath; }; #endregion diff --git a/Controllers/Utilities/MetadataRetriever.cs b/Controllers/Utilities/MetadataRetriever.cs index 913dc70..0a0aa5d 100644 --- a/Controllers/Utilities/MetadataRetriever.cs +++ b/Controllers/Utilities/MetadataRetriever.cs @@ -16,12 +16,6 @@ namespace Icarus.Controllers.Utilities private static NLog.Logger _logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); private Song _updatedSong; private string _message; - private string _title; - private string _artist; - private string _album; - private string _genre; - private int _year; - private int _duration; #endregion @@ -39,10 +33,6 @@ namespace Icarus.Controllers.Utilities #endregion - #region Constructors - #endregion - - #region Methods public static void PrintMetadata(Song song) { @@ -70,41 +60,11 @@ namespace Icarus.Controllers.Utilities _logger.Info($"Year: {song.Year}"); _logger.Info($"Duration: {song.Duration}"); } - public Song RetrieveMetaData(string filePath) - { - Song song = new Song(); - - try - { - TagLib.File fileTag = TagLib.File.Create(filePath); - _title = fileTag.Tag.Title; - _artist = string.Join("", fileTag.Tag.Performers); - _album = fileTag.Tag.Album; - _genre = string.Join("", fileTag.Tag.Genres); - _year = (int)fileTag.Tag.Year; - _duration = (int)fileTag.Properties.Duration.TotalSeconds; - - song.Title = _title; - song.Artist = _artist; - song.AlbumTitle = _album; - song.Genre = _genre; - song.Year = _year; - song.Duration = _duration; - } - catch (Exception ex) - { - var msg = ex.Message; - Console.WriteLine("An error occurred in MetadataRetriever"); - Console.WriteLine(msg); - _logger.Error(msg, "An error occurred in MetadataRetriever"); - } - - return song; - } + #region C++ Libs [DllImport("libicarus.so")] - //public static extern Icarus.Controllers.Managers.SongManager.Sng retrieve_metadata(string file_path); public static extern void retrieve_metadata(ref Icarus.Controllers.Managers.SongManager.Sng sng, string file_path); + #endregion public byte[] RetrieveCoverArtBytes(Song song) { @@ -125,28 +85,6 @@ namespace Icarus.Controllers.Utilities return null; } - public void UpdateMetadata(Song song) - { - try - { - Console.WriteLine("Updating song metadata"); - _logger.Info("Updating song metadata"); - var filePath = song.SongPath; - TagLib.File fileTag = TagLib.File.Create(filePath); - fileTag.Tag.Title = song.Title; - fileTag.Tag.Genres = new []{song.Genre}; - fileTag.Save(); - Console.WriteLine("Song metadata updated"); - _logger.Info("Song metadata updated"); - - } - catch (Exception ex) - { - var msg = ex.Message; - Console.WriteLine($"An error occurred: \n{msg}"); - _logger.Error(msg, "An error occurred"); - } - } public void UpdateMetadata(Song updatedSong, Song oldSong) { try @@ -253,42 +191,6 @@ namespace Icarus.Controllers.Utilities SongPath = song.SongPath }; } - private void PrintMetadata() - { - Console.WriteLine("\n\nMetadata of the song:"); - Console.WriteLine($"Title: {_title}"); - Console.WriteLine($"Artist: {_artist}"); - Console.WriteLine($"Album: {_album}"); - Console.WriteLine($"Genre: {_genre}"); - Console.WriteLine($"Year: {_year}"); - Console.WriteLine($"Duration: {_duration}\n\n"); - - _logger.Info("Metadata of the song"); - _logger.Info($"Title: {_title}"); - _logger.Info($"Artist: {_artist}"); - _logger.Info($"Album: {_album}"); - _logger.Info($"Genre: {_genre}"); - _logger.Info($"Year: {_year}"); - _logger.Info($"Duration: {_duration}"); - } - private void PrintMetadata(Song song, string message) - { - Console.WriteLine($"\n\n{message}"); - Console.WriteLine($"Title: {song.Title}"); - Console.WriteLine($"Artist: {song.Artist}"); - Console.WriteLine($"Album: {song.Album}"); - Console.WriteLine($"Genre: {song.Genre}"); - Console.WriteLine($"Year: {song.Year}"); - Console.WriteLine($"Duration: {song.Duration}\n\n"); - - _logger.Info(message); - _logger.Info($"Title: {_title}"); - _logger.Info($"Artist: {_artist}"); - _logger.Info($"Album: {_album}"); - _logger.Info($"Genre: {_genre}"); - _logger.Info($"Year: {_year}"); - _logger.Info($"Duration: {_duration}"); - } private SortedDictionary CheckSongValues(Song song) { diff --git a/Controllers/v1/SongController.cs b/Controllers/v1/SongController.cs index 8ab7569..f3b9d7a 100644 --- a/Controllers/v1/SongController.cs +++ b/Controllers/v1/SongController.cs @@ -45,11 +45,11 @@ namespace Icarus.Controllers.V1 [Authorize("read:song_details")] public IActionResult Get() { - List songs = new List(); + var songs = new List(); Console.WriteLine("Attemtping to retrieve songs"); _logger.LogInformation("Attempting to retrieve songs"); - SongRepository context = HttpContext.RequestServices + var context = HttpContext.RequestServices .GetService(typeof(SongRepository)) as SongRepository; songs = context.GetAllSongs(); @@ -64,14 +64,12 @@ namespace Icarus.Controllers.V1 [Authorize("read:song_details")] public IActionResult Get(int id) { - SongRepository context = HttpContext.RequestServices + var context = HttpContext.RequestServices .GetService(typeof(SongRepository)) as SongRepository; - Song song = new Song { Id = id }; + var song = new Song { Id = id }; song = context.GetSong(song); - Console.WriteLine("Here"); - if (song.Id != 0) return Ok(song); else @@ -82,19 +80,19 @@ namespace Icarus.Controllers.V1 [HttpPut("{id}")] public IActionResult Put(int id, [FromBody] Song song) { - SongRepository context = HttpContext.RequestServices + var context = HttpContext.RequestServices .GetService(typeof(SongRepository)) as SongRepository; - ArtistRepository artistStore = HttpContext.RequestServices + var artistStore = HttpContext.RequestServices .GetService(typeof(ArtistRepository)) as ArtistRepository; - AlbumRepository albumStore = HttpContext.RequestServices + var albumStore = HttpContext.RequestServices .GetService(typeof(AlbumRepository)) as AlbumRepository; - GenreRepository genreStore = HttpContext.RequestServices + var genreStore = HttpContext.RequestServices .GetService(typeof(GenreRepository)) as GenreRepository; - YearRepository yearStore = HttpContext.RequestServices + var yearStore = HttpContext.RequestServices .GetService(typeof(YearRepository)) as YearRepository; song.Id = id; diff --git a/Database/Repositories/SongRepository.cs b/Database/Repositories/SongRepository.cs index e628b4c..95dfa7c 100644 --- a/Database/Repositories/SongRepository.cs +++ b/Database/Repositories/SongRepository.cs @@ -8,15 +8,7 @@ using Icarus.Models; namespace Icarus.Database.Repositories { public class SongRepository : BaseRepository - { - #region Fields - #endregion - - - #region Properties - #endregion - - + { #region Constructors public SongRepository(string connectionString) { @@ -165,7 +157,7 @@ namespace Icarus.Database.Repositories public List GetAllSongs() { - List songs = new List(); + var songs = new List(); try { @@ -174,10 +166,10 @@ namespace Icarus.Database.Repositories { conn.Open(); var query = "SELECT * FROM Song"; - Console.WriteLine("ffff"); - MySqlCommand cmd = new MySqlCommand(query, conn); - using (var reader = cmd.ExecuteReader()) - songs = ParseData(reader); + + using (var cmd = new MySqlCommand(query, conn)) + using (var reader = cmd.ExecuteReader()) + songs = ParseData(reader); } } catch (Exception ex) @@ -269,7 +261,7 @@ namespace Icarus.Database.Repositories private List ParseData(MySqlDataReader reader) { - List songs = new List(); + var songs = new List(); while (reader.Read()) { songs.Add(new Song @@ -296,7 +288,7 @@ namespace Icarus.Database.Repositories private Song ParseSingleData(MySqlDataReader reader) { - Song song = new Song(); + var song = new Song(); while (reader.Read()) { @@ -318,7 +310,6 @@ namespace Icarus.Database.Repositories return song; } - #endregion } } diff --git a/Libs/include/metadata_retriever.h b/Libs/include/metadata_retriever.h index 4d84c82..80e3dd4 100644 --- a/Libs/include/metadata_retriever.h +++ b/Libs/include/metadata_retriever.h @@ -1,5 +1,3 @@ #include #include "models.h" - -void fetch_metadata(Song&, const char*); diff --git a/Libs/include/models.h b/Libs/include/models.h index a943fd5..e5e5ebc 100644 --- a/Libs/include/models.h +++ b/Libs/include/models.h @@ -2,10 +2,11 @@ struct Song { int Id; - char *Title; - char *Artist; - char *Album; - char *Genre; + char Title[1024]; + char Artist[1024]; + char Album[1024]; + char Genre[1024]; int Year; - char *SongPath; + int Duration; + char SongPath[1024]; }; diff --git a/Libs/src/metadata_retriever.cpp b/Libs/src/metadata_retriever.cpp index ec465ba..ca458cd 100644 --- a/Libs/src/metadata_retriever.cpp +++ b/Libs/src/metadata_retriever.cpp @@ -6,74 +6,23 @@ #include "metadata_retriever.h" -void fetch_metadata(Song &sng, const char *song_path) -{ - //Song sng; - std::cout<<"extracting metadata from: "<title().toCString(); - //sng->Title = (char*)file.tag()->title().toCString(); - std::cout<<"Title: "<Title<artist().toCString(); - //sng->Artist = (char*)file.tag()->artist().toCString(); - //std::cout<<"0"<album().toCString(); - //sng->Album = (char*)file.tag()->album().toCString(); - //std::cout<<"0"<genre().toCString(); - //sng->Genre = (char*)file.tag()->genre().toCString(); - //std::cout<<"0"<year(); - //sng->Year = file.tag()->year(); - //std::cout<<"0"<SongPath = (char*)song_path; - //std::cout<<"0"<title().toCString(); - sng->Title = (char*)file.tag()->title().toCString(); - //strcpy(sng->Title, file.tag()->title().toCString()); - //std::cout<<"Title: "<Title<artist().toCString(); - sng->Artist = (char*)file.tag()->artist().toCString(); - //std::cout<<"0"<album().toCString(); - sng->Album = (char*)file.tag()->album().toCString(); - //std::cout<<"0"<genre().toCString(); - sng->Genre = (char*)file.tag()->genre().toCString(); - //std::cout<<"0"<year(); + + strcpy(sng->Title, file.tag()->title().toCString()); + strcpy(sng->Artist, file.tag()->artist().toCString()); + strcpy(sng->Album, file.tag()->album().toCString()); + strcpy(sng->Genre, file.tag()->genre().toCString()); sng->Year = file.tag()->year(); - //std::cout<<"0"<SongPath = (char*)song_path; - //std::cout<<"res"<Duration = file.audioProperties()->lengthInSeconds(); + strcpy(sng->SongPath, song_path); } }