#93: Add flac support (#101)

* 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
This commit was merged in pull request #101.
This commit is contained in:
KD
2024-07-27 13:19:12 -04:00
committed by GitHub
parent 4b3fa78336
commit 8900afdfd2
50 changed files with 787 additions and 493 deletions
+15 -15
View File
@@ -6,7 +6,7 @@ namespace Icarus.Controllers.Managers;
public class AlbumManager : BaseManager
{
#region Fields
private AlbumContext _albumContext;
private AlbumContext? _albumContext;
#endregion
@@ -19,7 +19,7 @@ public class AlbumManager : BaseManager
{
_config = config;
_connectionString = _config.GetConnectionString("DefaultConnection");
_albumContext = new AlbumContext(_connectionString);
_albumContext = new AlbumContext(_connectionString!);
}
#endregion
@@ -33,11 +33,11 @@ public class AlbumManager : BaseManager
album.Title = song.AlbumTitle;
album.AlbumArtist = song.Artist;
album.Year = song.Year.Value;
album.Year = song.Year!.Value;
var albumTitle = song.AlbumTitle;
var albumArtist = song.Artist;
var albumRetrieved = _albumContext.Albums.FirstOrDefault(alb => alb.Title.Equals(albumTitle) && alb.AlbumArtist.Equals(albumArtist));
var albumRetrieved = _albumContext!.Albums!.FirstOrDefault(alb => alb.Title!.Equals(albumTitle) && alb.AlbumArtist!.Equals(albumArtist));
if (albumRetrieved == null)
{
@@ -58,7 +58,7 @@ public class AlbumManager : BaseManager
public void DeleteAlbumFromDatabase(Song song)
{
var album = _albumContext.Albums.FirstOrDefault(alb => alb.Title.Equals(song.AlbumTitle));
var album = _albumContext!.Albums!.FirstOrDefault(alb => alb.Title!.Equals(song.AlbumTitle));
if (album == null)
{
@@ -72,7 +72,7 @@ public class AlbumManager : BaseManager
public Album UpdateAlbumInDatabase(Song oldSong, Song newSong)
{
var albumRecord = _albumContext.Albums.FirstOrDefault(alb => alb.Title.Equals(oldSong.AlbumTitle));
var albumRecord = _albumContext!.Albums!.FirstOrDefault(alb => alb.Title!.Equals(oldSong.AlbumTitle));
var oldAlbumTitle = oldSong.AlbumTitle;
var oldAlbumArtist = oldSong.Artist;
var newAlbumTitle = newSong.AlbumTitle;
@@ -86,16 +86,16 @@ public class AlbumManager : BaseManager
newAlbumTitle = oldAlbumTitle;
if ((string.IsNullOrEmpty(newAlbumTitle) && string.IsNullOrEmpty(newAlbumArtist) ||
oldAlbumTitle.Equals(newAlbumTitle) && oldAlbumArtist.Equals(newAlbumArtist)))
oldAlbumTitle!.Equals(newAlbumTitle) && oldAlbumArtist!.Equals(newAlbumArtist)))
{
_logger.Info("No change to the song's album");
return albumRecord;
return albumRecord!;
}
info = "Change to the song's album";
_logger.Info(info);
var existingAlbumRecord = _albumContext.Albums.FirstOrDefault(alb => alb.Title.Equals(oldSong.AlbumTitle));
var existingAlbumRecord = _albumContext.Albums!.FirstOrDefault(alb => alb.Title!.Equals(oldSong.AlbumTitle));
if (existingAlbumRecord == null)
{
_logger.Info("Creating new album record");
@@ -104,7 +104,7 @@ public class AlbumManager : BaseManager
{
Title = newAlbumTitle,
AlbumArtist = newAlbumArtist,
Year = newSong.Year.Value
Year = newSong.Year!.Value
};
_albumContext.Add(newAlbumRecord);
@@ -116,8 +116,8 @@ public class AlbumManager : BaseManager
{
_logger.Info("Updating existing album record");
existingAlbumRecord = _albumContext.Albums.FirstOrDefault(alb => alb.Title.Equals(newSong.AlbumTitle));
existingAlbumRecord.AlbumArtist = newAlbumArtist;
existingAlbumRecord = _albumContext.Albums!.FirstOrDefault(alb => alb.Title!.Equals(newSong.AlbumTitle));
existingAlbumRecord!.AlbumArtist = newAlbumArtist;
_albumContext.Update(existingAlbumRecord);
_albumContext.SaveChanges();
@@ -130,15 +130,15 @@ public class AlbumManager : BaseManager
{
if (SongsInAlbum(album) <= 1)
{
_albumContext.Remove(album);
_albumContext!.Remove(album);
_albumContext.SaveChanges();
}
}
private int SongsInAlbum(Album album)
{
var sngContext = new SongContext(_connectionString);
var songs = sngContext.Songs.Where(sng => sng.AlbumId == album.Id).ToList();
var sngContext = new SongContext(_connectionString!);
var songs = sngContext!.Songs!.Where(sng => sng.AlbumId == album.Id).ToList();
return songs.Count;
}
+16 -16
View File
@@ -6,7 +6,7 @@ namespace Icarus.Controllers.Managers;
public class ArtistManager : BaseManager
{
#region Fields
private ArtistContext _artistContext;
private ArtistContext? _artistContext;
#endregion
@@ -19,7 +19,7 @@ public class ArtistManager : BaseManager
{
_config = config;
_connectionString = _config.GetConnectionString("DefaultConnection");
_artistContext = new ArtistContext(_connectionString);
_artistContext = new ArtistContext(_connectionString!);
}
#endregion
@@ -35,7 +35,7 @@ public class ArtistManager : BaseManager
SongCount = 1
};
var artistRetrieved = _artistContext.Artists.FirstOrDefault(art => art.Name.Equals(artist.Name));
var artistRetrieved = _artistContext!.Artists.FirstOrDefault(art => art.Name!.Equals(artist.Name));
if (artistRetrieved == null)
{
@@ -53,11 +53,11 @@ public class ArtistManager : BaseManager
public Artist UpdateArtistInDatabase(Song oldSongRecord, Song newSongRecord)
{
var oldArtistRecord = _artistContext.Artists.FirstOrDefault(art => art.Name.Equals(oldSongRecord.AlbumTitle));
var oldArtistName = oldArtistRecord.Name;
var oldArtistRecord = _artistContext!.Artists.FirstOrDefault(art => art.Name!.Equals(oldSongRecord.AlbumTitle));
var oldArtistName = oldArtistRecord!.Name;
var newArtistName = newSongRecord.Artist;
if (string.IsNullOrEmpty(newArtistName) || oldArtistName.Equals(newArtistName))
if (string.IsNullOrEmpty(newArtistName) || oldArtistName!.Equals(newArtistName))
{
_logger.Info("No change to the song's Artist");
return oldArtistRecord;
@@ -73,7 +73,7 @@ public class ArtistManager : BaseManager
_artistContext.SaveChanges();
}
if (!(_artistContext.Artists.FirstOrDefault(art => art.Name.Equals(oldSongRecord.AlbumTitle)) != null))
if (!(_artistContext.Artists.FirstOrDefault(art => art.Name!.Equals(oldSongRecord.AlbumTitle)) != null))
{
_logger.Info("Creating new artist record");
@@ -91,36 +91,36 @@ public class ArtistManager : BaseManager
{
_logger.Info("Updating existing artist record");
var existingArtistRecord = _artistContext.Artists.FirstOrDefault(art => art.Name.Equals(newSongRecord.AlbumTitle));
var existingArtistRecord = _artistContext.Artists.FirstOrDefault(art => art.Name!.Equals(newSongRecord.AlbumTitle));
_artistContext.Update(existingArtistRecord);
_artistContext.Update(existingArtistRecord!);
_artistContext.SaveChanges();
return existingArtistRecord;
return existingArtistRecord!;
}
}
public void DeleteArtistFromDatabase(Song song)
{
if (!(_artistContext.Artists.FirstOrDefault(art => art.Name.Equals(song.Artist)) != null))
if (!(_artistContext!.Artists.FirstOrDefault(art => art.Name!.Equals(song.Artist)) != null))
{
_logger.Info("Cannot delete the artist record because it does not exist");
return;
}
var artist = _artistContext.Artists.FirstOrDefault(art => art.Name.Equals(song.Artist));
var artist = _artistContext.Artists.FirstOrDefault(art => art.Name!.Equals(song.Artist));
if (SongsOfArtist(artist) <= 1)
if (SongsOfArtist(artist!) <= 1)
{
_artistContext.Remove(artist);
_artistContext!.Remove(artist!);
_artistContext.SaveChanges();
}
}
private int SongsOfArtist(Artist artist)
{
var sngContext = new SongContext(_connectionString);
var songs = sngContext.Songs.Where(sng => sng.ArtistId == artist.Id).ToList();
var sngContext = new SongContext(_connectionString!);
var songs = sngContext.Songs!.Where(sng => sng.ArtistId == artist.Id).ToList();
return songs.Count;
}
+2 -2
View File
@@ -7,7 +7,7 @@ public class BaseManager
{
#region Fields
protected static Logger _logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
protected IConfiguration _config;
protected string _connectionString;
protected IConfiguration? _config;
protected string? _connectionString;
#endregion
}
+19 -16
View File
@@ -8,9 +8,10 @@ namespace Icarus.Controllers.Managers;
public class CoverArtManager : BaseManager
{
#region Fields
private string _rootCoverArtPath;
private CoverArtContext _coverArtContext;
private byte[] _stockCoverArt = null;
private string? _rootCoverArtPath;
private CoverArtContext? _coverArtContext;
private byte[]? _stockCoverArt = null;
private const string? _filename = "CoverArt.png";
#endregion
@@ -29,7 +30,7 @@ public class CoverArtManager : BaseManager
public void SaveCoverArtToDatabase(ref Song song, ref CoverArt coverArt)
{
_logger.Info("Saving cover art record to the database");
_coverArtContext.Add(coverArt);
_coverArtContext!.Add(coverArt);
_coverArtContext.SaveChanges();
song.CoverArtId = coverArt.Id;
@@ -38,7 +39,7 @@ public class CoverArtManager : BaseManager
{
_logger.Info("Attempting to delete cover art from the database");
_coverArtContext.Attach(coverArt);
_coverArtContext!.Attach(coverArt);
_coverArtContext.Remove(coverArt);
_coverArtContext.SaveChanges();
}
@@ -46,7 +47,7 @@ public class CoverArtManager : BaseManager
{
try
{
var stockCoverArtPath = _rootCoverArtPath + "CoverArt.png";
var stockCoverArtPath = _rootCoverArtPath + _filename;
if (!string.Equals(stockCoverArtPath, coverArt.ImagePath(),
StringComparison.CurrentCultureIgnoreCase))
{
@@ -67,17 +68,17 @@ public class CoverArtManager : BaseManager
}
}
public CoverArt SaveCoverArt(Song song)
public CoverArt? SaveCoverArt(Song song)
{
try
{
var dirMgr = new DirectoryManager(_rootCoverArtPath);
var dirMgr = new DirectoryManager(_rootCoverArtPath!);
var defaultExtension = ".png";
dirMgr.CreateDirectory(song);
var coverArt = new CoverArt
{
SongTitle = song.Title
SongTitle = song.Title!
};
var segment = coverArt.GenerateFilename(0);
@@ -102,7 +103,7 @@ public class CoverArtManager : BaseManager
metaData.UpdateCoverArt(song, coverArt);
coverArt.Directory = this._rootCoverArtPath;
coverArt.Filename = $"{segment}{defaultExtension}";
File.WriteAllBytes(coverArt.ImagePath(), _stockCoverArt);
File.WriteAllBytes(coverArt.ImagePath(), _stockCoverArt!);
}
coverArt.Type = metaData.CoverArtFileExtensionType(coverArt);
@@ -131,7 +132,7 @@ public class CoverArtManager : BaseManager
try
{
MetadataRetriever metaData = new MetadataRetriever();
var dirMgr = new DirectoryManager(_rootCoverArtPath);
var dirMgr = new DirectoryManager(_rootCoverArtPath!);
cover.Type = metaData.FileExtensionType(data);
var defaultExtension = "." + cover.Type;
dirMgr.CreateDirectory(song);
@@ -157,21 +158,23 @@ public class CoverArtManager : BaseManager
public CoverArt GetCoverArt(Song song)
{
return _coverArtContext.CoverArtImages.FirstOrDefault(cov => cov.SongTitle.Equals(song.Title));
var title = song.Title;
var cov = _coverArtContext!.CoverArtImages!.FirstOrDefault(cov => cov.SongTitle!.Equals(title));
return cov!;
}
private void Initialize()
{
_coverArtContext = new CoverArtContext(_connectionString);
_coverArtContext = new CoverArtContext(_connectionString!);
var path = DirectoryPaths.CoverArtDirectory + DirectoryPaths.CoverArtFilename;
if (System.IO.File.Exists(path))
_stockCoverArt = File.ReadAllBytes(path);
if (!File.Exists(_rootCoverArtPath + "CoverArt.png"))
if (!File.Exists(_rootCoverArtPath + _filename))
{
File.WriteAllBytes(_rootCoverArtPath + "CoverArt.png",
_stockCoverArt);
File.WriteAllBytes(_rootCoverArtPath + _filename,
_stockCoverArt!);
Console.WriteLine("Copied Stock Cover Art");
}
}
+15 -15
View File
@@ -7,14 +7,14 @@ namespace Icarus.Controllers.Managers;
public class DirectoryManager : BaseManager
{
#region Fields
private Song _song;
private string _rootSongDirectory;
private string _songDirectory;
private Song? _song;
private string? _rootSongDirectory;
private string? _songDirectory;
#endregion
#region Properties
public string SongDirectory
public string? SongDirectory
{
get => _songDirectory;
set => _songDirectory = value;
@@ -65,7 +65,7 @@ public class DirectoryManager : BaseManager
public void CreateDirectory()
{
this.CreateDirectory(_song);
this.CreateDirectory(_song!);
}
public void CreateDirectory(Song song)
@@ -187,9 +187,9 @@ public class DirectoryManager : BaseManager
private class DirEnt
{
public string Pre { get; set;}
public string Path { get; set; }
public string Post { get; set; }
public string? Pre { get; set;}
public string? Path { get; set; }
public string? Post { get; set; }
}
private void GenerateDirectories(List<DirEnt> dirs)
@@ -197,7 +197,7 @@ public class DirectoryManager : BaseManager
foreach (var di in dirs)
{
_logger.Info(di.Pre);
Directory.CreateDirectory(di.Path);
Directory.CreateDirectory(di.Path!);
_logger.Info(di.Post);
}
}
@@ -207,10 +207,10 @@ public class DirectoryManager : BaseManager
switch (dirTypes)
{
case DirectoryType.Music:
_rootSongDirectory = _config.GetValue<string>("RootMusicPath");
_rootSongDirectory = _config!.GetValue<string>("RootMusicPath")!;
break;
case DirectoryType.CoverArt:
_rootSongDirectory = _config.GetValue<string>("CoverArtPath");
_rootSongDirectory = _config!.GetValue<string>("CoverArtPath")!;
break;
}
}
@@ -222,12 +222,12 @@ public class DirectoryManager : BaseManager
private string AlbumDirectory()
{
return AlbumDirectory(_song);
return AlbumDirectory(_song!);
}
private string AlbumDirectory(Song song)
{
var directory = ArtistDirectory(song);
var segment = SerializeValue(song.AlbumTitle);
var segment = SerializeValue(song.AlbumTitle!);
directory += $@"{segment}/";
Console.WriteLine($"Album directory {directory}");
@@ -235,12 +235,12 @@ public class DirectoryManager : BaseManager
}
private string ArtistDirectory()
{
return ArtistDirectory(_song);
return ArtistDirectory(_song!);
}
private string ArtistDirectory(Song song)
{
var directory = _rootSongDirectory;
var segment = SerializeValue(song.Artist);
var segment = SerializeValue(song.Artist!);
directory += $@"{segment}/";
Console.WriteLine($"Artist directory {directory}");
+16 -16
View File
@@ -6,7 +6,7 @@ namespace Icarus.Controllers.Managers;
public class GenreManager : BaseManager
{
#region Fields
private GenreContext _genreContext;
private GenreContext? _genreContext;
#endregion
@@ -19,7 +19,7 @@ public class GenreManager : BaseManager
{
_config = config;
_connectionString = _config.GetConnectionString("DefaultConnection");
_genreContext = new GenreContext(_connectionString);
_genreContext = new GenreContext(_connectionString!);
}
#endregion
@@ -36,7 +36,7 @@ public class GenreManager : BaseManager
};
var genreName = song.Genre;
var genreRetrieved = _genreContext.Genres.FirstOrDefault(gnr => gnr.GenreName.Equals(genreName));
var genreRetrieved = _genreContext!.Genres!.FirstOrDefault(gnr => gnr.GenreName!.Equals(genreName));
if (genreRetrieved == null)
{
@@ -53,11 +53,11 @@ public class GenreManager : BaseManager
public Genre UpdateGenreInDatabase(Song oldSongRecord, Song newSongRecord)
{
var oldGenreRecord = _genreContext.Genres.FirstOrDefault(gnr => gnr.GenreName.Equals(oldSongRecord.Genre));
var oldGenreName = oldGenreRecord.GenreName;
var oldGenreRecord = _genreContext!.Genres!.FirstOrDefault(gnr => gnr.GenreName!.Equals(oldSongRecord.Genre));
var oldGenreName = oldGenreRecord!.GenreName;
var newGenreName = newSongRecord.Genre;
if (string.IsNullOrEmpty(newGenreName) || oldGenreName.Equals(newGenreName))
if (string.IsNullOrEmpty(newGenreName) || oldGenreName!.Equals(newGenreName))
{
_logger.Info("No change to the song's Genre");
return oldGenreRecord;
@@ -73,7 +73,7 @@ public class GenreManager : BaseManager
_genreContext.SaveChanges();
}
if (!(_genreContext.Genres.FirstOrDefault(gnr => gnr.GenreName.Equals(oldSongRecord.Genre)) != null))
if (!(_genreContext.Genres!.FirstOrDefault(gnr => gnr.GenreName!.Equals(oldSongRecord.Genre)) != null))
{
_logger.Info("Creating new genre record");
@@ -91,36 +91,36 @@ public class GenreManager : BaseManager
{
_logger.Info("Updating existing genre record");
var existingGenreRecord = _genreContext.Genres.FirstOrDefault(gnr => gnr.GenreName.Equals(oldGenreRecord.GenreName));
var existingGenreRecord = _genreContext.Genres!.FirstOrDefault(gnr => gnr.GenreName!.Equals(oldGenreRecord.GenreName));
_genreContext.Update(existingGenreRecord);
_genreContext.Update(existingGenreRecord!);
_genreContext.SaveChanges();
return existingGenreRecord;
return existingGenreRecord!;
}
}
public void DeleteGenreFromDatabase(Song song)
{
if (!(_genreContext.Genres.FirstOrDefault(gnr => gnr.GenreName.Equals(song.Genre)) != null))
if (!(_genreContext!.Genres!.FirstOrDefault(gnr => gnr.GenreName!.Equals(song.Genre)) != null))
{
_logger.Info("Cannot delete the genre record because it does not exist");
return;
}
var genre = _genreContext.Genres.FirstOrDefault(gnr => gnr.GenreName.Equals(song.Genre));
var genre = _genreContext.Genres!.FirstOrDefault(gnr => gnr.GenreName!.Equals(song.Genre));
if (SongsInGenre(genre) <= 1)
if (SongsInGenre(genre!) <= 1)
{
_genreContext.Remove(genre);
_genreContext.Remove(genre!);
_genreContext.SaveChanges();
}
}
private int SongsInGenre(Genre genre)
{
var sngContext = new SongContext(_connectionString);
var songs = sngContext.Songs.Where(sng => sng.GenreId == genre.Id).ToList();
var sngContext = new SongContext(_connectionString!);
var songs = sngContext.Songs!.Where(sng => sng.GenreId == genre.Id).ToList();
return songs.Count;
}
+129 -91
View File
@@ -9,26 +9,26 @@ namespace Icarus.Controllers.Managers;
public class SongManager : BaseManager
{
#region Fields
private string _tempDirectoryRoot;
private string _archiveDirectoryRoot;
private string _compressedSongFilename;
private string _message;
private SongContext _songContext;
private string? _tempDirectoryRoot;
private string? _archiveDirectoryRoot;
private string? _compressedSongFilename;
private string? _message;
private SongContext? _songContext;
#endregion
#region Properties
public string ArchiveDirectoryRoot
public string? ArchiveDirectoryRoot
{
get => _archiveDirectoryRoot;
set => _archiveDirectoryRoot = value;
}
public string CompressedSongFilename
public string? CompressedSongFilename
{
get => _compressedSongFilename;
set => _compressedSongFilename = value;
}
public string Message
public string? Message
{
get => _message;
set => _message = value;
@@ -64,7 +64,7 @@ public class SongManager : BaseManager
try
{
var oldSongRecord = _songContext.RetrieveRecord(song);
var oldSongRecord = _songContext!.RetrieveRecord(song);
song.Filename = oldSongRecord.Filename;
song.SongDirectory = oldSongRecord.SongDirectory;
@@ -73,19 +73,19 @@ public class SongManager : BaseManager
var updatedSong = updateMetadata.UpdatedSongRecord;
var albMgr = new AlbumManager(_config);
var gnrMgr = new GenreManager(_config);
var artMgr = new ArtistManager(_config);
var updatedAlbum = albMgr.UpdateAlbumInDatabase(oldSongRecord, updatedSong);
var albMgr = new AlbumManager(_config!);
var gnrMgr = new GenreManager(_config!);
var artMgr = new ArtistManager(_config!);
var updatedAlbum = albMgr.UpdateAlbumInDatabase(oldSongRecord, updatedSong!);
oldSongRecord.AlbumId = updatedAlbum.Id;
var updatedArtist = artMgr.UpdateArtistInDatabase(oldSongRecord, updatedSong);
var updatedArtist = artMgr.UpdateArtistInDatabase(oldSongRecord, updatedSong!);
oldSongRecord.ArtistId = updatedArtist.Id;
var updatedGenre = gnrMgr.UpdateGenreInDatabase(oldSongRecord, updatedSong);
var updatedGenre = gnrMgr.UpdateGenreInDatabase(oldSongRecord, updatedSong!);
oldSongRecord.GenreId = updatedGenre.Id;
UpdateSongInDatabase(ref oldSongRecord, ref updatedSong, ref result);
UpdateSongInDatabase(ref oldSongRecord, ref updatedSong!, ref result);
DeleteEmptyDirectories(ref oldSongRecord, ref updatedSong);
}
@@ -109,7 +109,7 @@ public class SongManager : BaseManager
var songPath = songMetaData.SongPath();
File.Delete(songPath);
successful = true;
DirectoryManager dirMgr = new DirectoryManager(_config, songMetaData);
DirectoryManager dirMgr = new DirectoryManager(_config!, songMetaData);
dirMgr.DeleteEmptyDirectories();
Console.WriteLine("Song successfully deleted");
}
@@ -123,7 +123,7 @@ public class SongManager : BaseManager
public bool DoesSongExist(Song song)
{
if (!_songContext.DoesRecordExist(song))
if (!_songContext!.DoesRecordExist(song))
{
return false;
}
@@ -142,7 +142,7 @@ public class SongManager : BaseManager
}
_logger.Info("Song deleted from the filesystem");
var coverMgr = new CoverArtManager(_config);
var coverMgr = new CoverArtManager(_config!);
var coverArt = coverMgr.GetCoverArt(song);
coverMgr.DeleteCoverArt(coverArt);
@@ -166,11 +166,11 @@ public class SongManager : BaseManager
var song = await SaveSongTemp(songFile);
DirectoryManager dirMgr = new DirectoryManager(_config, song);
DirectoryManager dirMgr = new DirectoryManager(_config!, song);
dirMgr.CreateDirectory();
var tempPath = song.SongPath();
song.Filename = song.GenerateFilename(1);
song.Filename = song.GenerateFilename(true, AudioFileExtensionsType.WAV);
var filePath = $"{dirMgr.SongDirectory}{song.Filename}";
_logger.Info($"Absolute song path: {filePath}");
@@ -178,27 +178,15 @@ public class SongManager : BaseManager
await Task.Run(() =>
{
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
var songBytes = System.IO.File.ReadAllBytes(tempPath);
_logger.Info("Saving song to the filesystem");
fileStream.Write(songBytes, 0, songBytes.Count());
System.IO.File.Delete(tempPath);
_logger.Info("Deleting temp file");
_logger.Info("Song successfully saved to filesystem");
}
this.MoveSongToFinalDestination(tempPath, filePath);
});
song.SongDirectory = dirMgr.SongDirectory;
var coverMgr = new CoverArtManager(_config);
var coverMgr = new CoverArtManager(_config!);
var coverArt = coverMgr.SaveCoverArt(song);
coverMgr.SaveCoverArtToDatabase(ref song, ref coverArt);
SaveSongToDatabase(song);
SaveSongToDatabase(song, coverArt);
}
catch (Exception ex)
{
@@ -207,14 +195,28 @@ public class SongManager : BaseManager
}
}
public void SaveSongToFileSystem(IFormFile songFile, IFormFile coverArtData, Song song)
// Change the name of this method to only focus on wav files
public Song SaveSongToFileSystem(IFormFile songFile, IFormFile coverArtData, Song song)
{
song.SongDirectory = _tempDirectoryRoot;
song.DateCreated = DateTime.Now;
if (string.IsNullOrEmpty(song.SongDirectory))
{
song.SongDirectory = _tempDirectoryRoot;
}
if (string.IsNullOrEmpty(song.Filename))
{
song.Filename = song.GenerateFilename(1);
switch (song.AudioType)
{
case "wav":
song.Filename = song.GenerateFilename(true, AudioFileExtensionsType.WAV);
break;
case "flac":
song.Filename = song.GenerateFilename(true, AudioFileExtensionsType.FLAC);
break;
default:
song.Filename = song.GenerateFilename(true, AudioFileExtensionsType.Default);
break;
}
}
_logger.Info($"Temporary directory: {_tempDirectoryRoot}");
@@ -223,16 +225,19 @@ public class SongManager : BaseManager
_logger.Info("Temporary song path: {0}", tempPath);
using (var filestream = new FileStream(tempPath, FileMode.Create))
if (!System.IO.File.Exists(tempPath))
{
_logger.Info("Saving song to temporary directory");
songFile.CopyTo(filestream);
using (var filestream = new FileStream(tempPath, FileMode.Create))
{
_logger.Info("Saving song to temporary directory");
songFile.CopyTo(filestream);
}
}
var coverMgr = new CoverArtManager(_config);
var coverMgr = new CoverArtManager(_config!);
var coverArt = coverMgr.SaveCoverArt(coverArtData, song);
DirectoryManager dirMgr = new DirectoryManager(_config, song);
DirectoryManager dirMgr = new DirectoryManager(_config!, song);
dirMgr.CreateDirectory();
song.SongDirectory = dirMgr.SongDirectory;
@@ -240,24 +245,66 @@ public class SongManager : BaseManager
var filePath = song.SongPath();
_logger.Info($"Absolute song path: {filePath}");
using (var fileStream = new FileStream(filePath, FileMode.Create))
this.MoveSongToFinalDestination(tempPath, filePath);
SaveSongToDatabase(song, coverArt);
return song;
}
public Song SaveFlacSongToFileSystem(IFormFile songFile, IFormFile coverArtData, Song song)
{
// Save temp song (Should already be saved to the filesystem by the time it gets to this method)
// Save cover art
// Update the song's metadata with the song object
// Save song to its final directory
// Save cover art to the database
// Save song to the database
var coverMgr = new CoverArtManager(_config!);
var coverArt = coverMgr.SaveCoverArt(coverArtData, song);
var meta = new Utilities.MetadataRetriever();
meta.UpdateMetadata(song, song);
DirectoryManager dirMgr = new DirectoryManager(_config!, song);
dirMgr.CreateDirectory();
var tempPath = song.SongPath();
song.SongDirectory = dirMgr.SongDirectory;
song.Filename = song.GenerateFilename(true, AudioFileExtensionsType.FLAC);
var filePath = song.SongPath();
_logger.Info($"Absolute song path: {filePath}");
this.MoveSongToFinalDestination(tempPath, filePath);
SaveSongToDatabase(song, coverArt);
return song;
}
private void MoveSongToFinalDestination(string sourcePath, string targetPath)
{
using (var fileStream = new FileStream(targetPath, FileMode.Create))
{
var songBytes = System.IO.File.ReadAllBytes(tempPath);
var songBytes = System.IO.File.ReadAllBytes(sourcePath);
try
{
if (System.IO.File.Exists(filePath) && System.IO.File.Exists(tempPath) && fileStream.Length > 0)
if (System.IO.File.Exists(sourcePath) && System.IO.File.Exists(sourcePath) && fileStream.Length > 0)
{
System.IO.File.Delete(tempPath);
_logger.Info("Deleted temp song from filesystem: {0}", tempPath);
System.IO.File.Delete(sourcePath);
_logger.Info("Deleted temp song from filesystem: {0}", sourcePath);
}
else
{
fileStream.Write(songBytes, 0, songBytes.Count());
_logger.Info("Saved song to filesystem: {0}", filePath);
_logger.Info("Saved song to filesystem: {0}", targetPath);
System.IO.File.Delete(tempPath);
_logger.Info("Deleted temp song from filesystem: {0}", tempPath);
System.IO.File.Delete(sourcePath);
_logger.Info("Deleted temp song from filesystem: {0}", sourcePath);
}
}
catch (Exception ex)
@@ -268,12 +315,7 @@ public class SongManager : BaseManager
_logger.Info("Song successfully saved to filesystem");
}
coverMgr.SaveCoverArtToDatabase(ref song, ref coverArt);
SaveSongToDatabase(song);
}
}
public async Task<SongData> RetrieveSong(Song songMetaData)
{
@@ -304,28 +346,21 @@ public class SongManager : BaseManager
Data = uncompressedSong
};
}
private async Task<Song> SaveSongTemp(IFormFile songFile)
public async Task<Song> SaveSongTemp(IFormFile songFile)
{
var song = new Song();
_logger.Info("Assigning song filename");
song.SongDirectory = _tempDirectoryRoot;
var filename = song.GenerateFilename(1);
var song = new Song { SongDirectory = this._tempDirectoryRoot! };
var filename = await song.GenerateFilenameAsync(false) + "-" + songFile.FileName;
song.Filename = filename;
var songPath = song.SongPath();
using (var filestream = new FileStream(song.SongPath(), FileMode.Create))
using (var filestream = new FileStream(songPath, FileMode.Create))
{
_logger.Info("Saving temp song: {0}", song.SongPath());
_logger.Info("Saving temp song: {0}", songPath);
await songFile.CopyToAsync(filestream);
}
await Task.Run(() =>
{
MetadataRetriever meta = new MetadataRetriever();
song = meta.RetrieveMetaData(song.SongPath());
});
song.SongDirectory = _tempDirectoryRoot;
song.DateCreated = DateTime.Now;
song.Filename = filename;
return song;
}
@@ -339,9 +374,9 @@ public class SongManager : BaseManager
var currentGenre = currentSong.Genre;
var currentYear = currentSong.Year;
if (!currentTitle.Equals(songUpdates.Title) || !currentArtist.Equals(songUpdates.Artist) ||
!currentAlbum.Equals(songUpdates.AlbumTitle) ||
!currentGenre.Equals(songUpdates.Genre) || currentYear != songUpdates.Year)
if (!currentTitle!.Equals(songUpdates.Title) || !currentArtist!.Equals(songUpdates.Artist) ||
!currentAlbum!.Equals(songUpdates.AlbumTitle) ||
!currentGenre!.Equals(songUpdates.Genre) || currentYear != songUpdates.Year)
return true;
return false;
@@ -349,7 +384,7 @@ public class SongManager : BaseManager
private void DeleteEmptyDirectories(ref Song oldSong, ref Song updatedSong)
{
DirectoryManager mgr = new DirectoryManager(_config);
DirectoryManager mgr = new DirectoryManager(_config!);
_logger.Info("Checking to see if there are any directories to delete");
mgr.DeleteEmptyDirectories(oldSong);
@@ -359,8 +394,8 @@ public class SongManager : BaseManager
{
try
{
_connectionString = _config.GetConnectionString("DefaultConnection");
_songContext = new SongContext(_connectionString);
_connectionString = _config!.GetConnectionString("DefaultConnection");
_songContext = new SongContext(_connectionString!);
}
catch (Exception ex)
{
@@ -370,13 +405,14 @@ public class SongManager : BaseManager
private void SaveSongToDatabase(Song song)
private void SaveSongToDatabase(Song song, CoverArt? cover)
{
_logger.Info("Starting process to save the song to the database");
var albumMgr = new AlbumManager(_config);
var artistMgr = new ArtistManager(_config);
var genreMgr = new GenreManager(_config);
var albumMgr = new AlbumManager(_config!);
var artistMgr = new ArtistManager(_config!);
var genreMgr = new GenreManager(_config!);
var coverMgr = new CoverArtManager(_config!);
albumMgr.SaveAlbumToDatabase(ref song);
artistMgr.SaveArtistToDatabase(ref song);
genreMgr.SaveGenreToDatabase(ref song);
@@ -384,8 +420,10 @@ public class SongManager : BaseManager
var info = "Saving Song to DB";
_logger.Info(info);
_songContext.Add(song);
_songContext.SaveChanges();
_songContext!.Add(song);
_songContext!.SaveChanges();
coverMgr.SaveCoverArtToDatabase(ref song, ref cover!);
}
@@ -430,7 +468,7 @@ public class SongManager : BaseManager
{
var updatedSongRecord = oldSongRecord;
var songContext = new SongContext(_connectionString);
var songContext = new SongContext(_connectionString!);
if (!SongRecordChanged(oldSongRecord, newSongRecord))
{
@@ -470,18 +508,18 @@ public class SongManager : BaseManager
newSongRecord = updatedSongRecord;
result.Message = "Successfully updated song";
result.SongTitle = updatedSongRecord.Title;
result.SongTitle = updatedSongRecord.Title!;
}
private void DeleteSongFromDatabase(Song song)
{
_logger.Info("Starting process to delete records related to the song from the database");
var albumMgr = new AlbumManager(_config);
var artistMgr = new ArtistManager(_config);
var genreMgr = new GenreManager(_config);
var sngContext = new SongContext(_connectionString);
sngContext.Songs.Remove(song);
var albumMgr = new AlbumManager(_config!);
var artistMgr = new ArtistManager(_config!);
var genreMgr = new GenreManager(_config!);
var sngContext = new SongContext(_connectionString!);
sngContext.Songs!.Remove(song);
sngContext.SaveChanges();
artistMgr.DeleteArtistFromDatabase(song);
albumMgr.DeleteAlbumFromDatabase(song);
+23 -23
View File
@@ -15,16 +15,16 @@ namespace Icarus.Controllers.Managers;
public class TokenManager : BaseManager
{
#region Fields
private string _clientId;
private string _clientSecret;
private string _privateKeyPath = string.Empty;
private string _privateKey = string.Empty;
private string _publicKeyPath = string.Empty;
private string _publicKey = string.Empty;
private string _audience;
private string _grantType;
private string _url;
private string SUCCESSFUL_TOKEN_MESSAGE = "Successfully retrieved token";
private string? _clientId;
private string? _clientSecret;
private string? _privateKeyPath = string.Empty;
private string? _privateKey = string.Empty;
private string? _publicKeyPath = string.Empty;
private string? _publicKey = string.Empty;
private string? _audience;
private string? _grantType;
private string? _url;
private string? SUCCESSFUL_TOKEN_MESSAGE = "Successfully retrieved token";
#endregion
@@ -46,7 +46,7 @@ public class TokenManager : BaseManager
{
_logger.Info("Preparing Auth0 API request");
var client = new RestClient(_url);
var client = new RestClient(_url!);
var request = new RestRequest("oauth/token", Method.POST);
var tokenRequest = RetrieveTokenRequest();
@@ -68,7 +68,7 @@ public class TokenManager : BaseManager
.DeserializeObject<TokenTierOne>(response.Content);
_logger.Info("Response deserialized");
return this.InitializeLoginResult(user, tokenResult);
return this.InitializeLoginResult(user, tokenResult!);
}
@@ -80,7 +80,7 @@ public class TokenManager : BaseManager
payload.Add(new Claim("user_id", user.Id.ToString(), ClaimValueTypes.Integer));
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(_config["JWT:Secret"]);
var key = Encoding.ASCII.GetBytes(_config!["JWT:Secret"]!);
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
@@ -101,7 +101,7 @@ public class TokenManager : BaseManager
return cl.Type.Equals("exp");
});
var expiredDate = DateTime.Parse(expClaim.Value);
var expiredDate = DateTime.Parse(expClaim!.Value);
var exp = Math.Floor((expiredDate - DateTime.UnixEpoch).TotalSeconds);
tokenResult.Expiration = Convert.ToInt32(exp);
@@ -197,7 +197,7 @@ public class TokenManager : BaseManager
var expiredDate = currentDate.AddMinutes(expLimit);
var issuer = "http://localhost:5002";
var audience = "http://localhost:5002";
var subject = _config["JWT:Subject"];
var subject = _config!["JWT:Subject"];
var claim = new List<System.Security.Claims.Claim>()
{
@@ -205,7 +205,7 @@ public class TokenManager : BaseManager
new Claim(JwtRegisteredClaimNames.Exp, expiredDate.ToString()),
new Claim(JwtRegisteredClaimNames.Aud, audience),
new Claim(JwtRegisteredClaimNames.Iss, issuer),
new Claim(JwtRegisteredClaimNames.Sub, subject),
new Claim(JwtRegisteredClaimNames.Sub, subject!),
new Claim(JwtRegisteredClaimNames.Iat, currentDate.ToString())
};
@@ -233,7 +233,7 @@ public class TokenManager : BaseManager
{
_logger.Info("Analyzing Auth0 information");
_clientId = _config["Auth0:ClientId"];
_clientId = _config!["Auth0:ClientId"];
_clientSecret = _config["Auth0:ClientSecret"];
_audience = _config["Auth0:ApiIdentifier"];
_grantType = "client_credentials";
@@ -258,23 +258,23 @@ public class TokenManager : BaseManager
private class TokenRequest
{
[JsonProperty("client_id")]
public string ClientId { get; set; }
public string? ClientId { get; set; }
[JsonProperty("client_secret")]
public string ClientSecret { get; set; }
public string? ClientSecret { get; set; }
[JsonProperty("audience")]
public string Audience { get; set; }
public string? Audience { get; set; }
[JsonProperty("grant_type")]
public string GrantType { get; set; }
public string? GrantType { get; set; }
}
private class TokenTierOne
{
[JsonProperty("access_token")]
public string AccessToken { get; set; }
public string? AccessToken { get; set; }
[JsonProperty("expires_in")]
public int Expiration { get; set; }
[JsonProperty("token_type")]
public string TokenType { get; set; }
public string? TokenType { get; set; }
}
#endregion
}