Code cleanup, removed some unused dependencies, adding more to C++ lib. #54 and #55

This commit is contained in:
kdeng00
2019-08-05 23:47:43 -04:00
parent 1531cad134
commit 02bc671883
6 changed files with 58 additions and 341 deletions
+4 -176
View File
@@ -50,50 +50,6 @@ namespace Icarus.Controllers.Managers
#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 CreateDirectory(Song song)
{
_song = song;
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)
{
var msg = ex.Message;
_logger.Error(msg, "An error occurred");
}
}
#region C++ libs
[DllImport("libicarus.so")]
public static extern void create_directory(SongManager.Sng song, string root_path, StringBuilder created_dir);
@@ -103,104 +59,12 @@ namespace Icarus.Controllers.Managers
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);
[DllImport("libicarus.so")]
public static extern void delete_empty_directories(SongManager.Sng song, string root_path);
[DllImport("libicarus.so")]
public static extern void delete_song_empty_directories(SongManager.Sng song, string root_path);
#endregion
public void DeleteEmptyDirectories()
{
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}");
}
}
public void DeleteEmptyDirectories(Song song)
{
try
{
var albumDirectory = AlbumDirectory(song);
var artistDirectory = ArtistDirectory(song);
if (IsDirectoryEmpty(albumDirectory))
{
Directory.Delete(albumDirectory);
_logger.Info("Album directory deleted");
}
if (IsDirectoryEmpty(artistDirectory))
{
Directory.Delete(artistDirectory);
_logger.Info("Artist directory deleted");
}
}
catch (Exception ex)
{
var msg = ex.Message;
_logger.Error(msg, "An error occurred");
}
}
public string RetrieveAlbumPath(Song song)
{
_logger.Info("Retrieving album song path");
var albumPath = string.Empty;
albumPath = AlbumDirectory(song);
return albumPath;
}
public string RetrieveArtistPath(Song song)
{
_logger.Info("Retrieving artist path");
var artistPath = string.Empty;
artistPath = ArtistDirectory(song);
return artistPath;
}
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;
return songPath;
}
private void Initialize(DirectoryType dirTypes = DirectoryType.Music)
{
switch (dirTypes)
@@ -213,42 +77,6 @@ namespace Icarus.Controllers.Managers
break;
}
}
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
}
}
+3 -161
View File
@@ -117,25 +117,6 @@ namespace Icarus.Controllers.Managers
return result;
}
public bool DeleteSongFromFileSystem(Song songMetaData)
{
bool successful = false;
try
{
var songPath = songMetaData.SongPath;
System.IO.File.Delete(songPath);
successful = true;
DirectoryManager dirMgr = new DirectoryManager(_config, songMetaData);
dirMgr.DeleteEmptyDirectories();
Console.WriteLine("Song successfully deleted");
}
catch (Exception ex)
{
var exMsg = ex.Message;
}
return successful;
}
public void DeleteSong(Song song, SongRepository songStore,
AlbumRepository albumStore, ArtistRepository artistStore,
@@ -144,12 +125,9 @@ namespace Icarus.Controllers.Managers
{
try
{
if (DeleteSongFromFilesystem(song))
{
_logger.Error("Failed to delete the song");
throw new Exception("Failed to delete the song");
}
DirectoryManager.delete_song_empty_directories(
ConvertSongToSng(song),
_config.GetValue<string>("RootMusicPath"));
_logger.Info("Song deleted from the filesystem");
var coverMgr = new CoverArtManager(_config.GetValue<string>(
@@ -168,78 +146,6 @@ namespace Icarus.Controllers.Managers
}
}
public async Task SaveSongToFileSystem(IFormFile song)
{
try
{
Console.WriteLine("Saving song to the filesystem");
var filePath = Path.Combine(_tempDirectoryRoot, song.FileName);
Console.WriteLine("Saving song to the filePath");
await SaveSongToFileSystemTemp(song, filePath);
System.IO.File.Delete(filePath);
DirectoryManager dirMgr = new DirectoryManager(_config, _song);
dirMgr.CreateDirectory();
filePath = dirMgr.SongDirectory;
if (!song.FileName.EndsWith(".mp3"))
filePath += $"{song.FileName}.mp3";
else
filePath += $"{song.FileName}";
Console.WriteLine($"Full path {filePath}");
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await song.CopyToAsync(fileStream);
_song.SongPath = filePath;
Console.WriteLine($"Writing song to the directory: {filePath}");
}
}
catch (Exception ex)
{
var exMsg = ex.Message;
Console.WriteLine($"An error occurred: {exMsg}");
}
}
public async Task SaveSongToFileSystem(IFormFile songFile, SongRepository sStoreContext,
AlbumRepository alStoreContext, ArtistRepository arStoreContext)
{
try
{
_logger.Info("Starting process to save song to the filesystem");
var fileTempPath = Path.Combine(_tempDirectoryRoot, songFile.FileName);
var song = await SaveSongTemp(songFile, fileTempPath);
System.IO.File.Delete(fileTempPath);
DirectoryManager dirMgr = new DirectoryManager(_config, song);
dirMgr.CreateDirectory();
var filePath = dirMgr.SongDirectory;
if (!songFile.FileName.EndsWith(".mp3"))
filePath += $"{songFile.FileName}.mp3";
else
filePath += $"{songFile.FileName}";
_logger.Info($"Absolute song path: {filePath}");
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await (songFile.CopyToAsync(fileStream));
song.SongPath = filePath;
_logger.Info("Song Successfully saved");
}
SaveSongToDatabase(song, sStoreContext, alStoreContext, arStoreContext);
}
catch (Exception ex)
{
var msg = ex.Message;
_logger.Error(msg, "An error occurred");
}
}
public async Task SaveSongToFileSystem(IFormFile songFile, SongRepository songStore,
AlbumRepository albumStore, ArtistRepository artistStore,
GenreRepository genreStore, YearRepository yearStore,
@@ -394,29 +300,6 @@ namespace Icarus.Controllers.Managers
return false;
}
private void DeleteEmptyDirectories(ref Song oldSong, ref Song updatedSong)
{
DirectoryManager mgr = new DirectoryManager(_config);
_logger.Info("Checking to see if there are any directories to delete");
mgr.DeleteEmptyDirectories(oldSong);
}
private void SaveSongToDatabase(Song song, SongRepository songStore, AlbumRepository albumStore,
ArtistRepository artistStore)
{
_logger.Info("Starting process to save the song to the database");
MetadataRetriever.PrintMetadata(song);
SaveAlbumToDatabase(ref song, albumStore);
MetadataRetriever.PrintMetadata(song);
SaveArtistToDatabase(ref song, artistStore);
MetadataRetriever.PrintMetadata(song);
_logger.Info($"Song;\nTitle {song.Title}\nAlbum {song.AlbumTitle}\nAlbum Id {song.AlbumId}\nArtist {song.ArtistId}");
songStore.SaveSong(song);
}
private void SaveSongToDatabase(Song song, SongRepository songStore, AlbumRepository albumStore,
ArtistRepository artistStore, GenreRepository genreStore, YearRepository yearStore)
{
@@ -500,10 +383,6 @@ namespace Icarus.Controllers.Managers
genreStore.SaveGenre(genre);
Console.WriteLine("Going to find genre");
genre = genreStore.GetGenre(song);
var genreDump = $"Genre id {genre.GenreId} GenreName {genre.GenreName}" +
$" Genre song Count {genre.SongCount}";
Console.WriteLine(genreDump);
_logger.Info(genreDump);
}
else
{
@@ -543,41 +422,6 @@ namespace Icarus.Controllers.Managers
song.YearId = year.YearId;
}
private bool DeleteSongFromFilesystem(Song song)
{
var songPath = song.SongPath;
_logger.Info("Deleting song from the filesystem");
try
{
System.IO.File.Delete(songPath);
}
catch(Exception ex)
{
var msg = ex.Message;
_logger.Error(msg, "An error occurred when attempting to delete the song from the filesystem");
return false;
}
return DoesSongExistOnFilesystem(song);
}
private bool DoesSongExistOnFilesystem(Song song)
{
var songPath = song.SongPath;
if (!System.IO.File.Exists(songPath))
{
_logger.Info("Song does not exist on the filesystem");
return false;
}
_logger.Info("Song exists on the filesystem");
return true;
}
public Song SongCopy(Song song)
{
var updatedSongRecord = new Song
@@ -814,8 +658,6 @@ namespace Icarus.Controllers.Managers
if (!string.IsNullOrEmpty(newSongRecord.Genre))
{
updatedSongRecord.Genre = newSongRecord.Genre;
Console.WriteLine("Genre changed");
Console.WriteLine($"{updatedSongRecord.Genre} {newSongRecord.Genre}");
}
if (newSongRecord.Year != null || newSongRecord.Year > 0)
updatedSongRecord.Year = newSongRecord.Year;