Cover art functionality added. Left TODO's on where to continue on. #50
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
using Icarus.Controllers.Utilities;
|
using Icarus.Controllers.Utilities;
|
||||||
using Icarus.Database.Repositories;
|
using Icarus.Database.Repositories;
|
||||||
@@ -23,14 +24,42 @@ namespace Icarus.Controllers.Managers
|
|||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
public bool SaveCoverArt(Song song)
|
public void SaveCoverArtToDatabase(ref Song song, ref CoverArt coverArt,
|
||||||
|
CoverArtRepository coverArtRepository)
|
||||||
{
|
{
|
||||||
// TODO: Implement functionality to save
|
// TODO: Implement saving Cover Art record to
|
||||||
// cover art to the filesystem. The result
|
// the database.
|
||||||
// will determine if the the cover art record will
|
}
|
||||||
// be saved to the DB
|
public CoverArt SaveCoverArt(Song song)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var dirMgr = new DirectoryManager(_rootCoverArtPath);
|
||||||
|
dirMgr.CreateDirectory(song);
|
||||||
|
var imagePath = dirMgr.SongDirectory + song.Title + ".png";
|
||||||
|
var coverArt = new CoverArt
|
||||||
|
{
|
||||||
|
SongTitle = song.Title,
|
||||||
|
ImagePath = imagePath
|
||||||
|
};
|
||||||
|
|
||||||
return false;
|
Console.WriteLine($"Cover path {imagePath}");
|
||||||
|
var metaData = new MetadataRetriever();
|
||||||
|
var imgBytes = metaData.RetrieveCoverArtBytes(song);
|
||||||
|
|
||||||
|
Console.WriteLine("Saving image");
|
||||||
|
File.WriteAllBytes(imagePath, imgBytes);
|
||||||
|
|
||||||
|
return coverArt;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
Console.WriteLine(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using System.Linq;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
using Icarus.Models;
|
using Icarus.Models;
|
||||||
|
using Icarus.Types;
|
||||||
|
|
||||||
namespace Icarus.Controllers.Managers
|
namespace Icarus.Controllers.Managers
|
||||||
{
|
{
|
||||||
@@ -39,6 +40,10 @@ namespace Icarus.Controllers.Managers
|
|||||||
_config = config;
|
_config = config;
|
||||||
Initialize();
|
Initialize();
|
||||||
}
|
}
|
||||||
|
public DirectoryManager(string rootDirectory)
|
||||||
|
{
|
||||||
|
_rootSongDirectory = rootDirectory;
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
@@ -58,12 +63,35 @@ namespace Icarus.Controllers.Managers
|
|||||||
|
|
||||||
Console.WriteLine($"The song will be saved in the following" +
|
Console.WriteLine($"The song will be saved in the following" +
|
||||||
$" directory: {_songDirectory}");
|
$" directory: {_songDirectory}");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"An error occurred {ex.Message}");
|
Console.WriteLine($"An error occurred {ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public void CreateDirectory(Song song)
|
||||||
|
{
|
||||||
|
_song = song;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_songDirectory = AlbumDirectory();
|
||||||
|
|
||||||
|
if (!Directory.Exists(_songDirectory))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(_songDirectory);
|
||||||
|
Console.WriteLine($"The directory has been created");
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine($"The song will be saved in the following" +
|
||||||
|
$" directory: {_songDirectory}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
_logger.Error(msg, "An error occurred");
|
||||||
|
}
|
||||||
|
}
|
||||||
public void DeleteEmptyDirectories()
|
public void DeleteEmptyDirectories()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -160,14 +188,14 @@ namespace Icarus.Controllers.Managers
|
|||||||
return songPath;
|
return songPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Initialize(DirectoryTypes dirTypes = DirectoryTypes.Music)
|
private void Initialize(DirectoryType dirTypes = DirectoryType.Music)
|
||||||
{
|
{
|
||||||
switch (dirTypes)
|
switch (dirTypes)
|
||||||
{
|
{
|
||||||
case DirectoryTypes.Music:
|
case DirectoryType.Music:
|
||||||
_rootSongDirectory = _config.GetValue<string>("RootMusicPath");
|
_rootSongDirectory = _config.GetValue<string>("RootMusicPath");
|
||||||
break;
|
break;
|
||||||
case DirectoryTypes.CoverArt:
|
case DirectoryType.CoverArt:
|
||||||
_rootSongDirectory = _config.GetValue<string>("CoverArtPath");
|
_rootSongDirectory = _config.GetValue<string>("CoverArtPath");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -209,14 +237,5 @@ namespace Icarus.Controllers.Managers
|
|||||||
return directory;
|
return directory;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region enums
|
|
||||||
private enum DirectoryTypes
|
|
||||||
{
|
|
||||||
Music = 0,
|
|
||||||
CoverArt
|
|
||||||
};
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -342,7 +342,7 @@ namespace Icarus.Controllers.Managers
|
|||||||
public async Task SaveSongToFileSystem(IFormFile songFile, SongRepository songStore,
|
public async Task SaveSongToFileSystem(IFormFile songFile, SongRepository songStore,
|
||||||
AlbumRepository albumStore, ArtistRepository artistStore,
|
AlbumRepository albumStore, ArtistRepository artistStore,
|
||||||
GenreRepository genreStore, YearRepository yearStore,
|
GenreRepository genreStore, YearRepository yearStore,
|
||||||
CoverArtRepository coverStore)
|
CoverArtRepository coverArtStore)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -350,10 +350,14 @@ namespace Icarus.Controllers.Managers
|
|||||||
|
|
||||||
var fileTempPath = Path.Combine(_tempDirectoryRoot, songFile.FileName);
|
var fileTempPath = Path.Combine(_tempDirectoryRoot, songFile.FileName);
|
||||||
var song = await SaveSongTemp(songFile, fileTempPath);
|
var song = await SaveSongTemp(songFile, fileTempPath);
|
||||||
System.IO.File.Delete(fileTempPath);
|
song.SongPath = fileTempPath;
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
var filePath = dirMgr.SongDirectory;
|
var filePath = dirMgr.SongDirectory;
|
||||||
var songFilename = songFile.FileName;
|
var songFilename = songFile.FileName;
|
||||||
@@ -373,6 +377,8 @@ namespace Icarus.Controllers.Managers
|
|||||||
_logger.Info("Song successfully saved to filesystem");
|
_logger.Info("Song successfully saved to filesystem");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
coverMgr.SaveCoverArtToDatabase(ref song, ref coverArt,
|
||||||
|
coverArtStore);
|
||||||
SaveSongToDatabase(song, songStore, albumStore, artistStore, genreStore,
|
SaveSongToDatabase(song, songStore, albumStore, artistStore, genreStore,
|
||||||
yearStore);
|
yearStore);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,6 +101,20 @@ namespace Icarus.Controllers.Utilities
|
|||||||
return song;
|
return song;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Null image");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return imgBytes;
|
||||||
|
}
|
||||||
|
|
||||||
public void UpdateMetadata(Song song)
|
public void UpdateMetadata(Song song)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -68,6 +68,14 @@ namespace Icarus.Controllers.V1
|
|||||||
_genreRepository = HttpContext
|
_genreRepository = HttpContext
|
||||||
.RequestServices
|
.RequestServices
|
||||||
.GetService(typeof(GenreRepository)) as GenreRepository;
|
.GetService(typeof(GenreRepository)) as GenreRepository;
|
||||||
|
|
||||||
|
_yearRepository = HttpContext
|
||||||
|
.RequestServices
|
||||||
|
.GetService(typeof(YearRepository)) as YearRepository;
|
||||||
|
|
||||||
|
_coverArtRepository = HttpContext
|
||||||
|
.RequestServices
|
||||||
|
.GetService(typeof(CoverArtRepository)) as CoverArtRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -76,9 +84,8 @@ namespace Icarus.Controllers.V1
|
|||||||
[Authorize("download:songs")]
|
[Authorize("download:songs")]
|
||||||
public async Task<IActionResult> Get(int id)
|
public async Task<IActionResult> Get(int id)
|
||||||
{
|
{
|
||||||
SongRepository context = HttpContext.RequestServices
|
Initialize();
|
||||||
.GetService(typeof(SongRepository)) as SongRepository;
|
var songMetaData = _songRepository.GetSong(id);
|
||||||
var songMetaData = context.GetSong(id);
|
|
||||||
|
|
||||||
SongData song = await _songMgr.RetrieveSong(songMetaData);
|
SongData song = await _songMgr.RetrieveSong(songMetaData);
|
||||||
|
|
||||||
@@ -93,14 +100,6 @@ namespace Icarus.Controllers.V1
|
|||||||
{
|
{
|
||||||
Initialize();
|
Initialize();
|
||||||
|
|
||||||
YearRepository yearStore = HttpContext.RequestServices
|
|
||||||
.GetService(typeof(YearRepository)) as YearRepository;
|
|
||||||
|
|
||||||
CoverArtRepository coverArtRepository = HttpContext
|
|
||||||
.RequestServices
|
|
||||||
.GetService
|
|
||||||
(typeof(CoverArtRepository)) as CoverArtRepository;
|
|
||||||
|
|
||||||
Console.WriteLine("Uploading song...");
|
Console.WriteLine("Uploading song...");
|
||||||
_logger.LogInformation("Uploading song...");
|
_logger.LogInformation("Uploading song...");
|
||||||
|
|
||||||
@@ -114,7 +113,7 @@ namespace Icarus.Controllers.V1
|
|||||||
|
|
||||||
await _songMgr.SaveSongToFileSystem(sng, _songRepository,
|
await _songMgr.SaveSongToFileSystem(sng, _songRepository,
|
||||||
_albumRepository, _artistRepository,
|
_albumRepository, _artistRepository,
|
||||||
_genreRepository, yearStore, coverArtRepository);
|
_genreRepository, _yearRepository, _coverArtRepository);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Icarus.Types
|
||||||
|
{
|
||||||
|
public enum DirectoryType
|
||||||
|
{
|
||||||
|
Music = 0,
|
||||||
|
CoverArt
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user