#93: Uploading wav songs works. Moving on to flac support
This commit is contained in:
@@ -2,7 +2,9 @@ namespace Icarus.Constants;
|
|||||||
|
|
||||||
public class FileExtensions
|
public class FileExtensions
|
||||||
{
|
{
|
||||||
|
// Contains the default audio file extension with period at the beginning
|
||||||
public static string DEFAULT_AUDIO_EXTENSION = WAV_EXTENSION;
|
public static string DEFAULT_AUDIO_EXTENSION = WAV_EXTENSION;
|
||||||
|
|
||||||
// Contains file extension with period at the beginning
|
// Contains file extension with period at the beginning
|
||||||
public static string MP3_EXTENSION = ".mp3";
|
public static string MP3_EXTENSION = ".mp3";
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ public class CoverArtManager : BaseManager
|
|||||||
private string _rootCoverArtPath;
|
private string _rootCoverArtPath;
|
||||||
private CoverArtContext _coverArtContext;
|
private CoverArtContext _coverArtContext;
|
||||||
private byte[] _stockCoverArt = null;
|
private byte[] _stockCoverArt = null;
|
||||||
|
private const string _filename = "CoverArt.png";
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
@@ -46,7 +47,7 @@ public class CoverArtManager : BaseManager
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var stockCoverArtPath = _rootCoverArtPath + "CoverArt.png";
|
var stockCoverArtPath = _rootCoverArtPath + _filename;
|
||||||
if (!string.Equals(stockCoverArtPath, coverArt.ImagePath(),
|
if (!string.Equals(stockCoverArtPath, coverArt.ImagePath(),
|
||||||
StringComparison.CurrentCultureIgnoreCase))
|
StringComparison.CurrentCultureIgnoreCase))
|
||||||
{
|
{
|
||||||
@@ -168,9 +169,9 @@ public class CoverArtManager : BaseManager
|
|||||||
if (System.IO.File.Exists(path))
|
if (System.IO.File.Exists(path))
|
||||||
_stockCoverArt = File.ReadAllBytes(path);
|
_stockCoverArt = File.ReadAllBytes(path);
|
||||||
|
|
||||||
if (!File.Exists(_rootCoverArtPath + "CoverArt.png"))
|
if (!File.Exists(_rootCoverArtPath + _filename))
|
||||||
{
|
{
|
||||||
File.WriteAllBytes(_rootCoverArtPath + "CoverArt.png",
|
File.WriteAllBytes(_rootCoverArtPath + _filename,
|
||||||
_stockCoverArt);
|
_stockCoverArt);
|
||||||
Console.WriteLine("Copied Stock Cover Art");
|
Console.WriteLine("Copied Stock Cover Art");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -178,18 +178,7 @@ public class SongManager : BaseManager
|
|||||||
|
|
||||||
await Task.Run(() =>
|
await Task.Run(() =>
|
||||||
{
|
{
|
||||||
using (var fileStream = new FileStream(filePath, FileMode.Create))
|
this.MoveSongToFinalDestination(tempPath, filePath);
|
||||||
{
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
song.SongDirectory = dirMgr.SongDirectory;
|
song.SongDirectory = dirMgr.SongDirectory;
|
||||||
@@ -197,8 +186,7 @@ public class SongManager : BaseManager
|
|||||||
var coverMgr = new CoverArtManager(_config);
|
var coverMgr = new CoverArtManager(_config);
|
||||||
var coverArt = coverMgr.SaveCoverArt(song);
|
var coverArt = coverMgr.SaveCoverArt(song);
|
||||||
|
|
||||||
coverMgr.SaveCoverArtToDatabase(ref song, ref coverArt);
|
SaveSongToDatabase(song, coverArt);
|
||||||
SaveSongToDatabase(song);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -208,7 +196,7 @@ public class SongManager : BaseManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Change the name of this method to only focus on wav files
|
// Change the name of this method to only focus on wav files
|
||||||
public void SaveSongToFileSystem(IFormFile songFile, IFormFile coverArtData, Song song)
|
public Song SaveSongToFileSystem(IFormFile songFile, IFormFile coverArtData, Song song)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(song.SongDirectory))
|
if (string.IsNullOrEmpty(song.SongDirectory))
|
||||||
{
|
{
|
||||||
@@ -246,24 +234,45 @@ public class SongManager : BaseManager
|
|||||||
var filePath = song.SongPath();
|
var filePath = song.SongPath();
|
||||||
_logger.Info($"Absolute song path: {filePath}");
|
_logger.Info($"Absolute song path: {filePath}");
|
||||||
|
|
||||||
using (var fileStream = new FileStream(filePath, FileMode.Create))
|
this.MoveSongToFinalDestination(tempPath, filePath);
|
||||||
|
|
||||||
|
SaveSongToDatabase(song, coverArt);
|
||||||
|
|
||||||
|
return song;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Needs to be implemented
|
||||||
|
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
|
||||||
|
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
|
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);
|
System.IO.File.Delete(sourcePath);
|
||||||
_logger.Info("Deleted temp song from filesystem: {0}", tempPath);
|
_logger.Info("Deleted temp song from filesystem: {0}", sourcePath);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fileStream.Write(songBytes, 0, songBytes.Count());
|
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);
|
System.IO.File.Delete(sourcePath);
|
||||||
_logger.Info("Deleted temp song from filesystem: {0}", tempPath);
|
_logger.Info("Deleted temp song from filesystem: {0}", sourcePath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -274,14 +283,6 @@ public class SongManager : BaseManager
|
|||||||
|
|
||||||
_logger.Info("Song successfully saved to filesystem");
|
_logger.Info("Song successfully saved to filesystem");
|
||||||
}
|
}
|
||||||
|
|
||||||
coverMgr.SaveCoverArtToDatabase(ref song, ref coverArt);
|
|
||||||
SaveSongToDatabase(song);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Needs to be implemented
|
|
||||||
public void SaveFlacSongToFileSystem(IFormFile songFile, IFormFile coverArtData, Song song)
|
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<SongData> RetrieveSong(Song songMetaData)
|
public async Task<SongData> RetrieveSong(Song songMetaData)
|
||||||
@@ -382,13 +383,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");
|
_logger.Info("Starting process to save the song to the database");
|
||||||
|
|
||||||
var albumMgr = new AlbumManager(_config);
|
var albumMgr = new AlbumManager(_config);
|
||||||
var artistMgr = new ArtistManager(_config);
|
var artistMgr = new ArtistManager(_config);
|
||||||
var genreMgr = new GenreManager(_config);
|
var genreMgr = new GenreManager(_config);
|
||||||
|
var coverMgr = new CoverArtManager(_config);
|
||||||
albumMgr.SaveAlbumToDatabase(ref song);
|
albumMgr.SaveAlbumToDatabase(ref song);
|
||||||
artistMgr.SaveArtistToDatabase(ref song);
|
artistMgr.SaveArtistToDatabase(ref song);
|
||||||
genreMgr.SaveGenreToDatabase(ref song);
|
genreMgr.SaveGenreToDatabase(ref song);
|
||||||
@@ -398,6 +400,8 @@ public class SongManager : BaseManager
|
|||||||
|
|
||||||
_songContext.Add(song);
|
_songContext.Add(song);
|
||||||
_songContext.SaveChanges();
|
_songContext.SaveChanges();
|
||||||
|
|
||||||
|
coverMgr.SaveCoverArtToDatabase(ref song, ref cover);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -41,32 +41,6 @@ public class MetadataRetriever
|
|||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
public static void PrintMetadata(Song song)
|
|
||||||
{
|
|
||||||
Console.WriteLine("\n\nMetadata of the song:");
|
|
||||||
Console.WriteLine($"ID: {song.Id}");
|
|
||||||
Console.WriteLine($"Title: {song.Title}");
|
|
||||||
Console.WriteLine($"Artist: {song.Artist}");
|
|
||||||
Console.WriteLine($"Album: {song.AlbumTitle}");
|
|
||||||
Console.WriteLine($"Genre: {song.Genre}");
|
|
||||||
Console.WriteLine($"Year: {song.Year}");
|
|
||||||
Console.WriteLine($"Duration: {song.Duration}");
|
|
||||||
Console.WriteLine($"AlbumID: {song.AlbumId}");
|
|
||||||
Console.WriteLine($"ArtistID: {song.ArtistId}");
|
|
||||||
Console.WriteLine($"GenreID: {song.GenreId}");
|
|
||||||
Console.WriteLine($"Song Path: {song.SongPath()}");
|
|
||||||
Console.WriteLine($"Filename: {song.Filename}");
|
|
||||||
Console.WriteLine("\n");
|
|
||||||
|
|
||||||
_logger.Info("Metadata of the song");
|
|
||||||
_logger.Info($"Title: {song.Title}");
|
|
||||||
_logger.Info($"Artist: {song.Artist}");
|
|
||||||
_logger.Info($"Album: {song.AlbumTitle}");
|
|
||||||
_logger.Info($"Genre: {song.Genre}");
|
|
||||||
_logger.Info($"Year: {song.Year}");
|
|
||||||
_logger.Info($"Duration: {song.Duration}");
|
|
||||||
}
|
|
||||||
|
|
||||||
public string CoverArtFileExtensionType(CoverArt cover)
|
public string CoverArtFileExtensionType(CoverArt cover)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Retrieving CoverArt file extension type");
|
Console.WriteLine("Retrieving CoverArt file extension type");
|
||||||
@@ -100,7 +74,6 @@ public class MetadataRetriever
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var fileType = FileTypeChecker.FileTypeValidator.GetFileType(fileStream);
|
var fileType = FileTypeChecker.FileTypeValidator.GetFileType(fileStream);
|
||||||
Console.WriteLine($"Filetype: {fileType}");
|
Console.WriteLine($"Filetype: {fileType}");
|
||||||
Console.WriteLine($"Extension: {fileType.Extension}");
|
Console.WriteLine($"Extension: {fileType.Extension}");
|
||||||
@@ -344,42 +317,6 @@ public class MetadataRetriever
|
|||||||
{
|
{
|
||||||
_updatedSong = song;
|
_updatedSong = song;
|
||||||
}
|
}
|
||||||
private void PrintMetadata()
|
|
||||||
{
|
|
||||||
Console.WriteLine("\n\nMetadata of the song:");
|
|
||||||
Console.WriteLine($"Title: {_title}");
|
|
||||||
Console.WriteLine($"Artist: {_artist}");
|
|
||||||
Console.WriteLine($"Album: {_album}");
|
|
||||||
Console.WriteLine($"Genre: {_genre}");
|
|
||||||
Console.WriteLine($"Year: {_year}");
|
|
||||||
Console.WriteLine($"Duration: {_duration}\n\n");
|
|
||||||
|
|
||||||
_logger.Info("Metadata of the song");
|
|
||||||
_logger.Info($"Title: {_title}");
|
|
||||||
_logger.Info($"Artist: {_artist}");
|
|
||||||
_logger.Info($"Album: {_album}");
|
|
||||||
_logger.Info($"Genre: {_genre}");
|
|
||||||
_logger.Info($"Year: {_year}");
|
|
||||||
_logger.Info($"Duration: {_duration}");
|
|
||||||
}
|
|
||||||
private void PrintMetadata(Song song, string message)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"\n\n{message}");
|
|
||||||
Console.WriteLine($"Title: {song.Title}");
|
|
||||||
Console.WriteLine($"Artist: {song.Artist}");
|
|
||||||
Console.WriteLine($"Album: {song.AlbumTitle}");
|
|
||||||
Console.WriteLine($"Genre: {song.Genre}");
|
|
||||||
Console.WriteLine($"Year: {song.Year}");
|
|
||||||
Console.WriteLine($"Duration: {song.Duration}\n\n");
|
|
||||||
|
|
||||||
_logger.Info(message);
|
|
||||||
_logger.Info($"Title: {_title}");
|
|
||||||
_logger.Info($"Artist: {_artist}");
|
|
||||||
_logger.Info($"Album: {_album}");
|
|
||||||
_logger.Info($"Genre: {_genre}");
|
|
||||||
_logger.Info($"Year: {_year}");
|
|
||||||
_logger.Info($"Duration: {_duration}");
|
|
||||||
}
|
|
||||||
|
|
||||||
private SortedDictionary<string, bool> CheckSongValues(Song song)
|
private SortedDictionary<string, bool> CheckSongValues(Song song)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -140,15 +140,17 @@ public class SongDataController : BaseController
|
|||||||
case "wav":
|
case "wav":
|
||||||
// TODO: Identify the song file type. Then save the media.
|
// TODO: Identify the song file type. Then save the media.
|
||||||
// Create a new method to save song flac files
|
// Create a new method to save song flac files
|
||||||
_songMgr.SaveSongToFileSystem(up.SongData, up.CoverArtData, song);
|
song = _songMgr.SaveSongToFileSystem(up.SongData, up.CoverArtData, song);
|
||||||
break;
|
break;
|
||||||
case "flac":
|
case "flac":
|
||||||
// TODO: Skeleton method
|
// TODO: Skeleton method
|
||||||
_songMgr.SaveFlacSongToFileSystem(up.SongData, up.CoverArtData, song);
|
song = _songMgr.SaveFlacSongToFileSystem(up.SongData, up.CoverArtData, song);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
return BadRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Ok(song);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -156,7 +158,7 @@ public class SongDataController : BaseController
|
|||||||
_logger.LogError(ex.Message, "An error occurred");
|
_logger.LogError(ex.Message, "An error occurred");
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok();
|
return BadRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("delete/{id}")]
|
[HttpDelete("delete/{id}")]
|
||||||
|
|||||||
@@ -56,6 +56,24 @@ public class Song
|
|||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
public void PrintMetadata()
|
||||||
|
{
|
||||||
|
Console.WriteLine("\n\nMetadata of the song:");
|
||||||
|
Console.WriteLine($"ID: {this.Id}");
|
||||||
|
Console.WriteLine($"Title: {this.Title}");
|
||||||
|
Console.WriteLine($"Artist: {this.Artist}");
|
||||||
|
Console.WriteLine($"Album: {this.AlbumTitle}");
|
||||||
|
Console.WriteLine($"Genre: {this.Genre}");
|
||||||
|
Console.WriteLine($"Year: {this.Year}");
|
||||||
|
Console.WriteLine($"Duration: {this.Duration}");
|
||||||
|
Console.WriteLine($"AlbumID: {this.AlbumId}");
|
||||||
|
Console.WriteLine($"ArtistID: {this.ArtistId}");
|
||||||
|
Console.WriteLine($"GenreID: {this.GenreId}");
|
||||||
|
Console.WriteLine($"Song Path: {this.SongPath()}");
|
||||||
|
Console.WriteLine($"Filename: {this.Filename}");
|
||||||
|
Console.WriteLine("\n");
|
||||||
|
}
|
||||||
|
|
||||||
public string SongPath()
|
public string SongPath()
|
||||||
{
|
{
|
||||||
var fullPath = SongDirectory;
|
var fullPath = SongDirectory;
|
||||||
|
|||||||
Reference in New Issue
Block a user