Contining work on #55
This commit is contained in:
@@ -10,13 +10,6 @@ using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
using Id3;
|
||||
using Id3.Frames;
|
||||
using MySql.Data;
|
||||
using MySql.Data.MySqlClient;
|
||||
using NLog;
|
||||
using TagLib;
|
||||
|
||||
using Icarus.Controllers.Utilities;
|
||||
using Icarus.Models;
|
||||
using Icarus.Database.Contexts;
|
||||
@@ -27,14 +20,8 @@ namespace Icarus.Controllers.Managers
|
||||
public class SongManager : BaseManager
|
||||
{
|
||||
#region Fields
|
||||
private MySqlConnection _conn;
|
||||
private MySqlCommand _cmd;
|
||||
private MySqlDataAdapter _dataDump;
|
||||
private DataTable _results;
|
||||
private List<Song> _songs;
|
||||
private Song _song;
|
||||
private IConfiguration _config;
|
||||
private string _connectionString;
|
||||
private string _tempDirectoryRoot;
|
||||
private string _archiveDirectoryRoot;
|
||||
private string _compressedSongFilename;
|
||||
@@ -70,28 +57,20 @@ namespace Icarus.Controllers.Managers
|
||||
#region Constructors
|
||||
public SongManager()
|
||||
{
|
||||
Initialize();
|
||||
InitializeConnection();
|
||||
}
|
||||
|
||||
public SongManager(Song song)
|
||||
{
|
||||
Initialize();
|
||||
InitializeConnection();
|
||||
_song = song;
|
||||
}
|
||||
public SongManager(IConfiguration config)
|
||||
{
|
||||
_config = config;
|
||||
Initialize();
|
||||
InitializeConnection();
|
||||
}
|
||||
public SongManager(IConfiguration config, string tempDirectoryRoot)
|
||||
{
|
||||
_config = config;
|
||||
_tempDirectoryRoot = tempDirectoryRoot;
|
||||
Initialize();
|
||||
InitializeConnection();
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -193,91 +172,6 @@ namespace Icarus.Controllers.Managers
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveSongDetails()
|
||||
{
|
||||
try
|
||||
{
|
||||
using (MySqlConnection conn = new MySqlConnection(_connectionString))
|
||||
{
|
||||
conn.Open();
|
||||
string query = "INSERT INTO Songs(Title, AlbumTitle, Artist, Year, Genre, Duration, " +
|
||||
"Filename, SongPath) VALUES(@Title, @AlbumTitle, @Artist, @Year, @Genre, " +
|
||||
"@Duration, @Filename, @SongPath)";
|
||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@Title", _song.Title);
|
||||
cmd.Parameters.AddWithValue("@AlbumTitle", _song.AlbumTitle);
|
||||
cmd.Parameters.AddWithValue("@Artist", _song.Artist);
|
||||
cmd.Parameters.AddWithValue("@Year", _song.Year);
|
||||
cmd.Parameters.AddWithValue("@Genre", _song.Genre);
|
||||
cmd.Parameters.AddWithValue("@Duration", _song.Duration);
|
||||
cmd.Parameters.AddWithValue("@Filename", _song.Filename);
|
||||
cmd.Parameters.AddWithValue("@SongPath", _song.SongPath);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var exMsg = ex.Message;
|
||||
Console.WriteLine($"An Error Occurred: {exMsg}");
|
||||
}
|
||||
}
|
||||
public void SaveSongDetails(Song song)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (MySqlConnection conn = new MySqlConnection(_connectionString))
|
||||
{
|
||||
conn.Open();
|
||||
string query = "INSERT INTO Songs(Title, AlbumTitle, Artist, Year, Genre, Duration, " +
|
||||
", Filename, SongPath) VALUES(@Title, @AlbumTitle, @Artist, @Year, @Genre, " +
|
||||
"@Duration, @Filename, @SongPath)";
|
||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@Title", song.Title);
|
||||
cmd.Parameters.AddWithValue("@AlbumTitle", song.AlbumTitle);
|
||||
cmd.Parameters.AddWithValue("@Artist", song.Artist);
|
||||
cmd.Parameters.AddWithValue("@Year", song.Year);
|
||||
cmd.Parameters.AddWithValue("@Genre", song.Genre);
|
||||
cmd.Parameters.AddWithValue("@Duration", song.Duration);
|
||||
cmd.Parameters.AddWithValue("@Filename", song.Filename);
|
||||
cmd.Parameters.AddWithValue("@SongPath", song.SongPath);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var exMsg = ex.Message;
|
||||
Console.WriteLine($"An Error Occurred: {exMsg}");
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveSong(SongData songData)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (MySqlConnection conn = new MySqlConnection(_connectionString))
|
||||
{
|
||||
conn.Open();
|
||||
string query = "INSERT INTO SongData(Data) VALUES(@Data)";
|
||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@Data", songData.Data);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
var exMsg = ex.Message;
|
||||
Console.WriteLine($"An error occurred: {exMsg}");
|
||||
}
|
||||
}
|
||||
public async Task SaveSongToFileSystem(IFormFile song)
|
||||
{
|
||||
try
|
||||
@@ -363,15 +257,13 @@ namespace Icarus.Controllers.Managers
|
||||
var song = await SaveSongTemp(songFile, fileTempPath);
|
||||
song.SongPath = fileTempPath;
|
||||
|
||||
var sng = ConvertSongToSng(song);
|
||||
|
||||
var rootPath = _config.GetValue<string>("RootMusicPath");
|
||||
var strCount = rootPath.Length + song.Artist.Length +
|
||||
song.AlbumTitle.Length + 2;
|
||||
|
||||
var filePathSB = new StringBuilder(strCount);
|
||||
|
||||
DirectoryManager.create_directory(sng,
|
||||
DirectoryManager.create_directory(ConvertSongToSng(song),
|
||||
rootPath, filePathSB);
|
||||
|
||||
var filePath = filePathSB.ToString().Substring(0, strCount);
|
||||
@@ -411,90 +303,6 @@ namespace Icarus.Controllers.Managers
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<Song>> RetrieveAllSongDetails()
|
||||
{
|
||||
try
|
||||
{
|
||||
InitializeResults();
|
||||
_songs = new List<Song>();
|
||||
_conn.Open();
|
||||
string query = "SELECT * FROM Songs";
|
||||
|
||||
_cmd = new MySqlCommand(query, _conn);
|
||||
_cmd.ExecuteNonQuery();
|
||||
|
||||
_dataDump = new MySqlDataAdapter(_cmd);
|
||||
_dataDump.Fill(_results);
|
||||
_dataDump.Dispose();
|
||||
|
||||
await PopulateSongDetails();
|
||||
|
||||
|
||||
_conn.Close();
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var exMsg = ex.Message;
|
||||
Console.WriteLine($"An error ocurred: {exMsg}");
|
||||
}
|
||||
|
||||
return _songs;
|
||||
}
|
||||
public Song RetrieveSongDetails(int id)
|
||||
{
|
||||
DataTable results = new DataTable();
|
||||
|
||||
try
|
||||
{
|
||||
using (MySqlConnection conn = new MySqlConnection(_connectionString))
|
||||
{
|
||||
conn.Open();
|
||||
string query = "SELECT * FROM Songs WHERE Id=@Id";
|
||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@Id", id);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
|
||||
using (MySqlDataAdapter dataDump = new MySqlDataAdapter(cmd))
|
||||
{
|
||||
dataDump.Fill(results);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var exMsg = ex.Message;
|
||||
Console.WriteLine($"An error occurred");
|
||||
}
|
||||
|
||||
DataRow row = results.Rows[0];
|
||||
|
||||
return new Song
|
||||
{
|
||||
Id = Int32.Parse(row["Id"].ToString()),
|
||||
Filename = row["Filename"].ToString(),
|
||||
SongPath = row["SongPath"].ToString()
|
||||
};
|
||||
}
|
||||
public async Task<SongData> RetrieveSong(int id)
|
||||
{
|
||||
SongData song = new SongData();
|
||||
try
|
||||
{
|
||||
_song = RetrieveSongDetails(id);
|
||||
song = await RetrieveSongFromFileSystem(_song);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var exMsg = ex.Message;
|
||||
Console.WriteLine($"An error occurred: {exMsg}");
|
||||
}
|
||||
|
||||
return song;
|
||||
}
|
||||
public async Task<SongData> RetrieveSong(Song songMetaData)
|
||||
{
|
||||
SongData song = new SongData();
|
||||
@@ -512,7 +320,7 @@ namespace Icarus.Controllers.Managers
|
||||
return song;
|
||||
}
|
||||
|
||||
private Sng ConvertSongToSng(Song song)
|
||||
public static Sng ConvertSongToSng(Song song)
|
||||
{
|
||||
return new Sng
|
||||
{
|
||||
@@ -525,65 +333,10 @@ namespace Icarus.Controllers.Managers
|
||||
};
|
||||
}
|
||||
|
||||
private Song RetrieveMetaData(string filePath)
|
||||
{
|
||||
Song newSong = new Song
|
||||
{
|
||||
Title = "Untitled",
|
||||
Artist = "Untitled",
|
||||
AlbumTitle = "Untitled",
|
||||
Year = 0,
|
||||
Genre = "Untitled",
|
||||
Duration = 0,
|
||||
SongPath = ""
|
||||
};
|
||||
string title, artist, album, genre;
|
||||
int year, duration;
|
||||
|
||||
Console.WriteLine("Stripping song metadata");
|
||||
try
|
||||
{
|
||||
TagLib.File tfile = TagLib.File.Create(filePath);
|
||||
|
||||
|
||||
using (var mp3 = new Mp3(filePath))
|
||||
{
|
||||
Id3Tag tag = mp3.GetTag(Id3TagFamily.Version2X);
|
||||
title = tag.Title;
|
||||
Console.WriteLine("Title: {0}", title);
|
||||
newSong.Title = title;
|
||||
artist = tag.Artists;
|
||||
Console.WriteLine("Artist: {0}", artist);
|
||||
newSong.Artist = artist;
|
||||
album = tag.Album;
|
||||
Console.WriteLine("Album: {0}", album);
|
||||
newSong.AlbumTitle = album;
|
||||
genre = "Not Implemented";
|
||||
Console.WriteLine("Genre: {0}", genre);
|
||||
newSong.Genre = genre;
|
||||
year = (int)tag.Year;
|
||||
Console.WriteLine("Year: {0}", year);
|
||||
newSong.Year = year;
|
||||
duration = (int)tfile.Properties.Duration.TotalSeconds;
|
||||
Console.WriteLine("Duration: {0}", duration);
|
||||
newSong.Duration = duration;
|
||||
}
|
||||
|
||||
_song = newSong;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var msg = ex.Message;
|
||||
Console.WriteLine($"An error occurred when stripping metadata\n{msg}");
|
||||
_song = newSong;
|
||||
}
|
||||
|
||||
return newSong;
|
||||
}
|
||||
|
||||
private async Task<SongData> RetrieveSongFromFileSystem(Song details)
|
||||
{
|
||||
byte[] uncompressedSong = System.IO.File.ReadAllBytes(details.SongPath);
|
||||
byte[] uncompressedSong = await System.IO.File.ReadAllBytesAsync(details.SongPath);
|
||||
|
||||
return new SongData
|
||||
{
|
||||
@@ -652,27 +405,6 @@ namespace Icarus.Controllers.Managers
|
||||
mgr.DeleteEmptyDirectories(oldSong);
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
try
|
||||
{
|
||||
_connectionString = _config.GetConnectionString("IcarusDev");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error Occurred: {ex.Message}");
|
||||
}
|
||||
|
||||
}
|
||||
private void InitializeConnection()
|
||||
{
|
||||
_conn = new MySqlConnection(_connectionString);
|
||||
}
|
||||
private void InitializeResults()
|
||||
{
|
||||
_results = new DataTable();
|
||||
}
|
||||
|
||||
private void SaveSongToDatabase(Song song, SongRepository songStore, AlbumRepository albumStore,
|
||||
ArtistRepository artistStore)
|
||||
{
|
||||
@@ -1199,48 +931,6 @@ namespace Icarus.Controllers.Managers
|
||||
yearStore.DeleteYear(year);
|
||||
}
|
||||
|
||||
private async Task PopulateSongDetails()
|
||||
{
|
||||
foreach (DataRow row in _results.Rows)
|
||||
{
|
||||
Song song = new Song();
|
||||
foreach (DataColumn col in _results.Columns)
|
||||
{
|
||||
string colStr = col.ToString().ToUpper();
|
||||
switch (colStr)
|
||||
{
|
||||
case "ID":
|
||||
song.Id = Int32.Parse(row[col].ToString());
|
||||
break;
|
||||
case "TITLE":
|
||||
song.Title = row[col].ToString();
|
||||
break;
|
||||
case "ALBUM":
|
||||
song.AlbumTitle = row[col].ToString();
|
||||
break;
|
||||
case "ARTIST":
|
||||
song.Artist = row[col].ToString();
|
||||
break;
|
||||
case "YEAR":
|
||||
song.Year = Int32.Parse(row[col].ToString());
|
||||
break;
|
||||
case "GENRE":
|
||||
song.Genre = row[col].ToString();
|
||||
break;
|
||||
case "DURATION":
|
||||
song.Duration = Int32.Parse(row[col].ToString());
|
||||
break;
|
||||
case "FILENAME":
|
||||
song.Filename = row[col].ToString();
|
||||
break;
|
||||
case "SONGPATH":
|
||||
song.SongPath = row[col].ToString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
_songs.Add(song);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user