diff --git a/Constants/DirectoryPaths.cs b/Constants/DirectoryPaths.cs index 016cd53..37d96c4 100644 --- a/Constants/DirectoryPaths.cs +++ b/Constants/DirectoryPaths.cs @@ -2,8 +2,8 @@ namespace Icarus.Constants; public class DirectoryPaths { - 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..dbd259d 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,8 @@ public class CoverArtManager : BaseManager }; var segment = coverArt.GenerateFilename(0); - var imagePath = dirMgr.SongDirectory + segment + defaultExtension; - - coverArt.ImagePath = imagePath; + coverArt.Directory = dirMgr.SongDirectory; + coverArt.Filename = segment + defaultExtension; var metaData = new MetadataRetriever(); var imgBytes = metaData.RetrieveCoverArtBytes(song); @@ -92,17 +91,27 @@ 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.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}"; + 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; @@ -122,16 +131,19 @@ public class CoverArtManager : BaseManager try { + MetadataRetriever metaData = new MetadataRetriever(); var dirMgr = new DirectoryManager(_rootCoverArtPath); - var defaultExtension = ".png"; + cover.Type = metaData.FileExtensionType(data); + var defaultExtension = "." + cover.Type; dirMgr.CreateDirectory(song); var segment = cover.GenerateFilename(0); var imagePath = dirMgr.SongDirectory + segment + defaultExtension; - cover.ImagePath = imagePath; + cover.Directory = dirMgr.SongDirectory; + cover.Filename = segment + defaultExtension; - using (var fileStream = new FileStream(imagePath, FileMode.Create)) + using (var fileStream = new FileStream(cover.ImagePath(), FileMode.Create)) { data.CopyTo(fileStream); } @@ -152,9 +164,10 @@ public class CoverArtManager : BaseManager private void Initialize() { _coverArtContext = new CoverArtContext(_connectionString); + var path = DirectoryPaths.CoverArtDirectory + DirectoryPaths.CoverArtFilename; - if (System.IO.File.Exists(DirectoryPaths.CoverArtPath)) - _stockCoverArt = File.ReadAllBytes(DirectoryPaths.CoverArtPath); + if (System.IO.File.Exists(path)) + _stockCoverArt = File.ReadAllBytes(path); if (!File.Exists(_rootCoverArtPath + "CoverArt.png")) { diff --git a/Controllers/Utilities/MetadataRetriever.cs b/Controllers/Utilities/MetadataRetriever.cs index c0f6a3a..ee15437 100644 --- a/Controllers/Utilities/MetadataRetriever.cs +++ b/Controllers/Utilities/MetadataRetriever.cs @@ -68,6 +68,48 @@ 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($"Filetype: {fileType}"); + + return fileType.Extension; + } + } + + public string FileExtensionType(IFormFile file) + { + using (var fileStream = file.OpenReadStream()) + { + var isRecognizableType = FileTypeChecker.FileTypeValidator.IsTypeRecognizable(fileStream); + + if (!isRecognizableType) + { + return string.Empty; + } + + + var fileType = FileTypeChecker.FileTypeValidator.GetFileType(fileStream); + Console.WriteLine($"Filetype: {fileType}"); + Console.WriteLine($"Extension: {fileType.Extension}"); + + return fileType.Extension; + } + } + public Song RetrieveMetaData(string filePath) { Song song = new Song(); @@ -162,7 +204,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 } diff --git a/Scripts/Migrations/Linux/AddUpdate.sh b/Scripts/Migrations/Linux/AddUpdate.sh old mode 100644 new mode 100755 index 6a1759c..493ceb5 --- a/Scripts/Migrations/Linux/AddUpdate.sh +++ b/Scripts/Migrations/Linux/AddUpdate.sh @@ -1,27 +1,28 @@ echo "Adding migrations..." + echo "Adding User migration" -dotnet-ef migrations add User --context UserContext +~/.dotnet/tools/dotnet-ef migrations add User --context UserContext echo "Adding Album migration" -dotnet-ef migrations add Album --context AlbumContext +~/.dotnet/tools/dotnet-ef migrations add Album --context AlbumContext echo "Adding Artist migration" -dotnet-ef migrations add Artist --context ArtistContext +~/.dotnet/tools/dotnet-ef migrations add Artist --context ArtistContext echo "Adding Genre migration" -dotnet-ef migrations add Genre --context GenreContext +~/.dotnet/tools/dotnet-ef migrations add Genre --context GenreContext echo "Adding Cover art migration" -dotnet-ef migrations add CoverArt --context CoverArtContext +~/.dotnet/tools/dotnet-ef migrations add CoverArt --context CoverArtContext echo "Adding Song migration" -dotnet-ef migrations add Song --context SongContext +~/.dotnet/tools/dotnet-ef migrations add Song --context SongContext echo "Updating migrations.." echo "Updating User migration" -dotnet-ef database update --context UserContext +~/.dotnet/tools/dotnet-ef database update --context UserContext echo "Updating Album migration" -dotnet-ef database update --context AlbumContext +~/.dotnet/tools/dotnet-ef database update --context AlbumContext echo "Updating Artist migration" -dotnet-ef database update --context ArtistContext +~/.dotnet/tools/dotnet-ef database update --context ArtistContext echo "Updating Genre migration" -dotnet-ef database update --context GenreContext +~/.dotnet/tools/dotnet-ef database update --context GenreContext echo "Updating Cover art migration" -dotnet-ef database update --context CoverArtContext +~/.dotnet/tools/dotnet-ef database update --context CoverArtContext echo "Updating Song migration" -dotnet-ef database update --context SongContext +~/.dotnet/tools/dotnet-ef database update --context SongContext diff --git a/Scripts/MySQL/delete_all.sql b/Scripts/MySQL/delete_all.sql index 8ce8313..d33190e 100644 --- a/Scripts/MySQL/delete_all.sql +++ b/Scripts/MySQL/delete_all.sql @@ -1,5 +1,7 @@ -delete from Song; delete from Album; delete from Artist; +delete from CoverArt; delete from Genre; delete from CoverArt; +delete from Song; +delete from User;