From c0d2891d86a89373c414a9e83883d29d5fc2d20f Mon Sep 17 00:00:00 2001 From: amazing-username Date: Tue, 30 Apr 2019 01:49:43 +0000 Subject: [PATCH] Switched to the TagLib library for stripping metadata. Included TODO for updating a song's metadata for both the database and the tag of the MP3 file #36 --- Controllers/Managers/SongManager.cs | 5 +- Controllers/SongController.cs | 16 ++--- Controllers/SongDataController.cs | 5 -- Controllers/Utilities/MetadataRetriever.cs | 73 ++++++++++++++++++++++ Models/SongResult.cs | 14 +++++ 5 files changed, 95 insertions(+), 18 deletions(-) create mode 100644 Controllers/Utilities/MetadataRetriever.cs create mode 100644 Models/SongResult.cs diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index 7767d15..607df75 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -218,7 +218,6 @@ namespace Icarus.Controllers.Managers _song.SongPath = filePath; Console.WriteLine($"Writing song to the directory: {filePath}"); - Console.WriteLine("Song successfully saved"); } } catch (Exception ex) @@ -404,7 +403,9 @@ namespace Icarus.Controllers.Managers Console.WriteLine("Retrieving song and storing it in memory"); await song.CopyToAsync(fileStream); Console.WriteLine($"Retrieving metadata of song from filepath {filePath}"); - _song = RetrieveMetaData(filePath); + MetadataRetriever meta = new MetadataRetriever(); + _song = meta.RetrieveMetaData(filePath); + //_song = RetrieveMetaData(filePath); Console.WriteLine("Assigning song filename"); _song.Filename = song.FileName; Console.WriteLine($"Song filename retrieved: {song.FileName}"); diff --git a/Controllers/SongController.cs b/Controllers/SongController.cs index 96f754c..b806675 100644 --- a/Controllers/SongController.cs +++ b/Controllers/SongController.cs @@ -68,20 +68,14 @@ namespace Icarus.Controllers return song; } - [HttpPost] - public void Post([FromBody] Song song) - { - MusicStoreContext context = HttpContext - .RequestServices - .GetService(typeof(MusicStoreContext)) - as MusicStoreContext; - - context.SaveSong(song); - } [HttpPut("{id}")] - public void Put(int id, [FromBody] Song song) + public IActionResult Put(int id, [FromBody] Song song) { + // TODO: Implement updating of song metadata + SongResult songResult = new SongResult(); + + return Ok(songResult); } [HttpDelete("{id}")] diff --git a/Controllers/SongDataController.cs b/Controllers/SongDataController.cs index 336a35f..95f21c1 100644 --- a/Controllers/SongDataController.cs +++ b/Controllers/SongDataController.cs @@ -89,11 +89,6 @@ namespace Icarus.Controllers } } - [HttpPut("{id}")] - public void Put(int id, [FromBody] SongData song) - { - } - [HttpDelete("{id}")] [Authorize("delete:songs")] public void Delete(int id) diff --git a/Controllers/Utilities/MetadataRetriever.cs b/Controllers/Utilities/MetadataRetriever.cs new file mode 100644 index 0000000..4ce5b5e --- /dev/null +++ b/Controllers/Utilities/MetadataRetriever.cs @@ -0,0 +1,73 @@ +using System; + +using TagLib; + +using Icarus.Models; + +namespace Icarus.Controllers.Utilities +{ + public class MetadataRetriever + { + #region Fields + private string _title; + private string _artist; + private string _album; + private string _genre; + private int _year; + private int _duration; + #endregion + + + #region Properties + #endregion + + + #region Constructors + #endregion + + + #region Methods + 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.Artists); + _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.Album = _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); + } + + return song; + } + + 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"); + } + #endregion + } +} diff --git a/Models/SongResult.cs b/Models/SongResult.cs new file mode 100644 index 0000000..8918c0e --- /dev/null +++ b/Models/SongResult.cs @@ -0,0 +1,14 @@ +using System; + +using Newtonsoft.Json; + +namespace Icarus.Models +{ + public class SongResult + { + [JsonProperty("message")] + public string Message { get; set; } + [JsonProperty("song_title")] + public string SongTitle { get; set; } + } +}