From 3dd143fa4dd1e5fdb1646c922ae0b3a9e31aab17 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 20 Jun 2024 20:20:47 -0400 Subject: [PATCH] #94: Saving the file type of cover art, but there is still some work to do The filename of cover art is hard coded to have the png extension. Need to correct this --- Constants/DirectoryPaths.cs | 4 +++ Controllers/Managers/CoverArtManager.cs | 36 ++++++++++++++++------ Controllers/Managers/SongManager.cs | 9 ++++++ Controllers/Utilities/MetadataRetriever.cs | 29 ++++++++++++++++- Controllers/v1/CoverArtController.cs | 2 +- Icarus.csproj | 1 + Models/CoverArt.cs | 22 +++++++++++-- 7 files changed, 89 insertions(+), 14 deletions(-) diff --git a/Constants/DirectoryPaths.cs b/Constants/DirectoryPaths.cs index 016cd53..d23e749 100644 --- a/Constants/DirectoryPaths.cs +++ b/Constants/DirectoryPaths.cs @@ -2,8 +2,12 @@ namespace Icarus.Constants; public class DirectoryPaths { + // TODO: Remove this public static string CoverArtPath => Directory.GetCurrentDirectory() + "/Images/Stock/CoverArt.png"; + public static string CoverArtDirectory => + Directory.GetCurrentDirectory() + "/Images/Stock/"; + public static string CoverArtFilename => "CoverArt.png"; public static string FILENAME_CHARACTERS = "ABCDEF0123456789"; public static int FILENAME_LENGTH = 25; } diff --git a/Controllers/Managers/CoverArtManager.cs b/Controllers/Managers/CoverArtManager.cs index 8988305..47a4358 100644 --- a/Controllers/Managers/CoverArtManager.cs +++ b/Controllers/Managers/CoverArtManager.cs @@ -48,11 +48,11 @@ public class CoverArtManager : BaseManager try { var stockCoverArtPath = _rootCoverArtPath + "CoverArt.png"; - if (!string.Equals(stockCoverArtPath, coverArt.ImagePath, + if (!string.Equals(stockCoverArtPath, coverArt.ImagePath(), StringComparison.CurrentCultureIgnoreCase)) { _logger.Info("Song does not contain the stock cover art"); - File.Delete(coverArt.ImagePath); + File.Delete(coverArt.ImagePath()); _logger.Info("Cover art deleted from the filesystem"); } else @@ -82,9 +82,11 @@ public class CoverArtManager : BaseManager }; var segment = coverArt.GenerateFilename(0); - var imagePath = dirMgr.SongDirectory + segment + defaultExtension; + // var imagePath = dirMgr.SongDirectory + segment + defaultExtension; - coverArt.ImagePath = imagePath; + // coverArt.ImagePath = imagePath; + coverArt.Directory = dirMgr.SongDirectory; + coverArt.Filename = segment + defaultExtension; var metaData = new MetadataRetriever(); var imgBytes = metaData.RetrieveCoverArtBytes(song); @@ -92,17 +94,29 @@ public class CoverArtManager : BaseManager if (imgBytes != null) { _logger.Info("Saving cover art to the filesystem"); - File.WriteAllBytes(coverArt.ImagePath, imgBytes); + File.WriteAllBytes(coverArt.ImagePath(), imgBytes); } else { _logger.Info("Song has no cover art, applying stock cover art"); var coverArtFilePath = _rootCoverArtPath + $"{segment}{defaultExtension}"; - coverArt.ImagePath = DirectoryPaths.CoverArtPath; + // coverArt.ImagePath = DirectoryPaths.CoverArtPath; + coverArt.Directory = DirectoryPaths.CoverArtDirectory; + coverArt.Filename = DirectoryPaths.CoverArtFilename; metaData.UpdateCoverArt(song, coverArt); - coverArt.ImagePath = coverArtFilePath; - File.WriteAllBytes(coverArt.ImagePath, _stockCoverArt); + coverArt.Directory = this._rootCoverArtPath; + coverArt.Filename = $"{segment}{defaultExtension}"; + // coverArt.ImagePath = coverArtFilePath; + File.WriteAllBytes(coverArt.ImagePath(), _stockCoverArt); + } + + coverArt.Type = metaData.CoverArtFileExtensionType(coverArt); + if (string.IsNullOrEmpty(coverArt.Type)) + { + Console.WriteLine("File type is empty"); + Console.WriteLine($"Directory: {coverArt.Directory}"); + Console.WriteLine($"Filename: {coverArt.Filename}"); } return coverArt; @@ -129,9 +143,11 @@ public class CoverArtManager : BaseManager var segment = cover.GenerateFilename(0); var imagePath = dirMgr.SongDirectory + segment + defaultExtension; - cover.ImagePath = imagePath; + cover.Directory = dirMgr.SongDirectory; + cover.Filename = segment + defaultExtension; + // cover.ImagePath = imagePath; - using (var fileStream = new FileStream(imagePath, FileMode.Create)) + using (var fileStream = new FileStream(cover.ImagePath(), FileMode.Create)) { data.CopyTo(fileStream); } diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index 8234e06..ecea25f 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -232,6 +232,15 @@ public class SongManager : BaseManager var coverMgr = new CoverArtManager(_config); var coverArt = coverMgr.SaveCoverArt(coverArtData, song); + MetadataRetriever metaData = new MetadataRetriever(); + coverArt.Type = metaData.CoverArtFileExtensionType(coverArt); + if (string.IsNullOrEmpty(coverArt.Type)) + { + Console.WriteLine("File type is empty"); + Console.WriteLine($"Directory: {coverArt.Directory}"); + Console.WriteLine($"Filename: {coverArt.Filename}"); + } + DirectoryManager dirMgr = new DirectoryManager(_config, song); dirMgr.CreateDirectory(); diff --git a/Controllers/Utilities/MetadataRetriever.cs b/Controllers/Utilities/MetadataRetriever.cs index c0f6a3a..fda7bef 100644 --- a/Controllers/Utilities/MetadataRetriever.cs +++ b/Controllers/Utilities/MetadataRetriever.cs @@ -68,6 +68,33 @@ public class MetadataRetriever _logger.Info($"Year: {song.Year}"); _logger.Info($"Duration: {song.Duration}"); } + + public string CoverArtFileExtensionType(CoverArt cover) + { + Console.WriteLine("Retrieving CoverArt file extension type"); + + using (var fileStream = System.IO.File.OpenRead(cover.ImagePath())) + { + var isRecognizableType = FileTypeChecker.FileTypeValidator.IsTypeRecognizable(fileStream); + + if (!isRecognizableType) + { + return string.Empty; + } + + + var fileType = FileTypeChecker.FileTypeValidator.GetFileType(fileStream); + // Console.WriteLine("Type Name: {0}", fileType.Name); + // Console.WriteLine("Type Extension: {0}", fileType.Extension); + // Console.WriteLine("Is Image?: {0}", fileStream.IsImage()); + // Console.WriteLine("Is Bitmap?: {0}", fileStream.Is()); + // + Console.WriteLine($"Filetype: {fileType}"); + + return fileType.Extension; + } + } + public Song RetrieveMetaData(string filePath) { Song song = new Song(); @@ -162,7 +189,7 @@ public class MetadataRetriever var pics = tag.Tag.Pictures; Array.Resize(ref pics, 1); - pics[0] = new Picture(coverArt.ImagePath) + pics[0] = new Picture(coverArt.ImagePath()) { Description = "Cover Art" }; diff --git a/Controllers/v1/CoverArtController.cs b/Controllers/v1/CoverArtController.cs index 338ecb2..241e3e1 100644 --- a/Controllers/v1/CoverArtController.cs +++ b/Controllers/v1/CoverArtController.cs @@ -61,7 +61,7 @@ public class CoverArtController : BaseController { _logger.LogInformation("Found cover art record"); var coverArtBytes = System.IO.File.ReadAllBytes( - coverArt.ImagePath); + coverArt.ImagePath()); return File(coverArtBytes, "application/x-msdownload", coverArt.SongTitle); diff --git a/Icarus.csproj b/Icarus.csproj index b5fada8..4a7e949 100644 --- a/Icarus.csproj +++ b/Icarus.csproj @@ -12,6 +12,7 @@ + diff --git a/Models/CoverArt.cs b/Models/CoverArt.cs index 370c252..5c22aab 100644 --- a/Models/CoverArt.cs +++ b/Models/CoverArt.cs @@ -10,11 +10,29 @@ public class CoverArt [JsonProperty("title")] public string SongTitle { get; set; } [JsonIgnore] - public string ImagePath { get; set; } + public string Directory { get; set; } + [JsonProperty("filename")] + public string Filename { get; set; } + [JsonProperty("type")] + public string Type { get; set; } #endregion #region Methods + public string ImagePath() + { + var fullPath = this.Directory; + + if (fullPath[fullPath.Length -1] != '/') + { + fullPath += "/"; + } + + fullPath += Filename; + + return fullPath; + } + public string GenerateFilename(int flag) { int length = Constants.DirectoryPaths.FILENAME_LENGTH; @@ -27,6 +45,6 @@ public class CoverArt return (flag == 0) ? filename : $"{filename}{extension}"; } - public async Task GetData() => await File.ReadAllBytesAsync(this.ImagePath); + public async Task GetData() => await File.ReadAllBytesAsync(this.ImagePath()); #endregion }