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.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
using Icarus.Controllers.Utilities;
|
||||
using Icarus.Database.Repositories;
|
||||
@@ -23,14 +24,42 @@ namespace Icarus.Controllers.Managers
|
||||
|
||||
|
||||
#region Methods
|
||||
public bool SaveCoverArt(Song song)
|
||||
public void SaveCoverArtToDatabase(ref Song song, ref CoverArt coverArt,
|
||||
CoverArtRepository coverArtRepository)
|
||||
{
|
||||
// TODO: Implement functionality to save
|
||||
// cover art to the filesystem. The result
|
||||
// will determine if the the cover art record will
|
||||
// be saved to the DB
|
||||
// TODO: Implement saving Cover Art record to
|
||||
// the database.
|
||||
}
|
||||
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
|
||||
};
|
||||
|
||||
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 false;
|
||||
return null;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Linq;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
using Icarus.Models;
|
||||
using Icarus.Types;
|
||||
|
||||
namespace Icarus.Controllers.Managers
|
||||
{
|
||||
@@ -39,6 +40,10 @@ namespace Icarus.Controllers.Managers
|
||||
_config = config;
|
||||
Initialize();
|
||||
}
|
||||
public DirectoryManager(string rootDirectory)
|
||||
{
|
||||
_rootSongDirectory = rootDirectory;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -58,12 +63,35 @@ namespace Icarus.Controllers.Managers
|
||||
|
||||
Console.WriteLine($"The song will be saved in the following" +
|
||||
$" directory: {_songDirectory}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
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()
|
||||
{
|
||||
try
|
||||
@@ -160,14 +188,14 @@ namespace Icarus.Controllers.Managers
|
||||
return songPath;
|
||||
}
|
||||
|
||||
private void Initialize(DirectoryTypes dirTypes = DirectoryTypes.Music)
|
||||
private void Initialize(DirectoryType dirTypes = DirectoryType.Music)
|
||||
{
|
||||
switch (dirTypes)
|
||||
{
|
||||
case DirectoryTypes.Music:
|
||||
case DirectoryType.Music:
|
||||
_rootSongDirectory = _config.GetValue<string>("RootMusicPath");
|
||||
break;
|
||||
case DirectoryTypes.CoverArt:
|
||||
case DirectoryType.CoverArt:
|
||||
_rootSongDirectory = _config.GetValue<string>("CoverArtPath");
|
||||
break;
|
||||
}
|
||||
@@ -209,14 +237,5 @@ namespace Icarus.Controllers.Managers
|
||||
return directory;
|
||||
}
|
||||
#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,
|
||||
AlbumRepository albumStore, ArtistRepository artistStore,
|
||||
GenreRepository genreStore, YearRepository yearStore,
|
||||
CoverArtRepository coverStore)
|
||||
CoverArtRepository coverArtStore)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -350,10 +350,14 @@ namespace Icarus.Controllers.Managers
|
||||
|
||||
var fileTempPath = Path.Combine(_tempDirectoryRoot, songFile.FileName);
|
||||
var song = await SaveSongTemp(songFile, fileTempPath);
|
||||
System.IO.File.Delete(fileTempPath);
|
||||
song.SongPath = fileTempPath;
|
||||
|
||||
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);
|
||||
|
||||
var filePath = dirMgr.SongDirectory;
|
||||
var songFilename = songFile.FileName;
|
||||
@@ -373,6 +377,8 @@ namespace Icarus.Controllers.Managers
|
||||
_logger.Info("Song successfully saved to filesystem");
|
||||
}
|
||||
|
||||
coverMgr.SaveCoverArtToDatabase(ref song, ref coverArt,
|
||||
coverArtStore);
|
||||
SaveSongToDatabase(song, songStore, albumStore, artistStore, genreStore,
|
||||
yearStore);
|
||||
}
|
||||
|
||||
@@ -101,6 +101,20 @@ namespace Icarus.Controllers.Utilities
|
||||
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)
|
||||
{
|
||||
try
|
||||
|
||||
@@ -68,6 +68,14 @@ namespace Icarus.Controllers.V1
|
||||
_genreRepository = HttpContext
|
||||
.RequestServices
|
||||
.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")]
|
||||
public async Task<IActionResult> Get(int id)
|
||||
{
|
||||
SongRepository context = HttpContext.RequestServices
|
||||
.GetService(typeof(SongRepository)) as SongRepository;
|
||||
var songMetaData = context.GetSong(id);
|
||||
Initialize();
|
||||
var songMetaData = _songRepository.GetSong(id);
|
||||
|
||||
SongData song = await _songMgr.RetrieveSong(songMetaData);
|
||||
|
||||
@@ -93,14 +100,6 @@ namespace Icarus.Controllers.V1
|
||||
{
|
||||
Initialize();
|
||||
|
||||
YearRepository yearStore = HttpContext.RequestServices
|
||||
.GetService(typeof(YearRepository)) as YearRepository;
|
||||
|
||||
CoverArtRepository coverArtRepository = HttpContext
|
||||
.RequestServices
|
||||
.GetService
|
||||
(typeof(CoverArtRepository)) as CoverArtRepository;
|
||||
|
||||
Console.WriteLine("Uploading song...");
|
||||
_logger.LogInformation("Uploading song...");
|
||||
|
||||
@@ -114,7 +113,7 @@ namespace Icarus.Controllers.V1
|
||||
|
||||
await _songMgr.SaveSongToFileSystem(sng, _songRepository,
|
||||
_albumRepository, _artistRepository,
|
||||
_genreRepository, yearStore, coverArtRepository);
|
||||
_genreRepository, _yearRepository, _coverArtRepository);
|
||||
}
|
||||
}
|
||||
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