#94: Saving the file type of cover art, but there is still some work to do #100

Merged
kdeng00 merged 5 commits from tsk-94 into master 2024-06-21 20:26:44 -04:00
7 changed files with 89 additions and 14 deletions
Showing only changes of commit 3dd143fa4d - Show all commits
+4
View File
@@ -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;
}
+26 -10
View File
@@ -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);
}
+9
View File
@@ -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();
+28 -1
View File
@@ -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<Bitmap>());
//
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"
};
+1 -1
View File
@@ -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);
+1
View File
@@ -12,6 +12,7 @@
<PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />
<PackageReference Include="DotNetZip" Version="1.15.0" />
<PackageReference Include="EntityFramework" Version="6.4.4" />
<PackageReference Include="File.TypeChecker" Version="4.1.1" />
<PackageReference Include="ID3" Version="0.6.0" />
<PackageReference Include="JWT" Version="10.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6" />
+20 -2
View File
@@ -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<byte[]> GetData() => await File.ReadAllBytesAsync(this.ImagePath);
public async Task<byte[]> GetData() => await File.ReadAllBytesAsync(this.ImagePath());
#endregion
}