Refactoring

This commit is contained in:
kdeng00
2021-12-25 11:28:02 -05:00
parent 023298c6f8
commit 64e6f33d7c
6 changed files with 105 additions and 98 deletions
+1
View File
@@ -81,6 +81,7 @@ namespace Icarus.Controllers.Managers
{ {
var dirMgr = new DirectoryManager(_rootCoverArtPath); var dirMgr = new DirectoryManager(_rootCoverArtPath);
dirMgr.CreateDirectory(song); dirMgr.CreateDirectory(song);
// TODO: Do not use metadata for file structure
var imagePath = dirMgr.SongDirectory + song.Title + ".png"; var imagePath = dirMgr.SongDirectory + song.Title + ".png";
var coverArt = new CoverArt var coverArt = new CoverArt
{ {
+2
View File
@@ -9,6 +9,7 @@ using Icarus.Types;
namespace Icarus.Controllers.Managers namespace Icarus.Controllers.Managers
{ {
// NOTE: Do not use metadata for the song's metadata
public class DirectoryManager : BaseManager public class DirectoryManager : BaseManager
{ {
#region Fields #region Fields
@@ -157,6 +158,7 @@ namespace Icarus.Controllers.Managers
return artistPath; return artistPath;
} }
//TODO: Change this so the song's metadata is not being used
public string GenerateSongPath(Song song) public string GenerateSongPath(Song song)
{ {
_logger.Info("Generating song path"); _logger.Info("Generating song path");
+14 -10
View File
@@ -70,7 +70,8 @@ namespace Icarus.Controllers.Managers
try try
{ {
var oldSongRecord = _songContext.RetrieveRecord(song); var oldSongRecord = _songContext.RetrieveRecord(song);
song.SongPath = oldSongRecord.SongPath; song.Filename = oldSongRecord.Filename;
song.SongDirectory = oldSongRecord.SongDirectory;
MetadataRetriever updateMetadata = new MetadataRetriever(); MetadataRetriever updateMetadata = new MetadataRetriever();
updateMetadata.UpdateMetadata(song, oldSongRecord); updateMetadata.UpdateMetadata(song, oldSongRecord);
@@ -109,7 +110,7 @@ namespace Icarus.Controllers.Managers
bool successful = false; bool successful = false;
try try
{ {
var songPath = songMetaData.SongPath; var songPath = songMetaData.SongPath();
System.IO.File.Delete(songPath); System.IO.File.Delete(songPath);
successful = true; successful = true;
DirectoryManager dirMgr = new DirectoryManager(_config, songMetaData); DirectoryManager dirMgr = new DirectoryManager(_config, songMetaData);
@@ -163,7 +164,8 @@ namespace Icarus.Controllers.Managers
var fileTempPath = Path.Combine(_tempDirectoryRoot, songFile.FileName); var fileTempPath = Path.Combine(_tempDirectoryRoot, songFile.FileName);
var song = await SaveSongTemp(songFile, fileTempPath); var song = await SaveSongTemp(songFile, fileTempPath);
song.SongPath = fileTempPath; song.SongDirectory = _tempDirectoryRoot;
song.Filename = songFile.FileName;
DirectoryManager dirMgr = new DirectoryManager(_config, song); DirectoryManager dirMgr = new DirectoryManager(_config, song);
dirMgr.CreateDirectory(); dirMgr.CreateDirectory();
@@ -184,7 +186,8 @@ namespace Icarus.Controllers.Managers
using (var fileStream = new FileStream(filePath, FileMode.Create)) using (var fileStream = new FileStream(filePath, FileMode.Create))
{ {
await (songFile.CopyToAsync(fileStream)); await (songFile.CopyToAsync(fileStream));
song.SongPath = filePath; song.SongDirectory = dirMgr.SongDirectory;
_logger.Info("Song successfully saved to filesystem"); _logger.Info("Song successfully saved to filesystem");
} }
@@ -225,7 +228,7 @@ namespace Icarus.Controllers.Managers
private async Task<SongData> RetrieveSongFromFileSystem(Song details) private async Task<SongData> RetrieveSongFromFileSystem(Song details)
{ {
byte[] uncompressedSong = await System.IO.File.ReadAllBytesAsync(details.SongPath); byte[] uncompressedSong = await System.IO.File.ReadAllBytesAsync(details.SongPath());
return new SongData return new SongData
{ {
@@ -403,7 +406,7 @@ namespace Icarus.Controllers.Managers
private bool DeleteSongFromFilesystem(Song song) private bool DeleteSongFromFilesystem(Song song)
{ {
var songPath = song.SongPath; var songPath = song.SongPath();
_logger.Info("Deleting song from the filesystem"); _logger.Info("Deleting song from the filesystem");
@@ -422,7 +425,7 @@ namespace Icarus.Controllers.Managers
} }
private bool DoesSongExistOnFilesystem(Song song) private bool DoesSongExistOnFilesystem(Song song)
{ {
var songPath = song.SongPath; var songPath = song.SongPath();
if (!System.IO.File.Exists(songPath)) if (!System.IO.File.Exists(songPath))
{ {
@@ -598,7 +601,7 @@ namespace Icarus.Controllers.Managers
Genre = oldSongRecord.Genre, Genre = oldSongRecord.Genre,
Year = oldSongRecord.Year, Year = oldSongRecord.Year,
Filename = oldSongRecord.Filename, Filename = oldSongRecord.Filename,
SongPath = oldSongRecord.SongPath, SongDirectory = oldSongRecord.SongDirectory,
ArtistID = oldSongRecord.ArtistID, ArtistID = oldSongRecord.ArtistID,
AlbumID = oldSongRecord.AlbumID, AlbumID = oldSongRecord.AlbumID,
GenreID = oldSongRecord.GenreID GenreID = oldSongRecord.GenreID
@@ -644,7 +647,7 @@ namespace Icarus.Controllers.Managers
_logger.Info("Change to song's album or artist"); _logger.Info("Change to song's album or artist");
DirectoryManager dirMgr = new DirectoryManager(_config); DirectoryManager dirMgr = new DirectoryManager(_config);
var oldSongPath = updatedSongRecord.SongPath; var oldSongPath = updatedSongRecord.SongPath();
var newSongPath = dirMgr.GenerateSongPath(updatedSongRecord); var newSongPath = dirMgr.GenerateSongPath(updatedSongRecord);
var filename = updatedSongRecord.Filename; var filename = updatedSongRecord.Filename;
@@ -664,7 +667,8 @@ namespace Icarus.Controllers.Managers
Console.WriteLine("New path does not exist when it should"); Console.WriteLine("New path does not exist when it should");
} }
updatedSongRecord.SongPath = newSongPath + filename; updatedSongRecord.SongDirectory = newSongPath;
updatedSongRecord.Filename = filename;
_logger.Info("Deleting old song path"); _logger.Info("Deleting old song path");
+6 -6
View File
@@ -56,7 +56,7 @@ namespace Icarus.Controllers.Utilities
Console.WriteLine($"AlbumID: {song.AlbumID}"); Console.WriteLine($"AlbumID: {song.AlbumID}");
Console.WriteLine($"ArtistID: {song.ArtistID}"); Console.WriteLine($"ArtistID: {song.ArtistID}");
Console.WriteLine($"GenreID: {song.GenreID}"); Console.WriteLine($"GenreID: {song.GenreID}");
Console.WriteLine($"Song Path: {song.SongPath}"); Console.WriteLine($"Song Path: {song.SongPath()}");
Console.WriteLine($"Filename: {song.Filename}"); Console.WriteLine($"Filename: {song.Filename}");
Console.WriteLine("\n"); Console.WriteLine("\n");
@@ -105,7 +105,7 @@ namespace Icarus.Controllers.Utilities
try try
{ {
Console.WriteLine("Fetching image"); Console.WriteLine("Fetching image");
var tag = TagLib.File.Create(song.SongPath); var tag = TagLib.File.Create(song.SongPath());
byte[] imgBytes = tag.Tag.Pictures[0].Data.Data; byte[] imgBytes = tag.Tag.Pictures[0].Data.Data;
return imgBytes; return imgBytes;
@@ -125,7 +125,7 @@ namespace Icarus.Controllers.Utilities
{ {
Console.WriteLine("Updating song metadata"); Console.WriteLine("Updating song metadata");
_logger.Info("Updating song metadata"); _logger.Info("Updating song metadata");
var filePath = song.SongPath; var filePath = song.SongPath();
TagLib.File fileTag = TagLib.File.Create(filePath); TagLib.File fileTag = TagLib.File.Create(filePath);
fileTag.Tag.Title = song.Title; fileTag.Tag.Title = song.Title;
fileTag.Tag.Genres = new []{song.Genre}; fileTag.Tag.Genres = new []{song.Genre};
@@ -162,7 +162,7 @@ namespace Icarus.Controllers.Utilities
{ {
Console.WriteLine("Updating song's cover art"); Console.WriteLine("Updating song's cover art");
var tag = TagLib.File.Create(song.SongPath); var tag = TagLib.File.Create(song.SongPath());
var pics = tag.Tag.Pictures; var pics = tag.Tag.Pictures;
Array.Resize(ref pics, 1); Array.Resize(ref pics, 1);
@@ -177,7 +177,7 @@ namespace Icarus.Controllers.Utilities
private void PerformUpdate(Song updatedSong, SortedDictionary<string, bool> checkedValues) private void PerformUpdate(Song updatedSong, SortedDictionary<string, bool> checkedValues)
{ {
var filePath = updatedSong.SongPath; var filePath = updatedSong.SongPath();
var title = updatedSong.Title; var title = updatedSong.Title;
var artist = updatedSong.Artist; var artist = updatedSong.Artist;
var album = updatedSong.AlbumTitle; var album = updatedSong.AlbumTitle;
@@ -244,7 +244,7 @@ namespace Icarus.Controllers.Utilities
Year = song.Year, Year = song.Year,
Duration = song.Duration, Duration = song.Duration,
Filename = song.Filename, Filename = song.Filename,
SongPath = song.SongPath SongDirectory = song.SongDirectory
}; };
} }
private void PrintMetadata() private void PrintMetadata()
+80 -80
View File
@@ -13,18 +13,18 @@ namespace Icarus.Controllers.Utilities
public class SongCompression public class SongCompression
{ {
#region Fields #region Fields
string _compressedSongFilename; string _compressedSongFilename;
string _tempDirectory; string _tempDirectory;
byte[] _uncompressedSong; byte[] _uncompressedSong;
#endregion #endregion
#region Propterties #region Propterties
public string CompressedSongFilename public string CompressedSongFilename
{ {
get => _compressedSongFilename; get => _compressedSongFilename;
set => _compressedSongFilename = value; set => _compressedSongFilename = value;
} }
#endregion #endregion
@@ -32,10 +32,10 @@ namespace Icarus.Controllers.Utilities
public SongCompression() public SongCompression()
{ {
} }
public SongCompression(string tempDirectory) public SongCompression(string tempDirectory)
{ {
_tempDirectory = tempDirectory; _tempDirectory = tempDirectory;
} }
public SongCompression(byte[] uncompressedSong) public SongCompression(byte[] uncompressedSong)
{ {
_uncompressedSong = uncompressedSong; _uncompressedSong = uncompressedSong;
@@ -44,87 +44,87 @@ namespace Icarus.Controllers.Utilities
#region Methods #region Methods
public async Task<SongData> RetrieveCompressedSong(Song song) public async Task<SongData> RetrieveCompressedSong(Song song)
{ {
SongData songData = new SongData(); SongData songData = new SongData();
try try
{ {
var archivePath = RetrieveCompressesSongPath(song); var archivePath = RetrieveCompressesSongPath(song);
Console.WriteLine($"Compressed song saved to: {archivePath}"); Console.WriteLine($"Compressed song saved to: {archivePath}");
songData.Data = await System.IO.File.ReadAllBytesAsync(archivePath); songData.Data = await System.IO.File.ReadAllBytesAsync(archivePath);
} }
catch(Exception ex) catch(Exception ex)
{ {
var exMsg = ex.Message; var exMsg = ex.Message;
Console.WriteLine($"An error ocurred: \n{exMsg}"); Console.WriteLine($"An error ocurred: \n{exMsg}");
} }
return songData; return songData;
} }
public string RetrieveCompressesSongPath(Song songDetails) public string RetrieveCompressesSongPath(Song songDetails)
{ {
string tmpZipFilePath = _tempDirectory + songDetails.Filename; string tmpZipFilePath = _tempDirectory + songDetails.Filename;
try try
{ {
using (ZipFile zip = new ZipFile()) using (ZipFile zip = new ZipFile())
{ {
zip.AddFile(songDetails.SongPath()); zip.AddFile(songDetails.SongPath());
zip.Save(tmpZipFilePath); zip.Save(tmpZipFilePath);
} }
Console.WriteLine("Successfully compressed"); Console.WriteLine("Successfully compressed");
} }
catch (Exception ex) catch (Exception ex)
{ {
var exMsg = ex.Message; var exMsg = ex.Message;
Console.WriteLine("An error ocurred"); Console.WriteLine("An error ocurred");
Console.WriteLine(exMsg); Console.WriteLine(exMsg);
} }
if (songDetails.Filename.Contains(".mp3")) if (songDetails.Filename.Contains(".mp3"))
{ {
_compressedSongFilename = StripMP3Extension(songDetails.Filename); _compressedSongFilename = StripMP3Extension(songDetails.Filename);
} }
return tmpZipFilePath; return tmpZipFilePath;
} }
// Method not being used // Method not being used
public byte[] CompressedSong(byte[] uncompressedSong) public byte[] CompressedSong(byte[] uncompressedSong)
{ {
byte[] compressedSong = null; byte[] compressedSong = null;
try try
{ {
Console.WriteLine("Song has been successfully compressed"); Console.WriteLine("Song has been successfully compressed");
} }
catch (Exception ex) catch (Exception ex)
{ {
var exMsg = ex.Message; var exMsg = ex.Message;
Console.WriteLine("An error ocurred:"); Console.WriteLine("An error ocurred:");
Console.WriteLine(exMsg); Console.WriteLine(exMsg);
} }
return compressedSong; return compressedSong;
} }
string StripMP3Extension(string filename) string StripMP3Extension(string filename)
{ {
Console.WriteLine($"Before: {filename}"); Console.WriteLine($"Before: {filename}");
int filenameLength = filename.Length; int filenameLength = filename.Length;
Console.WriteLine($"Filename length {filenameLength}"); Console.WriteLine($"Filename length {filenameLength}");
var endIndex = filenameLength - 1; var endIndex = filenameLength - 1;
var startIndex = endIndex - 3; var startIndex = endIndex - 3;
Console.WriteLine($"Starting index {startIndex} and ending index {endIndex}"); Console.WriteLine($"Starting index {startIndex} and ending index {endIndex}");
var stripped = filename.Remove(startIndex, 4); var stripped = filename.Remove(startIndex, 4);
stripped += ".zip"; stripped += ".zip";
Console.WriteLine($"After {stripped}"); Console.WriteLine($"After {stripped}");
return stripped; return stripped;
} }
#endregion #endregion
} }
} }
+1 -1
View File
@@ -51,7 +51,7 @@ namespace Icarus.Controllers.V1
var song = context.Songs.FirstOrDefault(sng => sng.SongID == id); var song = context.Songs.FirstOrDefault(sng => sng.SongID == id);
var stream = new FileStream(song.SongPath, FileMode.Open, FileAccess.Read); var stream = new FileStream(song.SongPath(), FileMode.Open, FileAccess.Read);
stream.Position = 0; stream.Position = 0;
var filename = $"{song.Title}.mp3"; var filename = $"{song.Title}.mp3";