* #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 * #94: Added a TODO * #94: Corrected file extension being saved in the db * Cleanup * Saving databasescripts
This commit was merged in pull request #100.
This commit is contained in:
@@ -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"))
|
||||
{
|
||||
|
||||
@@ -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"
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user