#103: Remove WAV support #105
@@ -18,3 +18,7 @@ Icarus/bin
|
||||
Icarus/obj
|
||||
Models/bin
|
||||
Models/obj
|
||||
Migrations
|
||||
Icarus.txt
|
||||
Storage
|
||||
.DS_Store
|
||||
|
||||
@@ -116,6 +116,38 @@ public class DirectoryManager : BaseManager
|
||||
}
|
||||
}
|
||||
|
||||
public int DeleteEmptyDirectories(string? directory, int level)
|
||||
{
|
||||
var deleted = 0;
|
||||
|
||||
try
|
||||
{
|
||||
var curDir = directory;
|
||||
for (var i = 0; i < level; i++)
|
||||
{
|
||||
if (!System.IO.Directory.Exists(curDir))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (this.IsDirectoryEmpty(curDir))
|
||||
{
|
||||
System.IO.Directory.Delete(curDir);
|
||||
deleted++;
|
||||
}
|
||||
|
||||
curDir = System.IO.Directory.GetParent(curDir).ToString();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var exMsg = ex.Message;
|
||||
Console.WriteLine($"An error occurred {exMsg}");
|
||||
}
|
||||
|
||||
return deleted;
|
||||
}
|
||||
|
||||
public void DeleteEmptyDirectories(Song song)
|
||||
{
|
||||
try
|
||||
|
||||
@@ -3,9 +3,11 @@ using NLog;
|
||||
using Icarus.Controllers.Utilities;
|
||||
using Icarus.Models;
|
||||
using Icarus.Database.Contexts;
|
||||
using TagLib.Mpeg4;
|
||||
|
||||
namespace Icarus.Controllers.Managers;
|
||||
|
||||
|
||||
public class SongManager : BaseManager
|
||||
{
|
||||
#region Fields
|
||||
@@ -107,12 +109,19 @@ public class SongManager : BaseManager
|
||||
try
|
||||
{
|
||||
var songPath = songMetaData.SongPath();
|
||||
File.Delete(songPath);
|
||||
successful = true;
|
||||
DirectoryManager dirMgr = new DirectoryManager(_config!, songMetaData);
|
||||
dirMgr.DeleteEmptyDirectories();
|
||||
System.IO.File.Delete(songPath);
|
||||
successful = !System.IO.File.Exists(songPath);
|
||||
if (successful)
|
||||
{
|
||||
Console.WriteLine("Song successfully deleted");
|
||||
}
|
||||
DirectoryManager dirMgr = new DirectoryManager(_config!, songMetaData);
|
||||
var deletedAmount = dirMgr.DeleteEmptyDirectories(songMetaData.SongDirectory, 1);
|
||||
if (deletedAmount > 0)
|
||||
{
|
||||
Console.WriteLine($"{deletedAmount} directories deleted");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var exMsg = ex.Message;
|
||||
@@ -196,6 +205,7 @@ public class SongManager : BaseManager
|
||||
}
|
||||
|
||||
// Change the name of this method to only focus on wav files
|
||||
[Obsolete("Support for uplodaing wav files will end. Use the flac alternative instead - SaveFlacSongToFileSystem(..)")]
|
||||
public Song SaveSongToFileSystem(IFormFile songFile, IFormFile coverArtData, Song song)
|
||||
{
|
||||
if (string.IsNullOrEmpty(song.SongDirectory))
|
||||
@@ -366,6 +376,28 @@ public class SongManager : BaseManager
|
||||
}
|
||||
|
||||
|
||||
public Icarus.Models.CreateFileResult Create(IFormFile file, string filePath, string prompt)
|
||||
{
|
||||
if (System.IO.File.Exists(filePath))
|
||||
{
|
||||
return CreateFileResult.AlreadyExists;
|
||||
}
|
||||
|
||||
using (var filestream = new FileStream(filePath, FileMode.Create))
|
||||
{
|
||||
Console.WriteLine(prompt);
|
||||
file.CopyTo(filestream);
|
||||
|
||||
if (System.IO.File.Exists(filePath))
|
||||
{
|
||||
return CreateFileResult.FileCreatedAndExists;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
private bool SongRecordChanged(Song currentSong, Song songUpdates)
|
||||
{
|
||||
var currentTitle = currentSong.Title;
|
||||
|
||||
@@ -112,8 +112,11 @@ public class TokenManager : BaseManager
|
||||
{
|
||||
return new LoginResult
|
||||
{
|
||||
UserId = user.Id, Username = user.Username, Token = token.AccessToken,
|
||||
TokenType = token.TokenType, Expiration = token.Expiration,
|
||||
UserId = user.Id,
|
||||
Username = user.Username,
|
||||
Token = token.AccessToken,
|
||||
TokenType = token.TokenType,
|
||||
Expiration = token.Expiration,
|
||||
Message = SUCCESSFUL_TOKEN_MESSAGE
|
||||
};
|
||||
}
|
||||
@@ -224,8 +227,10 @@ public class TokenManager : BaseManager
|
||||
|
||||
return new TokenRequest
|
||||
{
|
||||
ClientId = _clientId, ClientSecret = _clientSecret,
|
||||
Audience = _audience, GrantType = _grantType
|
||||
ClientId = _clientId,
|
||||
ClientSecret = _clientSecret,
|
||||
Audience = _audience,
|
||||
GrantType = _grantType
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@ using Icarus.Database.Contexts;
|
||||
|
||||
namespace Icarus.Controllers.V1;
|
||||
|
||||
|
||||
|
||||
[Route("api/v1/song/data")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
@@ -151,8 +153,13 @@ public class SongDataController : BaseController
|
||||
switch (song.AudioType)
|
||||
{
|
||||
case "wav":
|
||||
song = _songMgr.SaveSongToFileSystem(up.SongData, up.CoverArtData, song);
|
||||
break;
|
||||
var _ = _songMgr.DeleteSongFromFileSystem(tmpSong);
|
||||
return BadRequest(new UploadSongWithDataResponse
|
||||
{
|
||||
Subject = "No longer supported",
|
||||
Message = "No support for .wav files",
|
||||
Songs = new List<Song>()
|
||||
});
|
||||
case "flac":
|
||||
song = _songMgr.SaveFlacSongToFileSystem(up.SongData, up.CoverArtData, song);
|
||||
break;
|
||||
@@ -207,4 +214,16 @@ public class SongDataController : BaseController
|
||||
[FromForm(Name = "metadata")]
|
||||
public string? SongFile { get; set; }
|
||||
}
|
||||
|
||||
public class UploadSongWithDataResponse
|
||||
{
|
||||
#region Properties
|
||||
[Newtonsoft.Json.JsonProperty("message")]
|
||||
public string Message { get; set; }
|
||||
[Newtonsoft.Json.JsonProperty("subject")]
|
||||
public string Subject { get; set; }
|
||||
[Newtonsoft.Json.JsonProperty("data")]
|
||||
public List<Song> Songs { get; set; }
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,8 @@ public class SongStreamController : BaseController
|
||||
_logger!.LogInformation("Starting to stream song...>");
|
||||
Console.WriteLine("Starting to streamsong...");
|
||||
|
||||
return await Task.Run(() => {
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
return File(stream, "application/octet-stream", filename);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
<ProjectReference Include="..\Models\Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<Content Update="nlog.config" CopyToOutputDirectory="PreserveNewest" />
|
||||
<Content Include="Images/Stock/*.*" CopyToOutputDirectory="PreserveNewest" />
|
||||
|
||||
+4
-2
@@ -56,14 +56,16 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJw
|
||||
{
|
||||
options.RequireHttpsMetadata = false;
|
||||
options.SaveToken = true;
|
||||
var audience = Configuration["JWT:Audience"];
|
||||
var issuer = Configuration["JWT:Issuer"];
|
||||
options.TokenValidationParameters = new TokenValidationParameters()
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidateLifetime = true,
|
||||
ValidAudience = Configuration["JWT:Audience"],
|
||||
ValidIssuer = Configuration["JWT:Issuer"],
|
||||
ValidAudience = audience,
|
||||
ValidIssuer = issuer,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(Configuration["JWT:Secret"]!))
|
||||
};
|
||||
});
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
|
||||
|
||||
namespace Icarus.Models;
|
||||
|
||||
public enum CreateFileResult
|
||||
{
|
||||
Unknwon = 0,
|
||||
AlreadyExists = 1,
|
||||
FileCreatedAndExists = 2
|
||||
}
|
||||
@@ -12,4 +12,8 @@
|
||||
<PackageReference Include="newtonsoft.json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user