From 270b7d059cca7d4c85a23a983742f4bc34528a7b Mon Sep 17 00:00:00 2001 From: Kun Deng Date: Sun, 16 Jun 2024 18:28:08 -0400 Subject: [PATCH] #90: Adding functionality to download cover art (#97) * Added endpoint to download cover art * Code cleanup * More cleanup --- Controllers/Managers/AlbumManager.cs | 8 ------- Controllers/Managers/ArtistManager.cs | 8 ------- Controllers/Managers/CoverArtManager.cs | 8 ------- Controllers/Managers/DirectoryManager.cs | 6 ------ Controllers/Managers/GenreManager.cs | 8 ------- Controllers/Managers/SongManager.cs | 11 ---------- Controllers/Managers/TokenManager.cs | 5 ----- Controllers/v1/CoverArtController.cs | 21 +++++++++++++------ .../v1/SongCompressedDataControllers.cs | 10 --------- Controllers/v1/SongDataController.cs | 13 +----------- Models/BaseResult.cs | 2 -- Models/CoverArt.cs | 9 ++------ Models/LoginResult.cs | 2 -- Models/RegisterResult.cs | 2 -- Models/Song.cs | 3 --- Models/SongData.cs | 3 --- Models/SongResult.cs | 2 -- Models/User.cs | 2 -- 18 files changed, 18 insertions(+), 105 deletions(-) diff --git a/Controllers/Managers/AlbumManager.cs b/Controllers/Managers/AlbumManager.cs index 977a82d..66c5a68 100644 --- a/Controllers/Managers/AlbumManager.cs +++ b/Controllers/Managers/AlbumManager.cs @@ -1,11 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.Linq; - -using Microsoft.Extensions.Configuration; - -using Icarus.Controllers.Utilities; using Icarus.Models; using Icarus.Database.Contexts; diff --git a/Controllers/Managers/ArtistManager.cs b/Controllers/Managers/ArtistManager.cs index b2141d3..31b4f83 100644 --- a/Controllers/Managers/ArtistManager.cs +++ b/Controllers/Managers/ArtistManager.cs @@ -1,11 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.Linq; - -using Microsoft.Extensions.Configuration; - -using Icarus.Controllers.Utilities; using Icarus.Models; using Icarus.Database.Contexts; diff --git a/Controllers/Managers/CoverArtManager.cs b/Controllers/Managers/CoverArtManager.cs index 33e093d..4026d21 100644 --- a/Controllers/Managers/CoverArtManager.cs +++ b/Controllers/Managers/CoverArtManager.cs @@ -1,11 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.IO; - -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Configuration; - using Icarus.Constants; using Icarus.Controllers.Utilities; using Icarus.Database.Contexts; diff --git a/Controllers/Managers/DirectoryManager.cs b/Controllers/Managers/DirectoryManager.cs index e02e56d..644e1d2 100644 --- a/Controllers/Managers/DirectoryManager.cs +++ b/Controllers/Managers/DirectoryManager.cs @@ -1,9 +1,3 @@ -using System; -using System.IO; -using System.Linq; - -using Microsoft.Extensions.Configuration; - using Icarus.Models; using Icarus.Types; diff --git a/Controllers/Managers/GenreManager.cs b/Controllers/Managers/GenreManager.cs index c05576b..009f76b 100644 --- a/Controllers/Managers/GenreManager.cs +++ b/Controllers/Managers/GenreManager.cs @@ -1,11 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.Linq; - -using Microsoft.Extensions.Configuration; - -using Icarus.Controllers.Utilities; using Icarus.Models; using Icarus.Database.Contexts; diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index 3fb3f52..7e4a93c 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -1,14 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.Linq; -using System.Data; -using System.IO; -using System.Threading.Tasks; - -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Configuration; - using NLog; using Icarus.Controllers.Utilities; diff --git a/Controllers/Managers/TokenManager.cs b/Controllers/Managers/TokenManager.cs index ece55fc..de2a185 100644 --- a/Controllers/Managers/TokenManager.cs +++ b/Controllers/Managers/TokenManager.cs @@ -1,12 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; using System.Security.Claims; -using System.Threading.Tasks; using System.IdentityModel.Tokens.Jwt; using System.Text; -using Microsoft.Extensions.Configuration; using Microsoft.IdentityModel.Tokens; using Newtonsoft.Json; using RestSharp; diff --git a/Controllers/v1/CoverArtController.cs b/Controllers/v1/CoverArtController.cs index a973bb6..97fbbf1 100644 --- a/Controllers/v1/CoverArtController.cs +++ b/Controllers/v1/CoverArtController.cs @@ -1,11 +1,5 @@ -using System; -using System.Linq; -using System.Threading.Tasks; - using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; using Icarus.Controllers.Managers; using Icarus.Database.Contexts; @@ -78,5 +72,20 @@ public class CoverArtController : BaseController return NotFound(); } } + + [HttpGet("data/download/{id}")] + public async Task Download(int id) + { + var songContext = new SongContext(_connectionString); + var covMgr = new CoverArtManager(this._config); + + var songMetaData = songContext.RetrieveRecord(new Song { SongID = id}); + var filename = songMetaData.Title + Constants.FileExtensions.JPG_EXTENSION; + var c = covMgr.GetCoverArt(songMetaData); + + var data = await c.GetData(); + + return File(data, "application/x-msdownload", filename); + } #endregion } diff --git a/Controllers/v1/SongCompressedDataControllers.cs b/Controllers/v1/SongCompressedDataControllers.cs index 883ad6b..f4e7e02 100644 --- a/Controllers/v1/SongCompressedDataControllers.cs +++ b/Controllers/v1/SongCompressedDataControllers.cs @@ -1,16 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -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; -using Icarus.Controllers.Managers; using Icarus.Controllers.Utilities; using Icarus.Models; using Icarus.Database.Contexts; diff --git a/Controllers/v1/SongDataController.cs b/Controllers/v1/SongDataController.cs index 5ae5529..57d0653 100644 --- a/Controllers/v1/SongDataController.cs +++ b/Controllers/v1/SongDataController.cs @@ -1,16 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.IO; -using System.Linq; -using System.Threading.Tasks; - -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; -using Newtonsoft.Json; using Icarus.Controllers.Managers; using Icarus.Models; diff --git a/Models/BaseResult.cs b/Models/BaseResult.cs index c9c236c..e86e495 100644 --- a/Models/BaseResult.cs +++ b/Models/BaseResult.cs @@ -1,5 +1,3 @@ -using System; - using Newtonsoft.Json; namespace Icarus.Models; diff --git a/Models/CoverArt.cs b/Models/CoverArt.cs index 7b22816..370c252 100644 --- a/Models/CoverArt.cs +++ b/Models/CoverArt.cs @@ -1,10 +1,3 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; -using System.Reflection.Metadata; -using System.Text; - using Newtonsoft.Json; namespace Icarus.Models; @@ -33,5 +26,7 @@ public class CoverArt return (flag == 0) ? filename : $"{filename}{extension}"; } + + public async Task GetData() => await File.ReadAllBytesAsync(this.ImagePath); #endregion } diff --git a/Models/LoginResult.cs b/Models/LoginResult.cs index 4553bbd..d3183d6 100644 --- a/Models/LoginResult.cs +++ b/Models/LoginResult.cs @@ -1,5 +1,3 @@ -using System; - using Newtonsoft.Json; namespace Icarus.Models; diff --git a/Models/RegisterResult.cs b/Models/RegisterResult.cs index 6d0cdcb..96ec1ce 100644 --- a/Models/RegisterResult.cs +++ b/Models/RegisterResult.cs @@ -1,5 +1,3 @@ -using System; - using Newtonsoft.Json; namespace Icarus.Models; diff --git a/Models/Song.cs b/Models/Song.cs index 828005e..9119d4a 100644 --- a/Models/Song.cs +++ b/Models/Song.cs @@ -1,7 +1,4 @@ -using System; using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; -using System.Threading.Tasks; using Newtonsoft.Json; diff --git a/Models/SongData.cs b/Models/SongData.cs index d710ff0..ad03c59 100644 --- a/Models/SongData.cs +++ b/Models/SongData.cs @@ -1,6 +1,3 @@ -using System; -using System.Text; - namespace Icarus.Models; public class SongData diff --git a/Models/SongResult.cs b/Models/SongResult.cs index 2ae0f02..266090c 100644 --- a/Models/SongResult.cs +++ b/Models/SongResult.cs @@ -1,5 +1,3 @@ -using System; - using Newtonsoft.Json; namespace Icarus.Models; diff --git a/Models/User.cs b/Models/User.cs index d3b8759..f69727e 100644 --- a/Models/User.cs +++ b/Models/User.cs @@ -1,7 +1,5 @@ -using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; using Newtonsoft.Json;