#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
|
||||
{
|
||||
// Contains the default audio file extension with period at the beginning
|
||||
public static string DEFAULT_AUDIO_EXTENSION = WAV_EXTENSION;
|
||||
|
||||
// Contains file extension with period at the beginning
|
||||
public static string MP3_EXTENSION = ".mp3";
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ public class CoverArtManager : BaseManager
|
||||
private string _rootCoverArtPath;
|
||||
private CoverArtContext _coverArtContext;
|
||||
private byte[] _stockCoverArt = null;
|
||||
private const string _filename = "CoverArt.png";
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -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))
|
||||
{
|
||||
@@ -168,9 +169,9 @@ public class CoverArtManager : BaseManager
|
||||
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",
|
||||
File.WriteAllBytes(_rootCoverArtPath + _filename,
|
||||
_stockCoverArt);
|
||||
Console.WriteLine("Copied Stock Cover Art");
|
||||
}
|
||||
|
||||
@@ -178,18 +178,7 @@ 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;
|
||||
@@ -197,8 +186,7 @@ public class SongManager : BaseManager
|
||||
var coverMgr = new CoverArtManager(_config);
|
||||
var coverArt = coverMgr.SaveCoverArt(song);
|
||||
|
||||
coverMgr.SaveCoverArtToDatabase(ref song, ref coverArt);
|
||||
SaveSongToDatabase(song);
|
||||
SaveSongToDatabase(song, coverArt);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -208,7 +196,7 @@ public class SongManager : BaseManager
|
||||
}
|
||||
|
||||
// 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))
|
||||
{
|
||||
@@ -246,24 +234,45 @@ 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;
|
||||
}
|
||||
|
||||
// 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
|
||||
{
|
||||
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)
|
||||
@@ -274,14 +283,6 @@ public class SongManager : BaseManager
|
||||
|
||||
_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)
|
||||
@@ -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");
|
||||
|
||||
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);
|
||||
@@ -398,6 +400,8 @@ public class SongManager : BaseManager
|
||||
|
||||
_songContext.Add(song);
|
||||
_songContext.SaveChanges();
|
||||
|
||||
coverMgr.SaveCoverArtToDatabase(ref song, ref cover);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -41,32 +41,6 @@ public class MetadataRetriever
|
||||
|
||||
|
||||
#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)
|
||||
{
|
||||
Console.WriteLine("Retrieving CoverArt file extension type");
|
||||
@@ -100,7 +74,6 @@ public class MetadataRetriever
|
||||
}
|
||||
|
||||
|
||||
|
||||
var fileType = FileTypeChecker.FileTypeValidator.GetFileType(fileStream);
|
||||
Console.WriteLine($"Filetype: {fileType}");
|
||||
Console.WriteLine($"Extension: {fileType.Extension}");
|
||||
@@ -344,42 +317,6 @@ public class MetadataRetriever
|
||||
{
|
||||
_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)
|
||||
{
|
||||
|
||||
@@ -140,15 +140,17 @@ public class SongDataController : BaseController
|
||||
case "wav":
|
||||
// TODO: Identify the song file type. Then save the media.
|
||||
// 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;
|
||||
case "flac":
|
||||
// TODO: Skeleton method
|
||||
_songMgr.SaveFlacSongToFileSystem(up.SongData, up.CoverArtData, song);
|
||||
song = _songMgr.SaveFlacSongToFileSystem(up.SongData, up.CoverArtData, song);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
return Ok(song);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -156,7 +158,7 @@ public class SongDataController : BaseController
|
||||
_logger.LogError(ex.Message, "An error occurred");
|
||||
}
|
||||
|
||||
return Ok();
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
[HttpDelete("delete/{id}")]
|
||||
|
||||
@@ -56,6 +56,24 @@ public class Song
|
||||
|
||||
|
||||
#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()
|
||||
{
|
||||
var fullPath = SongDirectory;
|
||||
|
||||
Reference in New Issue
Block a user