From 346609b7bd7e70c19e5d08b991ea1cc17c6ad907 Mon Sep 17 00:00:00 2001 From: amazing-username Date: Mon, 20 May 2019 20:02:19 -0400 Subject: [PATCH] Fixed bug where the album directory not being created when uploading a song. Working on moving the song to a new path when the artist or album has been changed. Running into issue with only one process is permitted to have access to the file. #43 and #37 --- Controllers/Managers/BaseManagers.cs | 13 ++ Controllers/Managers/DirectoryManager.cs | 223 ++++++++++++++--------- Controllers/Managers/SongManager.cs | 32 +++- 3 files changed, 180 insertions(+), 88 deletions(-) create mode 100644 Controllers/Managers/BaseManagers.cs diff --git a/Controllers/Managers/BaseManagers.cs b/Controllers/Managers/BaseManagers.cs new file mode 100644 index 0000000..5fda4a5 --- /dev/null +++ b/Controllers/Managers/BaseManagers.cs @@ -0,0 +1,13 @@ +using System; + +using NLog; + +namespace Icarus.Controllers.Managers +{ + public class BaseManager + { + #region Fields + protected static Logger _logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); + #endregion + } +} diff --git a/Controllers/Managers/DirectoryManager.cs b/Controllers/Managers/DirectoryManager.cs index 7081ddd..567349d 100644 --- a/Controllers/Managers/DirectoryManager.cs +++ b/Controllers/Managers/DirectoryManager.cs @@ -8,105 +8,156 @@ using Icarus.Models; namespace Icarus.Controllers.Managers { - public class DirectoryManager - { - #region Fields - private IConfiguration _config; - private Song _song; - private string _rootSongDirectory; - private string _songDirectory; - #endregion + public class DirectoryManager : BaseManager + { + #region Fields + private IConfiguration _config; + private Song _song; + private string _rootSongDirectory; + private string _songDirectory; + #endregion - #region Properties - public string SongDirectory - { - get => _songDirectory; - set => _songDirectory = value; - } - #endregion - - - #region Constructors - public DirectoryManager(IConfiguration config, Song song) - { - _config = config; - _song = song; - Initialize(); - } - #endregion - - - #region Methods - public void CreateDirectory() - { - try - { - _songDirectory = AlbumDirectory(); - - if (!Directory.Exists(_songDirectory)) + #region Properties + public string SongDirectory { - Directory.CreateDirectory(_songDirectory); - Console.WriteLine("The directory has been created"); + get => _songDirectory; + set => _songDirectory = value; } + #endregion - Console.WriteLine($"The song will be saved in the following" + + #region Constructors + public DirectoryManager(IConfiguration config, Song song) + { + _config = config; + _song = song; + Initialize(); + } + public DirectoryManager(IConfiguration config) + { + _config = config; + Initialize(); + } + #endregion + + + #region Methods + public void CreateDirectory() + { + try + { + _songDirectory = AlbumDirectory(); + + if (!Directory.Exists(_songDirectory)) + { + Directory.CreateDirectory(_songDirectory); + Console.WriteLine("The directory has been created"); + } + + + Console.WriteLine($"The song will be saved in the following" + $" directory: {_songDirectory}"); - } - catch (Exception ex) - { - Console.WriteLine($"An error occurred {ex.Message}"); - } - } - public void DeleteEmptyDirectories() - { - try - { - var albumDirectory = AlbumDirectory(); - var artistDirectory = ArtistDirectory(); - if (IsDirectoryEmpty(albumDirectory)) - { - Directory.Delete(albumDirectory); - Console.WriteLine($"directory {albumDirectory} deleted"); + } + catch (Exception ex) + { + Console.WriteLine($"An error occurred {ex.Message}"); + } } - if (IsDirectoryEmpty(artistDirectory)) + public void DeleteEmptyDirectories() { - Directory.Delete(artistDirectory); - Console.WriteLine($"directory {artistDirectory} deleted"); + try + { + var albumDirectory = AlbumDirectory(); + var artistDirectory = ArtistDirectory(); + if (IsDirectoryEmpty(albumDirectory)) + { + Directory.Delete(albumDirectory); + Console.WriteLine($"directory {albumDirectory} deleted"); + } + if (IsDirectoryEmpty(artistDirectory)) + { + Directory.Delete(artistDirectory); + Console.WriteLine($"directory {artistDirectory} deleted"); + } + } + catch (Exception ex) + { + var exMsg = ex.Message; + Console.WriteLine($"An error occurred {exMsg}"); + } } - } - catch (Exception ex) - { - var exMsg = ex.Message; - Console.WriteLine($"An error occurred {exMsg}"); - } - } - void Initialize() - { - _rootSongDirectory = _config.GetValue("RootMusicPath"); - } + public string GenerateSongPath(Song song) + { + _logger.Info("Generating song path"); - bool IsDirectoryEmpty(string path) - { - return !Directory.EnumerateFileSystemEntries(path).Any(); - } + var songPath = string.Empty; + var artistPath = ArtistDirectory(song); + var albumPath = AlbumDirectory(song); - string AlbumDirectory() - { - string directory = _rootSongDirectory; - directory += $"{_song.Artist}//{_song.Album}//"; + if (!Directory.Exists(artistPath)) + { + _logger.Info("Artist path does not exist"); - return directory; - } - string ArtistDirectory() - { - var directory = _rootSongDirectory; - directory += $"{_song.Artist}//"; + Directory.CreateDirectory(artistPath); - return directory; - } - #endregion - } + _logger.Info("Creating artist path"); + } + if (!Directory.Exists(albumPath)) + { + _logger.Info("Album path does not exist"); + + Directory.CreateDirectory(albumPath); + + _logger.Info("Created album path"); + } + + songPath = $@"{albumPath}{song.Filename}"; + + return songPath; + } + + private void Initialize() + { + _rootSongDirectory = _config.GetValue("RootMusicPath"); + } + + private bool IsDirectoryEmpty(string path) + { + return !Directory.EnumerateFileSystemEntries(path).Any(); + } + + private string AlbumDirectory() + { + string directory = _rootSongDirectory; + directory += $@"{_song.Artist}/{_song.AlbumTitle}/"; + + return directory; + } + private string AlbumDirectory(Song song) + { + var directory = _rootSongDirectory; + directory += $@"{song.Artist}/{song.AlbumTitle}/"; + Console.WriteLine($"Album directory {directory}"); + + return directory; + } + private string ArtistDirectory() + { + var directory = _rootSongDirectory; + directory += $@"{_song.Artist}/"; + + return directory; + } + private string ArtistDirectory(Song song) + { + var directory = _rootSongDirectory; + directory += $@"{song.Artist}/"; + Console.WriteLine($"Artist directory {directory}"); + + return directory; + } + #endregion + } } diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index 171c0ea..cddb08d 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -21,10 +21,9 @@ using Icarus.Models.Context; namespace Icarus.Controllers.Managers { - public class SongManager + public class SongManager : BaseManager { #region Fields - private static Logger _logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); private MySqlConnection _conn; private MySqlCommand _cmd; private MySqlDataAdapter _dataDump; @@ -972,6 +971,7 @@ namespace Icarus.Controllers.Managers private void UpdateSongInDatabase(Song oldSongRecord, Song newSongRecord, MusicStoreContext songStore) { var updatedSongRecord = oldSongRecord; + var artistOrAlbumChanged = false; if (!SongRecordChanged(updatedSongRecord, newSongRecord)) { @@ -992,10 +992,12 @@ namespace Icarus.Controllers.Managers if (!string.IsNullOrEmpty(newSongRecord.AlbumTitle)) { updatedSongRecord.AlbumTitle = newSongRecord.AlbumTitle; + artistOrAlbumChanged = true; } if (!string.IsNullOrEmpty(newSongRecord.Artist)) { updatedSongRecord.Artist = newSongRecord.Artist; + artistOrAlbumChanged = true; } if (!string.IsNullOrEmpty(newSongRecord.Genre)) { @@ -1011,6 +1013,32 @@ namespace Icarus.Controllers.Managers Console.WriteLine("Updated song values\n"); MetadataRetriever.PrintMetadata(updatedSongRecord); + if (artistOrAlbumChanged) + { + _logger.Info("Change to song's album or artist"); + + DirectoryManager dirMgr = new DirectoryManager(_config); + var newSongPath = dirMgr.GenerateSongPath(updatedSongRecord); + + Console.WriteLine($"Old song path {updatedSongRecord.SongPath}"); + Console.WriteLine($"New song path {newSongPath}"); + + System.IO.File.Copy(updatedSongRecord.SongPath, newSongPath, true); + + if (!System.IO.File.Exists(newSongPath)) + { + Console.WriteLine("New path does not exist when it should"); + } + if (System.IO.File.Exists(updatedSongRecord.SongPath)) + { + Console.WriteLine("Old path exists when it should"); + } + + updatedSongRecord.SongPath = newSongPath; + + System.IO.File.Delete(updatedSongRecord.SongPath); + } + if (songStore.DoesSongExist(newSongRecord)) { songStore.UpdateSong(updatedSongRecord);