#102: Moving Models and constants to their own library
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Icarus.Models;
|
||||
using Icarus.Database.Contexts;
|
||||
|
||||
namespace Icarus.Controllers.V1;
|
||||
|
||||
[Route("api/v1/album")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class AlbumController : BaseController
|
||||
{
|
||||
#region Fields
|
||||
private readonly ILogger<AlbumController>? _logger;
|
||||
private string? _connectionString;
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructors
|
||||
public AlbumController(ILogger<AlbumController> logger, IConfiguration config)
|
||||
{
|
||||
_logger = logger;
|
||||
_config = config;
|
||||
_connectionString = _config.GetConnectionString("DefaultConnection");
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region HTTP Routes
|
||||
[HttpGet]
|
||||
public IActionResult GetAlbums()
|
||||
{
|
||||
var albumContext = new AlbumContext(_connectionString!);
|
||||
|
||||
var albums = albumContext.Albums!.ToList();
|
||||
|
||||
if (albums.Count > 0)
|
||||
return Ok(albums);
|
||||
else
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public IActionResult GetAlbum(int id)
|
||||
{
|
||||
Album album = new Album{ Id = id };
|
||||
|
||||
var albumContext = new AlbumContext(_connectionString!);
|
||||
|
||||
if (albumContext.DoesRecordExist(album))
|
||||
{
|
||||
album = albumContext.RetrieveRecord(album);
|
||||
|
||||
return Ok(album);
|
||||
}
|
||||
else
|
||||
return NotFound();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Icarus.Models;
|
||||
using Icarus.Database.Contexts;
|
||||
|
||||
namespace Icarus.Controllers.V1;
|
||||
|
||||
[Route("api/v1/artist")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class ArtistController : BaseController
|
||||
{
|
||||
#region Fields
|
||||
private readonly ILogger<ArtistController>? _logger;
|
||||
private string? _connectionString;
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructors
|
||||
public ArtistController(ILogger<ArtistController> logger, IConfiguration config)
|
||||
{
|
||||
_logger = logger;
|
||||
_config = config;
|
||||
_connectionString = _config.GetConnectionString("DefaultConnection");
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region HTTP Routes
|
||||
[HttpGet]
|
||||
public IActionResult GetArtists()
|
||||
{
|
||||
var artistContext = new ArtistContext(_connectionString!);
|
||||
|
||||
var artists = artistContext.Artists.ToList();
|
||||
|
||||
if (artists.Count > 0)
|
||||
return Ok(artists);
|
||||
else
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public IActionResult GetArtist(int id)
|
||||
{
|
||||
Artist artist = new Artist { Id = id };
|
||||
|
||||
var artistContext = new ArtistContext(_connectionString!);
|
||||
|
||||
if (artistContext.DoesRecordExist(artist))
|
||||
{
|
||||
artist = artistContext.RetrieveRecord(artist);
|
||||
|
||||
return Ok(artist);
|
||||
}
|
||||
else
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
|
||||
namespace Icarus.Controllers.V1;
|
||||
|
||||
public class BaseController : ControllerBase
|
||||
{
|
||||
#region Fiends
|
||||
protected IConfiguration? _config;
|
||||
#endregion
|
||||
|
||||
|
||||
#region Methods
|
||||
[ApiExplorerSettings(IgnoreApi = true)]
|
||||
[Obsolete("Asymmetric key signing for tokens have been deprecated")]
|
||||
protected string ParseBearerTokenFromHeader()
|
||||
{
|
||||
var token = string.Empty;
|
||||
const string tokenType = "Bearer";
|
||||
const string otherTokenType = "Jwt";
|
||||
|
||||
var req = Request;
|
||||
var auth = req.Headers.Authorization;
|
||||
var val = auth.ToString();
|
||||
|
||||
if ((val.Contains(tokenType) || val.Contains(otherTokenType)) && val.Split(" ").Count() > 1)
|
||||
{
|
||||
var split = val.Split(" ");
|
||||
token = split[1];
|
||||
}
|
||||
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Icarus.Controllers.Managers;
|
||||
using Icarus.Database.Contexts;
|
||||
using Icarus.Models;
|
||||
|
||||
namespace Icarus.Controllers.V1;
|
||||
|
||||
[Route("api/v1/coverart")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class CoverArtController : BaseController
|
||||
{
|
||||
#region Fields
|
||||
private readonly ILogger<CoverArtController>? _logger;
|
||||
private string? _connectionString;
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructors
|
||||
public CoverArtController(ILogger<CoverArtController> logger, IConfiguration config)
|
||||
{
|
||||
_logger = logger;
|
||||
_config = config;
|
||||
_connectionString = _config.GetConnectionString("DefaultConnection");
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region HTTP Routes
|
||||
[HttpGet]
|
||||
public IActionResult GetCoverArts()
|
||||
{
|
||||
var coverArtContext = new CoverArtContext(_connectionString!);
|
||||
|
||||
var coverArtRecords = coverArtContext.CoverArtImages!.ToList();
|
||||
|
||||
if (coverArtRecords == null)
|
||||
{
|
||||
_logger!.LogInformation("No cover art records");
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger!.LogInformation("Found cover art records");
|
||||
return Ok(coverArtRecords);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public IActionResult GetCoverArt(int id)
|
||||
{
|
||||
var coverArt = new CoverArt { Id = id };
|
||||
|
||||
var coverArtContext = new CoverArtContext(_connectionString!);
|
||||
|
||||
coverArt = coverArtContext.RetrieveRecord(coverArt);
|
||||
|
||||
if (coverArt != null)
|
||||
{
|
||||
_logger!.LogInformation("Found cover art record");
|
||||
var coverArtBytes = System.IO.File.ReadAllBytes(
|
||||
coverArt.ImagePath());
|
||||
|
||||
return File(coverArtBytes, "application/x-msdownload",
|
||||
coverArt.SongTitle);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger!.LogInformation("Cover art not found");
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("data/download/{id}")]
|
||||
public async Task<IActionResult> Download(int id, [FromQuery] bool? randomizeFilename)
|
||||
{
|
||||
var songContext = new SongContext(_connectionString!);
|
||||
var covMgr = new CoverArtManager(this._config!);
|
||||
|
||||
var songMetaData = songContext.RetrieveRecord(new Song { Id = id});
|
||||
var c = covMgr.GetCoverArt(songMetaData);
|
||||
|
||||
var filename = DirectoryManager.GenerateDownloadFilename(10, Constants.FileExtensions.JPG_EXTENSION, songMetaData.Title!, randomizeFilename);
|
||||
|
||||
var data = await c.GetData();
|
||||
|
||||
return File(data, "application/x-msdownload", filename);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Icarus.Models;
|
||||
using Icarus.Database.Contexts;
|
||||
|
||||
namespace Icarus.Controllers.V1;
|
||||
|
||||
[Route("api/v1/genre")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class GenreController : BaseController
|
||||
{
|
||||
#region Fields
|
||||
private readonly ILogger<GenreController>? _logger;
|
||||
private string? _connectionString;
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructors
|
||||
public GenreController(ILogger<GenreController> logger, IConfiguration config)
|
||||
{
|
||||
_logger = logger;
|
||||
_config = config;
|
||||
_connectionString = _config.GetConnectionString("DefaultConnection");
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region HTTP Routes
|
||||
[HttpGet]
|
||||
public IActionResult GetGenres()
|
||||
{
|
||||
var genreStore = new GenreContext(_connectionString!);
|
||||
|
||||
var genres = genreStore!.Genres!.ToList();
|
||||
|
||||
if (genres.Count > 0)
|
||||
{
|
||||
return Ok(genres);
|
||||
}
|
||||
else
|
||||
{
|
||||
return NotFound(new List<Genre>());
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public IActionResult GetGenre(int id)
|
||||
{
|
||||
var genre = new Genre { Id = id };
|
||||
|
||||
var genreStore = new GenreContext(_connectionString!);
|
||||
|
||||
if (genreStore.DoesRecordExist(genre))
|
||||
{
|
||||
genre = genreStore.RetrieveRecord(genre);
|
||||
|
||||
return Ok(genre);
|
||||
}
|
||||
else
|
||||
{
|
||||
return NotFound(new Genre());
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using Icarus.Controllers.Managers;
|
||||
using Icarus.Controllers.Utilities;
|
||||
using Icarus.Models;
|
||||
using Icarus.Database.Contexts;
|
||||
|
||||
namespace Icarus.Controllers.V1;
|
||||
|
||||
[Route("api/v1/login")]
|
||||
[ApiController]
|
||||
public class LoginController : ControllerBase
|
||||
{
|
||||
#region Fields
|
||||
private string? _connectionString;
|
||||
private IConfiguration? _config;
|
||||
private ILogger<LoginController> _logger;
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Contructors
|
||||
public LoginController(IConfiguration config, ILogger<LoginController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_config = config;
|
||||
_connectionString = _config.GetConnectionString("DefaultConnection");
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region HTTP endpoints
|
||||
[HttpPost]
|
||||
public IActionResult Login([FromBody] User user)
|
||||
{
|
||||
var context = new UserContext(_connectionString!);
|
||||
|
||||
_logger.LogInformation("Starting process of validating credentials");
|
||||
|
||||
var message = "Invalid credentials";
|
||||
var password = user.Password;
|
||||
|
||||
var loginRes = new LoginResult
|
||||
{
|
||||
Username = user.Username
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
if (context.Users.FirstOrDefault(usr => usr.Username!.Equals(user.Username)) != null)
|
||||
{
|
||||
user = context.Users.FirstOrDefault(usr => usr.Username!.Equals(user.Username))!;
|
||||
|
||||
var validatePass = new PasswordEncryption();
|
||||
var validated = validatePass.VerifyPassword(user!, password!);
|
||||
if (!validated)
|
||||
{
|
||||
loginRes.Message = message;
|
||||
_logger.LogInformation(message);
|
||||
|
||||
return Ok(loginRes);
|
||||
}
|
||||
|
||||
_logger.LogInformation("Successfully validated user credentials");
|
||||
|
||||
TokenManager tk = new TokenManager(_config!);
|
||||
|
||||
loginRes = tk.LoginSymmetric(user!);
|
||||
|
||||
return Ok(loginRes);
|
||||
}
|
||||
else
|
||||
{
|
||||
loginRes.Message = message;
|
||||
|
||||
return NotFound(loginRes);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("An error occurred: {0}", ex.Message);
|
||||
_logger.LogError("Inner Exception: {0}", ex.InnerException!.Message);
|
||||
}
|
||||
|
||||
return NotFound(loginRes);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Icarus.Controllers.Utilities;
|
||||
using Icarus.Models;
|
||||
using Icarus.Database.Contexts;
|
||||
|
||||
namespace Icarus.Controllers.V1;
|
||||
|
||||
[Route("api/v1/register")]
|
||||
[ApiController]
|
||||
public class RegisterController : ControllerBase
|
||||
{
|
||||
#region Fields
|
||||
private IConfiguration? _config;
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructor
|
||||
public RegisterController(IConfiguration config)
|
||||
{
|
||||
_config = config;
|
||||
}
|
||||
#endregion
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult RegisterUser([FromBody] User user)
|
||||
{
|
||||
PasswordEncryption pe = new PasswordEncryption();
|
||||
user.Password = pe.HashPassword(user);
|
||||
user.EmailVerified = false;
|
||||
user.Status = "Registered";
|
||||
user.DateCreated = DateTime.Now;
|
||||
|
||||
UserContext? context = null;
|
||||
|
||||
try
|
||||
{
|
||||
var connString = _config!.GetConnectionString("DefaultConnection");
|
||||
context = new UserContext(connString!);
|
||||
context.Add(user);
|
||||
context.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var msg = ex.Message;
|
||||
var stackTrace = ex.StackTrace;
|
||||
|
||||
Console.WriteLine($"An error occurred: {msg}");
|
||||
}
|
||||
|
||||
var registerResult = new RegisterResult
|
||||
{
|
||||
Username = user.Username
|
||||
};
|
||||
|
||||
if (context!.Users.FirstOrDefault(sng => sng.Username!.Equals(user.Username)) != null)
|
||||
{
|
||||
registerResult.Message = "Successful registration";
|
||||
registerResult.SuccessfullyRegistered = true;
|
||||
|
||||
return Ok(registerResult);
|
||||
}
|
||||
else
|
||||
{
|
||||
registerResult.Message = "Registration failed";
|
||||
registerResult.SuccessfullyRegistered = false;
|
||||
|
||||
return Ok(registerResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Icarus.Controllers.Utilities;
|
||||
using Icarus.Models;
|
||||
using Icarus.Database.Contexts;
|
||||
using Icarus.Controllers.Managers;
|
||||
|
||||
namespace Icarus.Controllers.V1;
|
||||
|
||||
[Route("api/v1/song/compressed/data")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class SongCompressedDataController : BaseController
|
||||
{
|
||||
#region Fields
|
||||
private string? _connectionString;
|
||||
private string? _songTempDir;
|
||||
private string? _archiveDir;
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructor
|
||||
public SongCompressedDataController(IConfiguration config)
|
||||
{
|
||||
_config = config;
|
||||
_songTempDir = _config.GetValue<string>("TemporaryMusicPath");
|
||||
_archiveDir = _config.GetValue<string>("ArchivePath");
|
||||
_connectionString = _config.GetConnectionString("DefaultConnection");
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region API Routes
|
||||
[HttpGet("{id}")]
|
||||
public async Task<IActionResult> DownloadCompressedSong(int id, [FromQuery] bool? randomizeFilename)
|
||||
{
|
||||
var context = new SongContext(_connectionString!);
|
||||
|
||||
SongCompression cmp = new SongCompression(_archiveDir!);
|
||||
|
||||
Console.WriteLine($"Archive directory root: {_archiveDir}");
|
||||
|
||||
Console.WriteLine("Starting process of retrieving comrpessed song");
|
||||
var sng = context.RetrieveRecord(new Song{ Id = id });
|
||||
SongData song = await cmp.RetrieveCompressedSong(sng);
|
||||
|
||||
var filename = DirectoryManager.GenerateDownloadFilename(10, Constants.FileExtensions.ZIP_EXTENSION, sng.Title!, randomizeFilename);
|
||||
|
||||
return File(song.Data!, "application/x-msdownload", filename);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Icarus.Controllers.Managers;
|
||||
using Icarus.Models;
|
||||
using Icarus.Database.Contexts;
|
||||
|
||||
namespace Icarus.Controllers.V1;
|
||||
|
||||
[Route("api/v1/song")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class SongController : BaseController
|
||||
{
|
||||
#region Fields
|
||||
private readonly ILogger<SongController>? _logger;
|
||||
private string? _connectionString;
|
||||
private SongManager? _songMgr;
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructor
|
||||
public SongController(IConfiguration config, ILogger<SongController> logger)
|
||||
{
|
||||
_config = config;
|
||||
_logger = logger;
|
||||
_connectionString = _config.GetConnectionString("DefaultConnection");
|
||||
_songMgr = new SongManager(config);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region Methods
|
||||
#region HTTP Endpoints
|
||||
[HttpGet]
|
||||
public IActionResult GetSongs()
|
||||
{
|
||||
Console.WriteLine("Attemtping to retrieve songs");
|
||||
_logger!.LogInformation("Attempting to retrieve songs");
|
||||
|
||||
var context = new SongContext(_connectionString!);
|
||||
|
||||
var songs = context.Songs!.ToList();
|
||||
|
||||
if (songs.Count > 0)
|
||||
{
|
||||
return Ok(songs);
|
||||
}
|
||||
else
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public IActionResult GetSong(int id)
|
||||
{
|
||||
var context = new SongContext(_connectionString!);
|
||||
|
||||
var song = context.RetrieveRecord(new Song{ Id = id });
|
||||
|
||||
Console.WriteLine("Here");
|
||||
|
||||
if (song.Id != 0)
|
||||
return Ok(song);
|
||||
else
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
public IActionResult UpdateSong(int id, [FromBody] Song song)
|
||||
{
|
||||
song.Id = id;
|
||||
Console.WriteLine("Retrieving filepath of song");
|
||||
_logger!.LogInformation("Retrieving filepath of song");
|
||||
|
||||
if (!_songMgr!.DoesSongExist(song))
|
||||
{
|
||||
return NotFound(new SongResult
|
||||
{
|
||||
Message = "Song does not exist"
|
||||
});
|
||||
}
|
||||
|
||||
var songRes = _songMgr.UpdateSong(song);
|
||||
|
||||
return Ok(songRes);
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Icarus.Controllers.Managers;
|
||||
using Icarus.Models;
|
||||
using Icarus.Database.Contexts;
|
||||
|
||||
namespace Icarus.Controllers.V1;
|
||||
|
||||
[Route("api/v1/song/data")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class SongDataController : BaseController
|
||||
{
|
||||
#region Fields
|
||||
private string? _connectionString;
|
||||
private ILogger<SongDataController>? _logger;
|
||||
private SongManager? _songMgr;
|
||||
private string? _songTempDir;
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructor
|
||||
public SongDataController(IConfiguration config, ILogger<SongDataController> logger)
|
||||
{
|
||||
_config = config;
|
||||
_connectionString = _config.GetConnectionString("DefaultConnection");
|
||||
_logger = logger;
|
||||
_songTempDir = _config.GetValue<string>("TemporaryMusicPath");
|
||||
_songMgr = new SongManager(config, _songTempDir!);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
[HttpGet("download/{id}")]
|
||||
public IActionResult Download(int id, [FromQuery] bool? randomizeFilename)
|
||||
{
|
||||
var songContext = new SongContext(_connectionString!);
|
||||
var songMetaData = songContext.RetrieveRecord(new Song { Id = id});
|
||||
|
||||
var song = _songMgr!.RetrieveSong(songMetaData).Result;
|
||||
string filename;
|
||||
|
||||
switch (songMetaData.AudioType)
|
||||
{
|
||||
case "wav":
|
||||
filename = DirectoryManager.GenerateDownloadFilename(10, Constants.FileExtensions.WAV_EXTENSION,
|
||||
songMetaData.Title!, randomizeFilename);
|
||||
break;
|
||||
case "flac":
|
||||
filename = DirectoryManager.GenerateDownloadFilename(10, Constants.FileExtensions.FLAC_EXTENSION,
|
||||
songMetaData.Title!, randomizeFilename);
|
||||
break;
|
||||
default:
|
||||
filename = DirectoryManager.GenerateDownloadFilename(10, Constants.FileExtensions.DEFAULT_AUDIO_EXTENSION,
|
||||
songMetaData.Title!, randomizeFilename);
|
||||
break;
|
||||
}
|
||||
|
||||
return File(song.Data!, "application/x-msdownload", filename);
|
||||
}
|
||||
|
||||
// Assumes that the song already has metadata such as
|
||||
// Title
|
||||
// Artist
|
||||
// Album
|
||||
// Genre
|
||||
// Year
|
||||
// Track
|
||||
// Track count
|
||||
// Disc
|
||||
// Disc count
|
||||
// Cover art
|
||||
//
|
||||
[HttpPost("upload"), DisableRequestSizeLimit]
|
||||
public IActionResult Upload([FromForm(Name = "file")] List<IFormFile> songData)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger!.LogInformation("Uploading song...");
|
||||
|
||||
var uploads = _songTempDir;
|
||||
_logger!.LogInformation($"Song root path {uploads}");
|
||||
|
||||
foreach (var sng in songData)
|
||||
if (sng.Length > 0)
|
||||
{
|
||||
_logger!.LogInformation($"Song filename {sng.FileName}");
|
||||
|
||||
_songMgr!.SaveSongToFileSystem(sng).Wait();
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var msg = ex.Message;
|
||||
_logger!.LogError(msg, "An error occurred");
|
||||
}
|
||||
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
// The client is expected to send the file, metadata, and cover art separately.
|
||||
// Any metadata already on the file will be overwritten with values from the metadata
|
||||
// as well as the cover art
|
||||
//
|
||||
[HttpPost("upload/with/data")]
|
||||
public IActionResult UploadWithData([FromForm] UploadSongWithDataForm up)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (up.SongData!.Length > 0 && up.CoverArtData!.Length > 0 && !string.IsNullOrEmpty(up.SongFile))
|
||||
{
|
||||
var meta = new Utilities.MetadataRetriever();
|
||||
var tmpSong = this._songMgr!.SaveSongTemp(up.SongData).Result;
|
||||
if (!meta.IsSupportedFile(tmpSong.SongPath()) && !meta.IsSupportedFile(up.CoverArtData))
|
||||
{
|
||||
return BadRequest("Media is not supported");
|
||||
}
|
||||
else if (!meta.IsSupportedFile(tmpSong.SongPath()))
|
||||
{
|
||||
return BadRequest("Song is not supported");
|
||||
}
|
||||
else if (!meta.IsSupportedFile(up.CoverArtData))
|
||||
{
|
||||
return BadRequest("Cover art is not supported");
|
||||
}
|
||||
|
||||
var song = Newtonsoft.Json.JsonConvert.DeserializeObject<Song>(up.SongFile);
|
||||
var tokMgr = new TokenManager(this._config!);
|
||||
var accessToken = Request.Headers["Authorization"];
|
||||
var userId = tokMgr.RetrieveUserIdFromToken(accessToken!);
|
||||
|
||||
if (userId != -1)
|
||||
{
|
||||
song!.UserId = userId;
|
||||
}
|
||||
|
||||
_logger!.LogInformation($"Song title: {song!.Title}");
|
||||
|
||||
song.Filename = tmpSong.Filename;
|
||||
song.SongDirectory = tmpSong.SongDirectory;
|
||||
song.DateCreated = tmpSong.DateCreated;
|
||||
song.AudioType = meta.FileExtensionType(tmpSong.SongPath());
|
||||
|
||||
switch (song.AudioType)
|
||||
{
|
||||
case "wav":
|
||||
song = _songMgr.SaveSongToFileSystem(up.SongData, up.CoverArtData, song);
|
||||
break;
|
||||
case "flac":
|
||||
song = _songMgr.SaveFlacSongToFileSystem(up.SongData, up.CoverArtData, song);
|
||||
break;
|
||||
default:
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
return Ok(song);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger!.LogError(ex.Message, "An error occurred");
|
||||
}
|
||||
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
[HttpDelete("delete/{id}")]
|
||||
public IActionResult DeleteSong(int id)
|
||||
{
|
||||
var songContext = new SongContext(_connectionString!);
|
||||
|
||||
var songMetaData = new Song{ Id = id };
|
||||
Console.WriteLine($"Id {songMetaData.Id}");
|
||||
|
||||
songMetaData = songContext.RetrieveRecord(songMetaData);
|
||||
|
||||
if (string.IsNullOrEmpty(songMetaData.Title))
|
||||
{
|
||||
_logger!.LogInformation("Song does not exist");
|
||||
return NotFound("Song does not exist");
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger!.LogInformation("Starting process of deleting song from the filesystem and database");
|
||||
|
||||
_songMgr!.DeleteSong(songMetaData);
|
||||
|
||||
return Ok(songMetaData);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class UploadSongWithDataForm
|
||||
{
|
||||
[FromForm(Name = "file")]
|
||||
public IFormFile? SongData { get; set; }
|
||||
// NOTE: Think about making this optional and if it is not provided, use the stock cover art
|
||||
[FromForm(Name = "cover")]
|
||||
public IFormFile? CoverArtData { get; set; }
|
||||
[FromForm(Name = "metadata")]
|
||||
public string? SongFile { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Icarus.Database.Contexts;
|
||||
|
||||
namespace Icarus.Controllers.V1;
|
||||
|
||||
[Route("api/v1/song/stream")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class SongStreamController : BaseController
|
||||
{
|
||||
#region Fields
|
||||
private ILogger<SongStreamController>? _logger;
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructor
|
||||
public SongStreamController(ILogger<SongStreamController> logger, IConfiguration config)
|
||||
{
|
||||
_logger = logger;
|
||||
_config = config;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region HTTP endpoints
|
||||
[HttpGet("{id}")]
|
||||
public async Task<IActionResult> StreamSong(int id)
|
||||
{
|
||||
var context = new SongContext(_config!.GetConnectionString("DefaultConnection")!);
|
||||
|
||||
var song = context.Songs!.FirstOrDefault(sng => sng.Id == id);
|
||||
|
||||
var stream = new FileStream(song!.SongPath(), FileMode.Open, FileAccess.Read);
|
||||
stream.Position = 0;
|
||||
var filename = song.Filename;
|
||||
|
||||
if (string.IsNullOrEmpty(song.Filename))
|
||||
{
|
||||
filename = song.GenerateFilename();
|
||||
}
|
||||
|
||||
_logger!.LogInformation("Starting to stream song...>");
|
||||
Console.WriteLine("Starting to streamsong...");
|
||||
|
||||
return await Task.Run(() => {
|
||||
return File(stream, "application/octet-stream", filename);
|
||||
});
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user