Added functionality to save the stock cover art to a song if the song does not contain a cover art #50
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Icarus.Constants
|
||||
{
|
||||
public class DirectoryPaths
|
||||
{
|
||||
public static string CoverArtPath =>
|
||||
Directory.GetCurrentDirectory() + "/Images/Stock/CoverArt.png";
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
using Icarus.Constants;
|
||||
using Icarus.Controllers.Utilities;
|
||||
using Icarus.Database.Repositories;
|
||||
using Icarus.Models;
|
||||
@@ -13,6 +14,7 @@ namespace Icarus.Controllers.Managers
|
||||
{
|
||||
#region Fields
|
||||
private string _rootCoverArtPath;
|
||||
private byte[] _stockCoverArt = null;
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -20,6 +22,7 @@ namespace Icarus.Controllers.Managers
|
||||
public CoverArtManager(string rootPath)
|
||||
{
|
||||
_rootCoverArtPath = rootPath;
|
||||
Initialize();
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -28,12 +31,17 @@ namespace Icarus.Controllers.Managers
|
||||
public void SaveCoverArtToDatabase(ref Song song, ref CoverArt coverArt,
|
||||
CoverArtRepository coverArtRepository)
|
||||
{
|
||||
Console.WriteLine("Gonig to save cover art record");
|
||||
coverArtRepository.SaveCoverArt(coverArt);
|
||||
Console.WriteLine($"tlte {coverArt.SongTitle}");
|
||||
|
||||
coverArt = coverArtRepository.GetCoverArt(CoverArtField.SongTitle,
|
||||
coverArt);
|
||||
Console.WriteLine($"cover art id {coverArt.CoverArtId}");
|
||||
|
||||
song.CoverArtId = coverArt.CoverArtId;
|
||||
Console.WriteLine("Nothing wrong here");
|
||||
Console.WriteLine($"song cover art id {song.CoverArtId}");
|
||||
}
|
||||
|
||||
public CoverArt SaveCoverArt(Song song)
|
||||
@@ -52,7 +60,15 @@ namespace Icarus.Controllers.Managers
|
||||
var metaData = new MetadataRetriever();
|
||||
var imgBytes = metaData.RetrieveCoverArtBytes(song);
|
||||
|
||||
File.WriteAllBytes(imagePath, imgBytes);
|
||||
if (imgBytes != null)
|
||||
File.WriteAllBytes(coverArt.ImagePath, imgBytes);
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Song has no cover art");
|
||||
coverArt.ImagePath = _rootCoverArtPath + "CoverArt.png";
|
||||
metaData.UpdateCoverArt(song, coverArt);
|
||||
File.WriteAllBytes(coverArt.ImagePath, _stockCoverArt);
|
||||
}
|
||||
|
||||
return coverArt;
|
||||
}
|
||||
@@ -65,6 +81,19 @@ namespace Icarus.Controllers.Managers
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
if (System.IO.File.Exists(DirectoryPaths.CoverArtPath))
|
||||
_stockCoverArt = File.ReadAllBytes(DirectoryPaths.CoverArtPath);
|
||||
|
||||
if (!File.Exists(_rootCoverArtPath + "/CoverArt.png"))
|
||||
{
|
||||
File.WriteAllBytes(_rootCoverArtPath + "/CoverArt.png",
|
||||
_stockCoverArt);
|
||||
Console.WriteLine("Copied Stock Cover Art");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -354,8 +354,6 @@ namespace Icarus.Controllers.Managers
|
||||
|
||||
DirectoryManager dirMgr = new DirectoryManager(_config, song);
|
||||
dirMgr.CreateDirectory();
|
||||
var coverMgr = new CoverArtManager(_config.GetValue<string>("CoverArtPath"));
|
||||
var coverArt = coverMgr.SaveCoverArt(song);
|
||||
|
||||
System.IO.File.Delete(fileTempPath);
|
||||
|
||||
@@ -369,6 +367,7 @@ namespace Icarus.Controllers.Managers
|
||||
|
||||
_logger.Info($"Absolute song path: {filePath}");
|
||||
|
||||
|
||||
using (var fileStream = new FileStream(filePath, FileMode.Create))
|
||||
{
|
||||
await (songFile.CopyToAsync(fileStream));
|
||||
@@ -377,6 +376,9 @@ namespace Icarus.Controllers.Managers
|
||||
_logger.Info("Song successfully saved to filesystem");
|
||||
}
|
||||
|
||||
var coverMgr = new CoverArtManager(_config.GetValue<string>("CoverArtPath"));
|
||||
var coverArt = coverMgr.SaveCoverArt(song);
|
||||
|
||||
coverMgr.SaveCoverArtToDatabase(ref song, ref coverArt,
|
||||
coverArtStore);
|
||||
SaveSongToDatabase(song, songStore, albumStore, artistStore, genreStore,
|
||||
|
||||
@@ -103,16 +103,21 @@ namespace Icarus.Controllers.Utilities
|
||||
|
||||
public byte[] RetrieveCoverArtBytes(Song song)
|
||||
{
|
||||
Console.WriteLine("Fetching image");
|
||||
var tag = TagLib.File.Create(song.SongPath);
|
||||
byte[] imgBytes = tag.Tag.Pictures[0].Data.Data;
|
||||
if (imgBytes == null)
|
||||
try
|
||||
{
|
||||
Console.WriteLine("Null image");
|
||||
return null;
|
||||
Console.WriteLine("Fetching image");
|
||||
var tag = TagLib.File.Create(song.SongPath);
|
||||
byte[] imgBytes = tag.Tag.Pictures[0].Data.Data;
|
||||
|
||||
return imgBytes;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var msg = ex.Message;
|
||||
_logger.Error(msg, "An error occurred in MetadataRetriever");
|
||||
}
|
||||
|
||||
return imgBytes;
|
||||
return null;
|
||||
}
|
||||
|
||||
public void UpdateMetadata(Song song)
|
||||
@@ -154,6 +159,22 @@ namespace Icarus.Controllers.Utilities
|
||||
Message = "Failed to update metadata";
|
||||
}
|
||||
}
|
||||
public void UpdateCoverArt(Song song, CoverArt coverArt)
|
||||
{
|
||||
Console.WriteLine("Updating song's cover art");
|
||||
|
||||
var tag = TagLib.File.Create(song.SongPath);
|
||||
var pics = tag.Tag.Pictures;
|
||||
Array.Resize(ref pics, 1);
|
||||
|
||||
pics[0] = new Picture(coverArt.ImagePath)
|
||||
{
|
||||
Description = "Cover Art"
|
||||
};
|
||||
|
||||
tag.Tag.Pictures = pics;
|
||||
tag.Save();
|
||||
}
|
||||
|
||||
private void PerformUpdate(Song updatedSong, SortedDictionary<string, bool> checkedValues)
|
||||
{
|
||||
|
||||
@@ -36,9 +36,10 @@ namespace Icarus.Database.Repositories
|
||||
conn.Open();
|
||||
string query = "INSERT INTO Song(Title, AlbumTitle, Artist," +
|
||||
" Year, Genre, Duration, Filename, SongPath, AlbumId, " +
|
||||
"ArtistId, GenreId, YearId) VALUES(@Title, @AlbumTitle, " +
|
||||
"@Artist, @Year, @Genre, @Duration, @Filename, @SongPath, " +
|
||||
"@AlbumId, @ArtistId, @GenreId, @YearId)";
|
||||
"ArtistId, GenreId, YearId, CoverArtId) VALUES(@Title," +
|
||||
" @AlbumTitle, @Artist, @Year, @Genre, @Duration, " +
|
||||
"@Filename, @SongPath, @AlbumId, @ArtistId, @GenreId," +
|
||||
" @YearId, @CoverArtId)";
|
||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@Title", song.Title);
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Update="nlog.config" CopyToOutputDirectory="PreserveNewest" />
|
||||
<Content Include="Images/Stock/*.*" CopyToOutputDirectory="PreserveNewest" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 24 KiB |
Reference in New Issue
Block a user