Can download a cover art via the cover art's id. Just need to have a stock cover art for songs without any and update the documentation. #50

This commit is contained in:
amazing-username
2019-07-13 16:04:39 -04:00
parent 1004f6c93d
commit 59895af3b4
3 changed files with 107 additions and 76 deletions
+1
View File
@@ -35,6 +35,7 @@ namespace Icarus.Controllers.Managers
song.CoverArtId = coverArt.CoverArtId; song.CoverArtId = coverArt.CoverArtId;
} }
public CoverArt SaveCoverArt(Song song) public CoverArt SaveCoverArt(Song song)
{ {
try try
+36 -10
View File
@@ -1,14 +1,19 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Icarus.Controllers.Managers;
using Icarus.Database.Repositories;
using Icarus.Models;
namespace Icarus.Controllers.V1 namespace Icarus.Controllers.V1
{ {
[Route("api/v1/coverart/song")] [Route("api/v1/coverart")]
[ApiController] [ApiController]
public class CoverArtController : ControllerBase public class CoverArtController : ControllerBase
{ {
@@ -19,18 +24,39 @@ namespace Icarus.Controllers.V1
#region Constructors #region Constructors
public CoverArtController(ILogger<CoverArtController> logger) public CoverArtController(ILogger<CoverArtController> logger)
{ {
_logger = logger; _logger = logger;
} }
#endregion #endregion
#region HTTP Routes #region HTTP Routes
[HttpGet("{id}")] [HttpGet("{id}")]
public IActionResult Get(int id) [Authorize("download:cover_art")]
{ public IActionResult Get(int id)
return NotFound(); {
} var coverArt = new CoverArt
{
CoverArtId = id
};
var coverArtRepository = HttpContext
.RequestServices
.GetService(
typeof(CoverArtRepository)) as CoverArtRepository;
coverArt = coverArtRepository.GetCoverArt(coverArt);
if (coverArt != null)
{
var coverArtBytes = System.IO.File.ReadAllBytes(
coverArt.ImagePath);
return File(coverArtBytes, "application/x-msdownload",
coverArt.SongTitle);
}
else
return NotFound();
}
#endregion #endregion
} }
} }
+70 -66
View File
@@ -41,62 +41,66 @@ namespace Icarus
// This method gets called by the runtime. Use this method to add services to the container. // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSingleton<IConfiguration>(Configuration); services.AddSingleton<IConfiguration>(Configuration);
string domain = $"https://{Configuration["Auth0:Domain"]}/"; string domain = $"https://{Configuration["Auth0:Domain"]}/";
services.AddAuthentication(options => services.AddAuthentication(options =>
{ {
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options => }).AddJwtBearer(options =>
{ {
options.Authority = domain; options.Authority = domain;
options.Audience = Configuration["Auth0:ApiIdentifier"]; options.Audience = Configuration["Auth0:ApiIdentifier"];
}); });
services.AddAuthorization(options => services.AddAuthorization(options =>
{ {
options.AddPolicy("download:songs", policy => options.AddPolicy("download:songs", policy =>
policy.Requirements policy.Requirements
.Add(new HasScopeRequirement("download:songs", domain))); .Add(new HasScopeRequirement("download:songs", domain)));
options.AddPolicy("upload:songs", policy => options.AddPolicy("download:cover_art", policy =>
policy.Requirements policy.Requirements
.Add(new HasScopeRequirement("upload:songs", domain))); .Add(new HasScopeRequirement("download:cover_art", domain)));
options.AddPolicy("delete:songs", policy => options.AddPolicy("upload:songs", policy =>
policy.Requirements policy.Requirements
.Add(new HasScopeRequirement("delete:songs", domain))); .Add(new HasScopeRequirement("upload:songs", domain)));
options.AddPolicy("read:song_details", policy => options.AddPolicy("delete:songs", policy =>
policy.Requirements policy.Requirements
.Add(new HasScopeRequirement("read:song_details", domain))); .Add(new HasScopeRequirement("delete:songs", domain)));
options.AddPolicy("update:songs", policy => options.AddPolicy("read:song_details", policy =>
policy.Requirements policy.Requirements
.Add(new HasScopeRequirement("update:songs", domain))); .Add(new HasScopeRequirement("read:song_details", domain)));
options.AddPolicy("read:artists", policy => options.AddPolicy("update:songs", policy =>
policy.Requirements policy.Requirements
.Add(new HasScopeRequirement("read:artists", domain))); .Add(new HasScopeRequirement("update:songs", domain)));
options.AddPolicy("read:albums", policy => options.AddPolicy("read:artists", policy =>
policy.Requirements policy.Requirements
.Add(new HasScopeRequirement("read:albums", domain))); .Add(new HasScopeRequirement("read:artists", domain)));
options.AddPolicy("read:genre", policy => options.AddPolicy("read:albums", policy =>
policy.Requirements policy.Requirements
.Add(new HasScopeRequirement("read:genre", domain))); .Add(new HasScopeRequirement("read:albums", domain)));
options.AddPolicy("read:year", policy => options.AddPolicy("read:genre", policy =>
policy.Requirements policy.Requirements
.Add(new HasScopeRequirement("read:year", domain))); .Add(new HasScopeRequirement("read:genre", domain)));
options.AddPolicy("stream:songs", policy => options.AddPolicy("read:year", policy =>
policy.Requirements policy.Requirements
.Add(new HasScopeRequirement("stream:songs", domain))); .Add(new HasScopeRequirement("read:year", domain)));
options.AddPolicy("stream:songs", policy =>
policy.Requirements
.Add(new HasScopeRequirement("stream:songs", domain)));
}); });
@@ -104,34 +108,34 @@ namespace Icarus
var connString = Configuration.GetConnectionString("DefaultConnection"); var connString = Configuration.GetConnectionString("DefaultConnection");
services.Add(new ServiceDescriptor(typeof(SongRepository), services.Add(new ServiceDescriptor(typeof(SongRepository),
new SongRepository(Configuration.GetConnectionString("DefaultConnection")))); new SongRepository(Configuration.GetConnectionString("DefaultConnection"))));
services.Add(new ServiceDescriptor(typeof(AlbumRepository), services.Add(new ServiceDescriptor(typeof(AlbumRepository),
new AlbumRepository(Configuration.GetConnectionString("DefaultConnection")))); new AlbumRepository(Configuration.GetConnectionString("DefaultConnection"))));
services.Add(new ServiceDescriptor(typeof(ArtistRepository), services.Add(new ServiceDescriptor(typeof(ArtistRepository),
new ArtistRepository(Configuration.GetConnectionString("DefaultConnection")))); new ArtistRepository(Configuration.GetConnectionString("DefaultConnection"))));
services.Add(new ServiceDescriptor(typeof(GenreRepository), services.Add(new ServiceDescriptor(typeof(GenreRepository),
new GenreRepository(Configuration.GetConnectionString("DefaultConnection")))); new GenreRepository(Configuration.GetConnectionString("DefaultConnection"))));
services.Add(new ServiceDescriptor(typeof(YearRepository), services.Add(new ServiceDescriptor(typeof(YearRepository),
new YearRepository(Configuration.GetConnectionString("DefaultConnection")))); new YearRepository(Configuration.GetConnectionString("DefaultConnection"))));
services.Add(new ServiceDescriptor(typeof(CoverArtRepository), services.Add(new ServiceDescriptor(typeof(CoverArtRepository),
new CoverArtRepository(Configuration.GetConnectionString("DefaultConnection")))); new CoverArtRepository(Configuration.GetConnectionString("DefaultConnection"))));
services.Add(new ServiceDescriptor(typeof(UserRepository), services.Add(new ServiceDescriptor(typeof(UserRepository),
new UserRepository(Configuration.GetConnectionString("DefaultConnection")))); new UserRepository(Configuration.GetConnectionString("DefaultConnection"))));
services.AddDbContext<SongContext>(options => options.UseMySQL(connString)); services.AddDbContext<SongContext>(options => options.UseMySQL(connString));
services.AddDbContext<AlbumContext>(options => options.UseMySQL(connString)); services.AddDbContext<AlbumContext>(options => options.UseMySQL(connString));
services.AddDbContext<ArtistContext>(options => options.UseMySQL(connString)); services.AddDbContext<ArtistContext>(options => options.UseMySQL(connString));
services.AddDbContext<UserContext>(options => options.UseMySQL(connString)); services.AddDbContext<UserContext>(options => options.UseMySQL(connString));
services.AddDbContext<GenreContext>(options => options.UseMySQL(connString)); services.AddDbContext<GenreContext>(options => options.UseMySQL(connString));
services.AddDbContext<YearContext>(options => options.UseMySQL(connString)); services.AddDbContext<YearContext>(options => options.UseMySQL(connString));
services.AddDbContext<CoverArtContext>(options => options.UseMySQL(connString)); services.AddDbContext<CoverArtContext>(options => options.UseMySQL(connString));
} }
// Called by the runtime. Use this method to configure the HTTP request pipeline. // Called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -141,10 +145,10 @@ namespace Icarus
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
else else
// The default HSTS value is 30 days. // The default HSTS value is 30 days.
// You may want to change this for production scenarios // You may want to change this for production scenarios
app.UseHsts(); app.UseHsts();
app.UseAuthentication(); app.UseAuthentication();
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseMvc(); app.UseMvc();