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