Implemented functionality to download a song #11
This commit is contained in:
@@ -2,12 +2,17 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Configuration;
|
using System.Configuration;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
using Id3;
|
||||||
|
using Id3.Frames;
|
||||||
using MySql.Data;
|
using MySql.Data;
|
||||||
using MySql.Data.MySqlClient;
|
using MySql.Data.MySqlClient;
|
||||||
|
using TagLib;
|
||||||
|
|
||||||
using Icarus.Models;
|
using Icarus.Models;
|
||||||
|
|
||||||
@@ -20,10 +25,22 @@ namespace Icarus.Controllers.Managers
|
|||||||
private Song _song;
|
private Song _song;
|
||||||
private IConfiguration _config;
|
private IConfiguration _config;
|
||||||
private string _connectionString;
|
private string _connectionString;
|
||||||
|
private string _songRootDirectory;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
|
public Song SongDetails
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _song;
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_song = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
@@ -43,21 +60,57 @@ namespace Icarus.Controllers.Managers
|
|||||||
_config = config;
|
_config = config;
|
||||||
Initialize();
|
Initialize();
|
||||||
}
|
}
|
||||||
|
public SongManager(IConfiguration config, string songDirectoryRoot)
|
||||||
|
{
|
||||||
|
_config = config;
|
||||||
|
_songRootDirectory = songDirectoryRoot;
|
||||||
|
Initialize();
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
public void SaveSongDetails(Song song)
|
public void SaveSongDetails()
|
||||||
{
|
{
|
||||||
_song = song;
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Connection string is: {_connectionString}");
|
|
||||||
using (MySqlConnection conn = new MySqlConnection(_connectionString))
|
using (MySqlConnection conn = new MySqlConnection(_connectionString))
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
string query = "INSERT INTO Song(Title, Album, Artist, Year, Genre, Duration) " +
|
string query = "INSERT INTO Song(Title, Album, Artist, Year, Genre, Duration, " +
|
||||||
"VALUES(@Title, @Album, @Artist, @Year, @Genre, @Duration)";
|
"Filename, SongPath) VALUES(@Title, @Album, @Artist, @Year, @Genre, " +
|
||||||
|
"@Duration, @Filename, @SongPath)";
|
||||||
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@Title", _song.Title);
|
||||||
|
cmd.Parameters.AddWithValue("@Album", _song.Album);
|
||||||
|
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 Song(Title, Album, Artist, Year, Genre, Duration, " +
|
||||||
|
", Filename, SongPath) VALUES(@Title, @Album, @Artist, @Year, @Genre, " +
|
||||||
|
"@Duration, @Filename, @SongPath)";
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@Title", song.Title);
|
cmd.Parameters.AddWithValue("@Title", song.Title);
|
||||||
@@ -66,6 +119,8 @@ namespace Icarus.Controllers.Managers
|
|||||||
cmd.Parameters.AddWithValue("@Year", song.Year);
|
cmd.Parameters.AddWithValue("@Year", song.Year);
|
||||||
cmd.Parameters.AddWithValue("@Genre", song.Genre);
|
cmd.Parameters.AddWithValue("@Genre", song.Genre);
|
||||||
cmd.Parameters.AddWithValue("@Duration", song.Duration);
|
cmd.Parameters.AddWithValue("@Duration", song.Duration);
|
||||||
|
cmd.Parameters.AddWithValue("@Filename", song.Filename);
|
||||||
|
cmd.Parameters.AddWithValue("@SongPath", song.SongPath);
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
@@ -99,25 +154,39 @@ namespace Icarus.Controllers.Managers
|
|||||||
Console.WriteLine($"An error occurred: {exMsg}");
|
Console.WriteLine($"An error occurred: {exMsg}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public async Task SaveSongToFileSystem(IFormFile song)
|
||||||
public Song RetrieveSongDetails(int id)
|
|
||||||
{
|
{
|
||||||
return new Song();
|
try
|
||||||
|
{
|
||||||
|
var filePath = Path.Combine(_songRootDirectory, song.FileName);
|
||||||
|
using (var fileStream = new FileStream(filePath, FileMode.Create))
|
||||||
|
{
|
||||||
|
await song.CopyToAsync(fileStream);
|
||||||
|
_song = RetrieveMetaData(filePath);
|
||||||
|
_song.Filename = song.FileName;
|
||||||
|
SaveSongDetails();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var exMsg = ex.Message;
|
||||||
|
Console.WriteLine($"An error occurred: {exMsg}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public async Task<SongData> RetrieveSong(int id)
|
|
||||||
|
public async Task<Song> RetrieveSongDetails(int id)
|
||||||
{
|
{
|
||||||
SongData song = new SongData();
|
|
||||||
DataTable results = new DataTable();
|
DataTable results = new DataTable();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (MySqlConnection conn = new MySqlConnection(_connectionString))
|
using (MySqlConnection conn = new MySqlConnection(_connectionString))
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
string query = "SELECT Id, Data From SongData WHERE Id=@Id";
|
string query = "SELECT * FROM Song WHERE Id=@Id";
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@Id", id);
|
cmd.Parameters.AddWithValue("@Id", id);
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
|
|
||||||
using (MySqlDataAdapter dataDump = new MySqlDataAdapter(cmd))
|
using (MySqlDataAdapter dataDump = new MySqlDataAdapter(cmd))
|
||||||
@@ -126,9 +195,28 @@ namespace Icarus.Controllers.Managers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DataRow row = results.Rows[0];
|
}
|
||||||
song.Data = (byte[])row[1];
|
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 = await RetrieveSongDetails(id);
|
||||||
|
song = RetrieveSongFromFileSystem(_song);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -140,6 +228,55 @@ namespace Icarus.Controllers.Managers
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Song RetrieveMetaData(string filePath)
|
||||||
|
{
|
||||||
|
string title, artist, album, genre;
|
||||||
|
int year, duration;
|
||||||
|
|
||||||
|
TagLib.File tfile = TagLib.File.Create(filePath);
|
||||||
|
duration = (int)tfile.Properties.Duration.TotalSeconds;
|
||||||
|
|
||||||
|
|
||||||
|
using (var mp3 = new Mp3(filePath))
|
||||||
|
{
|
||||||
|
Id3Tag tag = mp3.GetTag(Id3TagFamily.Version2X);
|
||||||
|
title = tag.Title;
|
||||||
|
artist = tag.Artists;
|
||||||
|
album = tag.Album;
|
||||||
|
genre = "Not Implemented";
|
||||||
|
year = (int)tag.Year;
|
||||||
|
Console.WriteLine("Title: {0}", title);
|
||||||
|
Console.WriteLine("Artist: {0}", artist);
|
||||||
|
Console.WriteLine("Album: {0}", album);
|
||||||
|
Console.WriteLine("Genre: {0}", genre);
|
||||||
|
Console.WriteLine("Year: {0}", year);
|
||||||
|
Console.WriteLine("Duration: {0}", duration);
|
||||||
|
}
|
||||||
|
|
||||||
|
_song = new Song
|
||||||
|
{
|
||||||
|
Title = title,
|
||||||
|
Artist = artist,
|
||||||
|
Album = album,
|
||||||
|
Year = year,
|
||||||
|
Genre = genre,
|
||||||
|
Duration = duration,
|
||||||
|
SongPath = filePath
|
||||||
|
};
|
||||||
|
|
||||||
|
return _song;
|
||||||
|
}
|
||||||
|
|
||||||
|
SongData RetrieveSongFromFileSystem(Song details)
|
||||||
|
{
|
||||||
|
|
||||||
|
return new SongData
|
||||||
|
{
|
||||||
|
Data = System.IO.File.ReadAllBytes(details.SongPath)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void Initialize()
|
void Initialize()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ namespace Icarus.Controllers
|
|||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
public ActionResult<Song> Get(int id)
|
public ActionResult<Song> Get(int id)
|
||||||
{
|
{
|
||||||
Song song = _songMgr.RetrieveSongDetails(id);
|
Song song = _songMgr.RetrieveSongDetails(id).Result;
|
||||||
|
|
||||||
return song;
|
return song;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ namespace Icarus.Controllers
|
|||||||
#region Fields
|
#region Fields
|
||||||
private IConfiguration _config;
|
private IConfiguration _config;
|
||||||
private SongManager _songMgr;
|
private SongManager _songMgr;
|
||||||
private string _songDir = @"";
|
private string _songDir;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
@@ -33,7 +33,8 @@ namespace Icarus.Controllers
|
|||||||
public SongDataController(IConfiguration config)
|
public SongDataController(IConfiguration config)
|
||||||
{
|
{
|
||||||
_config = config;
|
_config = config;
|
||||||
_songMgr = new SongManager(config);
|
_songDir = _config.GetValue<string>("FilePath");
|
||||||
|
_songMgr = new SongManager(config, _songDir);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -54,7 +55,7 @@ namespace Icarus.Controllers
|
|||||||
|
|
||||||
song = await _songMgr.RetrieveSong(id);
|
song = await _songMgr.RetrieveSong(id);
|
||||||
|
|
||||||
return File(song.Data, "application/x-msdownload", "dfdf.mp3");
|
return File(song.Data, "application/x-msdownload", _songMgr.SongDetails.Filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
@@ -64,32 +65,13 @@ namespace Icarus.Controllers
|
|||||||
{
|
{
|
||||||
Console.WriteLine("Uploading song...");
|
Console.WriteLine("Uploading song...");
|
||||||
|
|
||||||
var uploads = Path.Combine(_songDir, "uploads");
|
var uploads = _songDir;
|
||||||
Console.WriteLine($"Song Path {uploads}");
|
Console.WriteLine($"Song Root Path {uploads}");
|
||||||
foreach (var sng in songData)
|
foreach (var sng in songData)
|
||||||
{
|
{
|
||||||
byte[] data;
|
byte[] data;
|
||||||
if (sng.Length > 0) {
|
if (sng.Length > 0) {
|
||||||
|
await _songMgr.SaveSongToFileSystem(sng);
|
||||||
using (var ms = new MemoryStream())
|
|
||||||
{
|
|
||||||
sng.CopyTo(ms);
|
|
||||||
data = ms.ToArray();
|
|
||||||
}
|
|
||||||
SongData songD = new SongData
|
|
||||||
{
|
|
||||||
Data = data
|
|
||||||
};
|
|
||||||
|
|
||||||
_songMgr.SaveSong(songD);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
var filePath = Path.Combine(uploads, sng.FileName);
|
|
||||||
using (var fileStream = new FileStream(filePath, FileMode.Create)) {
|
|
||||||
await sng.CopyToAsync(fileStream);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,12 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="ID3" Version="0.6.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.App" />
|
<PackageReference Include="Microsoft.AspNetCore.App" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
|
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
|
||||||
<PackageReference Include="mysql.data" Version="8.0.15" />
|
<PackageReference Include="mysql.data" Version="8.0.15" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
|
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
|
||||||
|
<PackageReference Include="taglib" Version="2.1.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -20,5 +20,9 @@ namespace Icarus.Models
|
|||||||
public string Genre { get; set; }
|
public string Genre { get; set; }
|
||||||
[JsonProperty("duration")]
|
[JsonProperty("duration")]
|
||||||
public int Duration { get; set; }
|
public int Duration { get; set; }
|
||||||
|
[JsonProperty("filename")]
|
||||||
|
public string Filename { get; set; }
|
||||||
|
[JsonProperty("song_path")]
|
||||||
|
public string SongPath { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user