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

This commit is contained in:
amazing-username
2019-05-20 20:02:19 -04:00
parent e30f80d7d5
commit 346609b7bd
3 changed files with 180 additions and 88 deletions
+13
View File
@@ -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
}
}
+58 -7
View File
@@ -8,7 +8,7 @@ using Icarus.Models;
namespace Icarus.Controllers.Managers namespace Icarus.Controllers.Managers
{ {
public class DirectoryManager public class DirectoryManager : BaseManager
{ {
#region Fields #region Fields
private IConfiguration _config; private IConfiguration _config;
@@ -34,6 +34,11 @@ namespace Icarus.Controllers.Managers
_song = song; _song = song;
Initialize(); Initialize();
} }
public DirectoryManager(IConfiguration config)
{
_config = config;
Initialize();
}
#endregion #endregion
@@ -83,27 +88,73 @@ namespace Icarus.Controllers.Managers
} }
} }
void Initialize() public string GenerateSongPath(Song song)
{
_logger.Info("Generating song path");
var songPath = string.Empty;
var artistPath = ArtistDirectory(song);
var albumPath = AlbumDirectory(song);
if (!Directory.Exists(artistPath))
{
_logger.Info("Artist path does not exist");
Directory.CreateDirectory(artistPath);
_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<string>("RootMusicPath"); _rootSongDirectory = _config.GetValue<string>("RootMusicPath");
} }
bool IsDirectoryEmpty(string path) private bool IsDirectoryEmpty(string path)
{ {
return !Directory.EnumerateFileSystemEntries(path).Any(); return !Directory.EnumerateFileSystemEntries(path).Any();
} }
string AlbumDirectory() private string AlbumDirectory()
{ {
string directory = _rootSongDirectory; string directory = _rootSongDirectory;
directory += $"{_song.Artist}//{_song.Album}//"; directory += $@"{_song.Artist}/{_song.AlbumTitle}/";
return directory; return directory;
} }
string ArtistDirectory() private string AlbumDirectory(Song song)
{ {
var directory = _rootSongDirectory; var directory = _rootSongDirectory;
directory += $"{_song.Artist}//"; 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; return directory;
} }
+30 -2
View File
@@ -21,10 +21,9 @@ using Icarus.Models.Context;
namespace Icarus.Controllers.Managers namespace Icarus.Controllers.Managers
{ {
public class SongManager public class SongManager : BaseManager
{ {
#region Fields #region Fields
private static Logger _logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
private MySqlConnection _conn; private MySqlConnection _conn;
private MySqlCommand _cmd; private MySqlCommand _cmd;
private MySqlDataAdapter _dataDump; private MySqlDataAdapter _dataDump;
@@ -972,6 +971,7 @@ namespace Icarus.Controllers.Managers
private void UpdateSongInDatabase(Song oldSongRecord, Song newSongRecord, MusicStoreContext songStore) private void UpdateSongInDatabase(Song oldSongRecord, Song newSongRecord, MusicStoreContext songStore)
{ {
var updatedSongRecord = oldSongRecord; var updatedSongRecord = oldSongRecord;
var artistOrAlbumChanged = false;
if (!SongRecordChanged(updatedSongRecord, newSongRecord)) if (!SongRecordChanged(updatedSongRecord, newSongRecord))
{ {
@@ -992,10 +992,12 @@ namespace Icarus.Controllers.Managers
if (!string.IsNullOrEmpty(newSongRecord.AlbumTitle)) if (!string.IsNullOrEmpty(newSongRecord.AlbumTitle))
{ {
updatedSongRecord.AlbumTitle = newSongRecord.AlbumTitle; updatedSongRecord.AlbumTitle = newSongRecord.AlbumTitle;
artistOrAlbumChanged = true;
} }
if (!string.IsNullOrEmpty(newSongRecord.Artist)) if (!string.IsNullOrEmpty(newSongRecord.Artist))
{ {
updatedSongRecord.Artist = newSongRecord.Artist; updatedSongRecord.Artist = newSongRecord.Artist;
artistOrAlbumChanged = true;
} }
if (!string.IsNullOrEmpty(newSongRecord.Genre)) if (!string.IsNullOrEmpty(newSongRecord.Genre))
{ {
@@ -1011,6 +1013,32 @@ namespace Icarus.Controllers.Managers
Console.WriteLine("Updated song values\n"); Console.WriteLine("Updated song values\n");
MetadataRetriever.PrintMetadata(updatedSongRecord); 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)) if (songStore.DoesSongExist(newSongRecord))
{ {
songStore.UpdateSong(updatedSongRecord); songStore.UpdateSong(updatedSongRecord);