Implemented Tokens, Login Functionality, and download authorization #15, #27, #31, #34

This commit is contained in:
amazing-username
2019-04-28 22:29:15 +00:00
parent 6cc0b32563
commit 065abeecbc
7 changed files with 234 additions and 124 deletions
+65 -59
View File
@@ -5,6 +5,7 @@ using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
@@ -21,68 +22,71 @@ namespace Icarus.Controllers
{
#region Fields
private IConfiguration _config;
private SongManager _songMgr;
private string _songTempDir;
#endregion
private SongManager _songMgr;
private string _songTempDir;
#endregion
#region Properties
#endregion
#region Properties
#endregion
#region Constructor
public SongDataController(IConfiguration config)
{
_config = config;
_songTempDir = _config.GetValue<string>("TemporaryMusicPath");
_songMgr = new SongManager(config, _songTempDir);
}
#endregion
#region Constructor
public SongDataController(IConfiguration config)
{
_config = config;
_songTempDir = _config.GetValue<string>("TemporaryMusicPath");
_songMgr = new SongManager(config, _songTempDir);
}
#endregion
[HttpGet("{id}")]
//[Route("private-scoped")]
//[Authorize("download:songs")]
[Route("private-scoped")]
[Authorize("download:songs")]
public async Task<IActionResult> Get(int id)
{
MusicStoreContext context = HttpContext.RequestServices
.GetService(typeof(MusicStoreContext))
as MusicStoreContext;
var songMetaData = context.GetSong(id);
SongData song = await _songMgr.RetrieveSong(songMetaData);
return File(song.Data, "application/x-msdownload", songMetaData.Filename);
}
MusicStoreContext context = HttpContext
.RequestServices
.GetService(typeof(MusicStoreContext))
as MusicStoreContext;
var songMetaData = context.GetSong(id);
SongData song = await _songMgr.RetrieveSong(songMetaData);
return File(song.Data, "application/x-msdownload", songMetaData.Filename);
}
[HttpPost]
[Authorize("upload:songs")]
public async Task Post([FromForm(Name = "file")] List<IFormFile> songData)
{
try
{
MusicStoreContext context = HttpContext.RequestServices
.GetService(typeof(MusicStoreContext))
as MusicStoreContext;
try
{
MusicStoreContext context = HttpContext
.RequestServices
.GetService(typeof(MusicStoreContext))
as MusicStoreContext;
Console.WriteLine("Uploading song...");
Console.WriteLine("Uploading song...");
var uploads = _songTempDir;
Console.WriteLine($"Song Root Path {uploads}");
foreach (var sng in songData)
{
if (sng.Length > 0) {
await _songMgr.SaveSongToFileSystem(sng);
var song = _songMgr.SongDetails;
context.SaveSong(song);
Console.WriteLine("Song successfully saved");
}
var uploads = _songTempDir;
Console.WriteLine($"Song Root Path {uploads}");
foreach (var sng in songData)
{
if (sng.Length > 0) {
await _songMgr.SaveSongToFileSystem(sng);
var song = _songMgr.SongDetails;
context.SaveSong(song);
Console.WriteLine("Song successfully saved");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
[HttpPut("{id}")]
public void Put(int id, [FromBody] SongData song)
@@ -90,20 +94,22 @@ namespace Icarus.Controllers
}
[HttpDelete("{id}")]
[Authorize("delete:songs")]
public void Delete(int id)
{
MusicStoreContext context = HttpContext.RequestServices
.GetService(typeof(MusicStoreContext))
as MusicStoreContext;
var songMetaData = context.GetSong(id);
var result = _songMgr.DeleteSongFromFileSystem(songMetaData);
if (result)
{
context.DeleteSong(songMetaData.Id);
}
}
MusicStoreContext context = HttpContext
.RequestServices
.GetService(typeof(MusicStoreContext))
as MusicStoreContext;
var songMetaData = context.GetSong(id);
var result = _songMgr.DeleteSongFromFileSystem(songMetaData);
if (result)
{
context.DeleteSong(songMetaData.Id);
}
}
}
}