From b3b5a975d9c52e1f79a6cabd89a10950607dbaf3 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 6 Aug 2024 19:51:50 -0400 Subject: [PATCH] #103: Added method to create song and moving some code around --- Icarus/Controllers/Managers/SongManager.cs | 21 ++++++++++++++ Icarus/Controllers/v1/SongDataController.cs | 2 +- Icarus/Icarus.csproj | 6 ++-- Icarus/Program.cs | 5 +++- Models/Models.csproj | 4 +++ Models/Song.cs | 32 +++++++++++++++++++++ 6 files changed, 66 insertions(+), 4 deletions(-) diff --git a/Icarus/Controllers/Managers/SongManager.cs b/Icarus/Controllers/Managers/SongManager.cs index 34b35e4..4d047e0 100644 --- a/Icarus/Controllers/Managers/SongManager.cs +++ b/Icarus/Controllers/Managers/SongManager.cs @@ -366,6 +366,27 @@ public class SongManager : BaseManager return song; } + public int Create(IFormFile file, string filePath, string prompt) + { + if (System.IO.File.Exists(filePath)) + { + return 1; + } + + using (var filestream = new FileStream(filePath, FileMode.Create)) + { + Console.WriteLine(prompt); + file.CopyTo(filestream); + + if (System.IO.File.Exists(filePath)) + { + return 2; + } + } + + return 0; + } + private bool SongRecordChanged(Song currentSong, Song songUpdates) { diff --git a/Icarus/Controllers/v1/SongDataController.cs b/Icarus/Controllers/v1/SongDataController.cs index 0b4e47c..150d84c 100644 --- a/Icarus/Controllers/v1/SongDataController.cs +++ b/Icarus/Controllers/v1/SongDataController.cs @@ -153,7 +153,7 @@ public class SongDataController : BaseController case "wav": // song = _songMgr.SaveSongToFileSystem(up.SongData, up.CoverArtData, song); // TODO: Make sure the tmp file gets deleted - return BadRequest(".wav files are not supported"); + return BadRequest("No support for .wav files"); case "flac": song = _songMgr.SaveFlacSongToFileSystem(up.SongData, up.CoverArtData, song); break; diff --git a/Icarus/Icarus.csproj b/Icarus/Icarus.csproj index 82181da..b9f76b2 100644 --- a/Icarus/Icarus.csproj +++ b/Icarus/Icarus.csproj @@ -15,8 +15,6 @@ - - runtime; build; native; contentfiles; analyzers @@ -37,6 +35,10 @@ + + + + diff --git a/Icarus/Program.cs b/Icarus/Program.cs index 4f015a5..31e6840 100644 --- a/Icarus/Program.cs +++ b/Icarus/Program.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Authentication.JwtBearer; +// using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; @@ -52,6 +52,9 @@ var Configuration = builder.Configuration; var connString = Configuration.GetConnectionString("DefaultConnection"); +// Microsoft.AspNetCore.Authentication +// builder.Services.AddAuthentication() + builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => { options.RequireHttpsMetadata = false; diff --git a/Models/Models.csproj b/Models/Models.csproj index e6928d4..96fc5db 100644 --- a/Models/Models.csproj +++ b/Models/Models.csproj @@ -12,4 +12,8 @@ + + + + diff --git a/Models/Song.cs b/Models/Song.cs index 8ee4377..1404c03 100644 --- a/Models/Song.cs +++ b/Models/Song.cs @@ -112,6 +112,27 @@ public class Song return includeExtension ? $"{filename}{extension}" : filename; } + public CreateSongResult Create(Microsoft.AspNetCore.Http.IFormFile file, string filePath, string prompt) + { + if (System.IO.File.Exists(filePath)) + { + return CreateSongResult.AlreadyExists; + } + + using (var filestream = new FileStream(filePath, FileMode.Create)) + { + Console.WriteLine(prompt); + file.CopyTo(filestream); + + if (System.IO.File.Exists(filePath)) + { + return CreateSongResult.Created; + } + } + + return CreateSongResult.NotCreated; + } + private string DetermineFileExtension(AudioFileExtensionsType flag) { switch (flag) @@ -135,11 +156,22 @@ public class Song return filename; } #endregion + } +#region Enums public enum AudioFileExtensionsType { Default = 0, WAV = 1, FLAC = 2 } + +public enum CreateSongResult +{ + NotCreated = 0, + AlreadyExists = 1, + Created = 2 +} +#endregion +