tsk-#76: Adding more code
This commit is contained in:
@@ -121,8 +121,6 @@ public class TokenManager : BaseManager
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Make this call to whenver media is being accessed or modified - cover art, genre,
|
|
||||||
// album, song, et cetera
|
|
||||||
public bool CanAccessSong(string token, Song song, AccessLevel accessLevel)
|
public bool CanAccessSong(string token, Song song, AccessLevel accessLevel)
|
||||||
{
|
{
|
||||||
if (accessLevel!.Level!.Equals(Models.AccessLevel.DefaultLevel().Level))
|
if (accessLevel!.Level!.Equals(Models.AccessLevel.DefaultLevel().Level))
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ public class AccessLevelController : BaseController
|
|||||||
{
|
{
|
||||||
this._logger = logger;
|
this._logger = logger;
|
||||||
this._config = config;
|
this._config = config;
|
||||||
_connectionString = this._config.GetConnectionString("DefaultConnection");
|
this._connectionString = this._config.GetConnectionString("DefaultConnection");
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -29,8 +29,8 @@ public class AccessLevelController : BaseController
|
|||||||
[HttpGet]
|
[HttpGet]
|
||||||
public IActionResult GetAccessLevels(int? id, int? songId)
|
public IActionResult GetAccessLevels(int? id, int? songId)
|
||||||
{
|
{
|
||||||
var accLevel = new Icarus.Models.AccessLevel { Id = 0 };
|
var accLevel = new Models.AccessLevel { Id = 0 };
|
||||||
var accessLevelContext = new Icarus.Database.Contexts.AccessLevelContext(_connectionString!);
|
var accessLevelContext = new Database.Contexts.AccessLevelContext(_connectionString!);
|
||||||
|
|
||||||
if (id != null)
|
if (id != null)
|
||||||
{
|
{
|
||||||
@@ -38,7 +38,7 @@ public class AccessLevelController : BaseController
|
|||||||
}
|
}
|
||||||
else if (songId != null)
|
else if (songId != null)
|
||||||
{
|
{
|
||||||
accLevel = accessLevelContext.AccessLevels!.FirstOrDefault(al => al.SongId == songId);
|
accLevel = accessLevelContext.GetAccessLevel(songId.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
var response = new GetAccessLevelsResponse { Data = new List<Models.AccessLevel>() };
|
var response = new GetAccessLevelsResponse { Data = new List<Models.AccessLevel>() };
|
||||||
@@ -115,7 +115,7 @@ public class GetAccessLevelsResponse
|
|||||||
[Newtonsoft.Json.JsonProperty("subject")]
|
[Newtonsoft.Json.JsonProperty("subject")]
|
||||||
public string? Subject { get; set; }
|
public string? Subject { get; set; }
|
||||||
[Newtonsoft.Json.JsonProperty("data")]
|
[Newtonsoft.Json.JsonProperty("data")]
|
||||||
public List<Icarus.Models.AccessLevel>? Data { get; set; }
|
public List<Models.AccessLevel>? Data { get; set; }
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,7 +125,7 @@ public class UpdateAccessLevelResponse
|
|||||||
[Newtonsoft.Json.JsonProperty("subject")]
|
[Newtonsoft.Json.JsonProperty("subject")]
|
||||||
public string? Subject { get; set; }
|
public string? Subject { get; set; }
|
||||||
[Newtonsoft.Json.JsonProperty("data")]
|
[Newtonsoft.Json.JsonProperty("data")]
|
||||||
public List<Icarus.Models.AccessLevel>? Data { get; set; }
|
public List<Models.AccessLevel>? Data { get; set; }
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -47,6 +47,20 @@ public class SongCompressedDataController : BaseController
|
|||||||
|
|
||||||
Console.WriteLine("Starting process of retrieving comrpessed song");
|
Console.WriteLine("Starting process of retrieving comrpessed song");
|
||||||
var sng = context.RetrieveRecord(new Song { Id = id });
|
var sng = context.RetrieveRecord(new Song { Id = id });
|
||||||
|
|
||||||
|
var tokenManager = new TokenManager(this._config!);
|
||||||
|
var accLvlContext = new AccessLevelContext(this._connectionString!);
|
||||||
|
var accessLevel = accLvlContext.GetAccessLevel(sng.Id);
|
||||||
|
var token = tokenManager.GetBearerToken(HttpContext);
|
||||||
|
if (token == null || accessLevel == null)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tokenManager.CanAccessSong(token, sng, accessLevel))
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
SongData song = await cmp.RetrieveCompressedSong(sng);
|
SongData song = await cmp.RetrieveCompressedSong(sng);
|
||||||
|
|
||||||
var filename = DirectoryManager.GenerateDownloadFilename(10, Constants.FileExtensions.ZIP_EXTENSION, sng.Title!, randomizeFilename);
|
var filename = DirectoryManager.GenerateDownloadFilename(10, Constants.FileExtensions.ZIP_EXTENSION, sng.Title!, randomizeFilename);
|
||||||
|
|||||||
@@ -86,6 +86,20 @@ public class SongController : BaseController
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var tokenManager = new TokenManager(this._config!);
|
||||||
|
var accLvlContext = new AccessLevelContext(this._connectionString!);
|
||||||
|
var accessLevel = accLvlContext.GetAccessLevel(song.Id);
|
||||||
|
var token = tokenManager.GetBearerToken(HttpContext);
|
||||||
|
if (token == null || accessLevel == null)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tokenManager.CanAccessSong(token, song, accessLevel))
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
var songRes = _songMgr.UpdateSong(song);
|
var songRes = _songMgr.UpdateSong(song);
|
||||||
|
|
||||||
return Ok(songRes);
|
return Ok(songRes);
|
||||||
|
|||||||
@@ -175,11 +175,21 @@ public class SongDataController : BaseController
|
|||||||
public IActionResult DeleteSong(int id)
|
public IActionResult DeleteSong(int id)
|
||||||
{
|
{
|
||||||
var songContext = new SongContext(_connectionString!);
|
var songContext = new SongContext(_connectionString!);
|
||||||
|
var songMetaData = songContext.RetrieveRecord(new Song { Id = id });
|
||||||
|
|
||||||
var songMetaData = new Song { Id = id };
|
var tokenManager = new TokenManager(this._config!);
|
||||||
Console.WriteLine($"Id {songMetaData.Id}");
|
var accLvlContext = new AccessLevelContext(this._connectionString!);
|
||||||
|
var accessLevel = accLvlContext.GetAccessLevel(songMetaData.Id);
|
||||||
|
var token = tokenManager.GetBearerToken(HttpContext);
|
||||||
|
if (token == null || accessLevel == null)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
songMetaData = songContext.RetrieveRecord(songMetaData);
|
if (!tokenManager.CanAccessSong(token, songMetaData, accessLevel))
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(songMetaData.Title))
|
if (string.IsNullOrEmpty(songMetaData.Title))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ namespace Icarus.Controllers.V1;
|
|||||||
public class SongStreamController : BaseController
|
public class SongStreamController : BaseController
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
|
private string? _connectionString;
|
||||||
private ILogger<SongStreamController>? _logger;
|
private ILogger<SongStreamController>? _logger;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -22,8 +23,9 @@ public class SongStreamController : BaseController
|
|||||||
#region Constructor
|
#region Constructor
|
||||||
public SongStreamController(ILogger<SongStreamController> logger, IConfiguration config)
|
public SongStreamController(ILogger<SongStreamController> logger, IConfiguration config)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
this._logger = logger;
|
||||||
_config = config;
|
this._config = config;
|
||||||
|
this._connectionString = this._config.GetConnectionString("DefaultConnection");
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -35,6 +37,22 @@ public class SongStreamController : BaseController
|
|||||||
var context = new SongContext(_config!.GetConnectionString("DefaultConnection")!);
|
var context = new SongContext(_config!.GetConnectionString("DefaultConnection")!);
|
||||||
|
|
||||||
var song = context.Songs!.FirstOrDefault(sng => sng.Id == id);
|
var song = context.Songs!.FirstOrDefault(sng => sng.Id == id);
|
||||||
|
if (song == null) {
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
var tokenManager = new Managers.TokenManager(this._config!);
|
||||||
|
var accLvlContext = new AccessLevelContext(this._connectionString!);
|
||||||
|
var accessLevel = accLvlContext.GetAccessLevel(song.Id);
|
||||||
|
var token = tokenManager.GetBearerToken(HttpContext);
|
||||||
|
if (token == null || accessLevel == null)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tokenManager.CanAccessSong(token, song, accessLevel))
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
var stream = new FileStream(song!.SongPath(), FileMode.Open, FileAccess.Read);
|
var stream = new FileStream(song!.SongPath(), FileMode.Open, FileAccess.Read);
|
||||||
stream.Position = 0;
|
stream.Position = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user