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