From 1531cad1343c969aa68908671e64de7e237e6000 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 4 Aug 2019 17:08:49 -0400 Subject: [PATCH] Handling song being moved on the c++ side #55 --- Controllers/Managers/DirectoryManager.cs | 2 + Controllers/Managers/SongManager.cs | 82 +++++++++++------------- Controllers/v1/SongStreamController.cs | 2 +- Libs/include/models.h | 1 + Libs/src/directory_manager.cpp | 17 +++++ 5 files changed, 57 insertions(+), 47 deletions(-) diff --git a/Controllers/Managers/DirectoryManager.cs b/Controllers/Managers/DirectoryManager.cs index 6497bdc..e462bf5 100644 --- a/Controllers/Managers/DirectoryManager.cs +++ b/Controllers/Managers/DirectoryManager.cs @@ -100,6 +100,8 @@ namespace Icarus.Controllers.Managers [DllImport("libicarus.so")] public static extern void copy_stock_cover_art(string target_path, string source_path); [DllImport("libicarus.so")] + public static extern void copy_song(string target_path, string source_path); + [DllImport("libicarus.so")] public static extern void delete_cover_art(string cover_art_path, string stock_path); #endregion diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index e3f846b..209361e 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -98,16 +98,12 @@ namespace Icarus.Controllers.Managers oldSongRecord.ArtistId = updatedArtist.ArtistId; var updatedGenre = UpdateGenreInDatabase(oldSongRecord, updatedSong, genreStore); - Console.WriteLine($"Old Genre Id {oldSongRecord.GenreId}"); oldSongRecord.GenreId = updatedGenre.GenreId; - Console.WriteLine($"Updated Genre Id {updatedGenre.GenreId}"); var updatedYear = UpdateYearInDatabase(oldSongRecord, updatedSong, yearStore); oldSongRecord.YearId = updatedYear.YearId; UpdateSongInDatabase(ref oldSongRecord, ref updatedSong, songStore, ref result); - - DeleteEmptyDirectories(ref oldSongRecord, ref updatedSong); } catch (Exception ex) { @@ -329,7 +325,8 @@ namespace Icarus.Controllers.Managers Artist = song.Artist, Album = song.AlbumTitle, Genre = song.Genre, - Year = song.Year.Value + Year = song.Year.Value, + SongPath = song.SongPath }; } @@ -581,6 +578,28 @@ namespace Icarus.Controllers.Managers return true; } + public Song SongCopy(Song song) + { + var updatedSongRecord = new Song + { + Id = song.Id, + Title = song.Title, + Artist = song.Artist, + AlbumTitle = song.AlbumTitle, + Genre = song.Genre, + Year = song.Year, + Duration = song.Duration, + Filename = song.Filename, + SongPath = song.SongPath, + ArtistId = song.ArtistId, + AlbumId = song.AlbumId, + GenreId = song.GenreId, + YearId = song.YearId + }; + + return updatedSongRecord; + } + private Album UpdateAlbumInDatabase(Song oldSongRecord, Song newSongRecord, AlbumRepository albumStore) { var albumRecord = albumStore.GetAlbum(oldSongRecord, true); @@ -769,22 +788,7 @@ namespace Icarus.Controllers.Managers private void UpdateSongInDatabase(ref Song oldSongRecord, ref Song newSongRecord, SongRepository songStore, ref SongResult result) { - var updatedSongRecord = new Song - { - Id = oldSongRecord.Id, - Title = oldSongRecord.Title, - Artist = oldSongRecord.Artist, - AlbumTitle = oldSongRecord.AlbumTitle, - Genre = oldSongRecord.Genre, - Year = oldSongRecord.Year, - Filename = oldSongRecord.Filename, - SongPath = oldSongRecord.SongPath, - ArtistId = oldSongRecord.ArtistId, - AlbumId = oldSongRecord.AlbumId, - GenreId = oldSongRecord.GenreId, - YearId = oldSongRecord.YearId - - }; + var updatedSongRecord = SongCopy(oldSongRecord); var artistOrAlbumChanged = false; if (!SongRecordChanged(oldSongRecord, newSongRecord)) @@ -822,35 +826,20 @@ namespace Icarus.Controllers.Managers { _logger.Info("Change to song's album or artist"); - DirectoryManager dirMgr = new DirectoryManager(_config); - var oldSongPath = updatedSongRecord.SongPath; - var newSongPath = dirMgr.GenerateSongPath(updatedSongRecord); - var filename = updatedSongRecord.Filename; + var rootPath = _config.GetValue("RootMusicPath"); + var strCount = rootPath.Length + updatedSongRecord.Artist.Length + + updatedSongRecord.AlbumTitle.Length + 2; + var updatedPath = new StringBuilder(strCount); - Console.WriteLine($"Old song path {oldSongPath}"); - Console.WriteLine($"New song path {newSongPath}"); + DirectoryManager.create_directory(ConvertSongToSng(updatedSongRecord), + rootPath, updatedPath); - _logger.Info("Copying song to the new path"); + var newSongPath = updatedPath.ToString().Substring(0, strCount) + + updatedSongRecord.Filename; - System.IO.File.Copy(oldSongPath, newSongPath + filename, true); + DirectoryManager.copy_song(newSongPath, updatedSongRecord.SongPath); - _logger.Info("Checking to see if song successfully copied"); - - if (!System.IO.File.Exists(newSongPath + filename)) - { - _logger.Info("Song did not successfully copy"); - - Console.WriteLine("New path does not exist when it should"); - } - - updatedSongRecord.SongPath = newSongPath + filename; - - _logger.Info("Deleting old song path"); - - System.IO.File.Delete(oldSongPath); - - if (System.IO.File.Exists(oldSongPath)) - Console.WriteLine("Old path exists when it should not"); + updatedSongRecord.SongPath = newSongPath; } _logger.Info("Saving song metadata to the database"); @@ -944,6 +933,7 @@ namespace Icarus.Controllers.Managers public string Album; public string Genre; public int Year; + public string SongPath; }; #endregion } diff --git a/Controllers/v1/SongStreamController.cs b/Controllers/v1/SongStreamController.cs index 372d496..49178d6 100644 --- a/Controllers/v1/SongStreamController.cs +++ b/Controllers/v1/SongStreamController.cs @@ -40,7 +40,7 @@ namespace Icarus.Controllers.V1 #region HTTP endpoints [HttpGet("{id}")] [Authorize("stream:songs")] - public async Task Get(int id) + public IActionResult Get(int id) { var songStore= HttpContext.RequestServices .GetService(typeof(SongRepository)) as SongRepository; diff --git a/Libs/include/models.h b/Libs/include/models.h index 0ba7ed6..a943fd5 100644 --- a/Libs/include/models.h +++ b/Libs/include/models.h @@ -7,4 +7,5 @@ struct Song char *Album; char *Genre; int Year; + char *SongPath; }; diff --git a/Libs/src/directory_manager.cpp b/Libs/src/directory_manager.cpp index ec8630a..832287f 100644 --- a/Libs/src/directory_manager.cpp +++ b/Libs/src/directory_manager.cpp @@ -73,6 +73,18 @@ void copy_stock_to_root(const char *target, const std::string buff) std::cout<<"copy finished"<