8900afdfd2
* Added script shell to convert wav files to flac * #93: Minor change to script to convert wav files to flac * #93: Added more code to for the music file conversion * #93: Script to convert wav files to flac files is done * #93: Clean up * #93: Starting work to implement flac support * #93: Minor update to conversion script * #93: Added dotnet manifest file Includes the dotnet-ef tool * #93: Updated README and migration script * #93: Created skeleton for saving flac files * #93: Making some changes to how the file extension is determined * #93: Updated gitignore * #93: Another gitignore update * #93: Making some changes to the process of saving a song to the filesystem * #93: Adding SQL script to drop and create database * #93: Saving uploaded song to a temporary location for processing * #93: Initializing some properties in the song model earlier * #93: Uploading wav songs works. Moving on to flac support * #93: Added more code to save flac files * #93: Updated some code that affects filename generation for audio files * #93: Song Model changes and support for different file types * #93: Working on build errors, removed DotnetZip dependency, and added new Ionic.Zlip.Netstandard dependency * #93: Reduce build errors * #93: Removed ID3 dependency * #93: Cleanup
183 lines
5.8 KiB
C#
183 lines
5.8 KiB
C#
using Icarus.Constants;
|
|
using Icarus.Controllers.Utilities;
|
|
using Icarus.Database.Contexts;
|
|
using Icarus.Models;
|
|
|
|
namespace Icarus.Controllers.Managers;
|
|
|
|
public class CoverArtManager : BaseManager
|
|
{
|
|
#region Fields
|
|
private string? _rootCoverArtPath;
|
|
private CoverArtContext? _coverArtContext;
|
|
private byte[]? _stockCoverArt = null;
|
|
private const string? _filename = "CoverArt.png";
|
|
#endregion
|
|
|
|
|
|
#region Constructors
|
|
public CoverArtManager(IConfiguration config)
|
|
{
|
|
_config = config;
|
|
_connectionString = _config.GetConnectionString("DefaultConnection");
|
|
_rootCoverArtPath = _config.GetValue<string>("CoverArtPath");
|
|
Initialize();
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region Methods
|
|
public void SaveCoverArtToDatabase(ref Song song, ref CoverArt coverArt)
|
|
{
|
|
_logger.Info("Saving cover art record to the database");
|
|
_coverArtContext!.Add(coverArt);
|
|
_coverArtContext.SaveChanges();
|
|
|
|
song.CoverArtId = coverArt.Id;
|
|
}
|
|
public void DeleteCoverArtFromDatabase(CoverArt coverArt)
|
|
{
|
|
_logger.Info("Attempting to delete cover art from the database");
|
|
|
|
_coverArtContext!.Attach(coverArt);
|
|
_coverArtContext.Remove(coverArt);
|
|
_coverArtContext.SaveChanges();
|
|
}
|
|
public void DeleteCoverArt(CoverArt coverArt)
|
|
{
|
|
try
|
|
{
|
|
var stockCoverArtPath = _rootCoverArtPath + _filename;
|
|
if (!string.Equals(stockCoverArtPath, coverArt.ImagePath(),
|
|
StringComparison.CurrentCultureIgnoreCase))
|
|
{
|
|
_logger.Info("Song does not contain the stock cover art");
|
|
File.Delete(coverArt.ImagePath());
|
|
_logger.Info("Cover art deleted from the filesystem");
|
|
}
|
|
else
|
|
{
|
|
_logger.Info("Song contains the stock cover art");
|
|
_logger.Info("Will not delete from from the filesystem");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var msg = ex.Message;
|
|
_logger.Error(msg, "An error occurred");
|
|
}
|
|
}
|
|
|
|
public CoverArt? SaveCoverArt(Song song)
|
|
{
|
|
try
|
|
{
|
|
var dirMgr = new DirectoryManager(_rootCoverArtPath!);
|
|
var defaultExtension = ".png";
|
|
dirMgr.CreateDirectory(song);
|
|
|
|
var coverArt = new CoverArt
|
|
{
|
|
SongTitle = song.Title!
|
|
};
|
|
|
|
var segment = coverArt.GenerateFilename(0);
|
|
coverArt.Directory = dirMgr.SongDirectory;
|
|
coverArt.Filename = segment + defaultExtension;
|
|
|
|
var metaData = new MetadataRetriever();
|
|
var imgBytes = metaData.RetrieveCoverArtBytes(song);
|
|
|
|
if (imgBytes != null)
|
|
{
|
|
_logger.Info("Saving cover art to the filesystem");
|
|
File.WriteAllBytes(coverArt.ImagePath(), imgBytes);
|
|
}
|
|
else
|
|
{
|
|
_logger.Info("Song has no cover art, applying stock cover art");
|
|
|
|
var coverArtFilePath = _rootCoverArtPath + $"{segment}{defaultExtension}";
|
|
coverArt.Directory = DirectoryPaths.CoverArtDirectory;
|
|
coverArt.Filename = DirectoryPaths.CoverArtFilename;
|
|
metaData.UpdateCoverArt(song, coverArt);
|
|
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;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var msg = ex.Message;
|
|
_logger.Error(msg, "An error occurred");
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public CoverArt SaveCoverArt(IFormFile data, Song song)
|
|
{
|
|
var cover = new CoverArt { SongTitle = song.Title };
|
|
|
|
try
|
|
{
|
|
MetadataRetriever metaData = new MetadataRetriever();
|
|
var dirMgr = new DirectoryManager(_rootCoverArtPath!);
|
|
cover.Type = metaData.FileExtensionType(data);
|
|
var defaultExtension = "." + cover.Type;
|
|
dirMgr.CreateDirectory(song);
|
|
|
|
var segment = cover.GenerateFilename(0);
|
|
var imagePath = dirMgr.SongDirectory + segment + defaultExtension;
|
|
|
|
cover.Directory = dirMgr.SongDirectory;
|
|
cover.Filename = segment + defaultExtension;
|
|
|
|
using (var fileStream = new FileStream(cover.ImagePath(), FileMode.Create))
|
|
{
|
|
data.CopyTo(fileStream);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error(ex.Message, "An error occurred");
|
|
}
|
|
|
|
return cover;
|
|
}
|
|
|
|
public CoverArt GetCoverArt(Song song)
|
|
{
|
|
var title = song.Title;
|
|
var cov = _coverArtContext!.CoverArtImages!.FirstOrDefault(cov => cov.SongTitle!.Equals(title));
|
|
return cov!;
|
|
}
|
|
|
|
private void Initialize()
|
|
{
|
|
_coverArtContext = new CoverArtContext(_connectionString!);
|
|
var path = DirectoryPaths.CoverArtDirectory + DirectoryPaths.CoverArtFilename;
|
|
|
|
if (System.IO.File.Exists(path))
|
|
_stockCoverArt = File.ReadAllBytes(path);
|
|
|
|
if (!File.Exists(_rootCoverArtPath + _filename))
|
|
{
|
|
File.WriteAllBytes(_rootCoverArtPath + _filename,
|
|
_stockCoverArt!);
|
|
Console.WriteLine("Copied Stock Cover Art");
|
|
}
|
|
}
|
|
#endregion
|
|
}
|