#103: Remove WAV support (#105)

* #103: Adding checks to prevent .wav files from being uploaded:

* #103: Added method to create song and moving some code around

* #103: Fixed build issue

* tsk-103: Cleanup of temporary files

* tsk-103: Formatting changes

* tsk-103: Minor changes

* tsk-103: Fixing build issue

* tsk-103: Refactored enum

* CORE-23734: Refactoring

* tsk-103: Confirmed functionality is working

* Removed commented code

* Removed commented code
This commit was merged in pull request #105.
This commit is contained in:
KD
2025-02-16 17:24:50 -05:00
committed by GitHub
parent a89580ac70
commit fd3ce9f96a
33 changed files with 242 additions and 99 deletions
+1 -1
View File
@@ -85,7 +85,7 @@ public class AlbumManager : BaseManager
if (string.IsNullOrEmpty(newAlbumTitle))
newAlbumTitle = oldAlbumTitle;
if ((string.IsNullOrEmpty(newAlbumTitle) && string.IsNullOrEmpty(newAlbumArtist) ||
if ((string.IsNullOrEmpty(newAlbumTitle) && string.IsNullOrEmpty(newAlbumArtist) ||
oldAlbumTitle!.Equals(newAlbumTitle) && oldAlbumArtist!.Equals(newAlbumArtist)))
{
_logger.Info("No change to the song's album");
+1 -1
View File
@@ -84,7 +84,7 @@ public class ArtistManager : BaseManager
_artistContext.Add(newArtistRecord);
_artistContext.SaveChanges();
return newArtistRecord;
}
else
@@ -48,7 +48,7 @@ public class CoverArtManager : BaseManager
try
{
var stockCoverArtPath = _rootCoverArtPath + _filename;
if (!string.Equals(stockCoverArtPath, coverArt.ImagePath(),
if (!string.Equals(stockCoverArtPath, coverArt.ImagePath(),
StringComparison.CurrentCultureIgnoreCase))
{
_logger.Info("Song does not contain the stock cover art");
@@ -87,7 +87,7 @@ public class CoverArtManager : BaseManager
var metaData = new MetadataRetriever();
var imgBytes = metaData.RetrieveCoverArtBytes(song);
if (imgBytes != null)
{
_logger.Info("Saving cover art to the filesystem");
@@ -121,7 +121,7 @@ public class CoverArtManager : BaseManager
var msg = ex.Message;
_logger.Error(msg, "An error occurred");
}
return null;
}
@@ -173,7 +173,7 @@ public class CoverArtManager : BaseManager
if (!File.Exists(_rootCoverArtPath + _filename))
{
File.WriteAllBytes(_rootCoverArtPath + _filename,
File.WriteAllBytes(_rootCoverArtPath + _filename,
_stockCoverArt!);
Console.WriteLine("Copied Stock Cover Art");
}
@@ -53,7 +53,7 @@ public class DirectoryManager : BaseManager
public static string GenerateDownloadFilename(int length, string extension, string title, bool? randomize)
{
if (randomize.HasValue && randomize.Value)
if (randomize.HasValue && randomize.Value)
{
return GenerateFilename(length) + extension;
}
@@ -100,13 +100,13 @@ public class DirectoryManager : BaseManager
var artistDirectory = ArtistDirectory();
if (IsDirectoryEmpty(albumDirectory))
{
Directory.Delete(albumDirectory);
Console.WriteLine($"directory {albumDirectory} deleted");
Directory.Delete(albumDirectory);
Console.WriteLine($"directory {albumDirectory} deleted");
}
if (IsDirectoryEmpty(artistDirectory))
{
Directory.Delete(artistDirectory);
Console.WriteLine($"directory {artistDirectory} deleted");
Directory.Delete(artistDirectory);
Console.WriteLine($"directory {artistDirectory} deleted");
}
}
catch (Exception ex)
@@ -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
@@ -125,12 +157,12 @@ public class DirectoryManager : BaseManager
if (IsDirectoryEmpty(albumDirectory))
{
Directory.Delete(albumDirectory);
Directory.Delete(albumDirectory);
_logger.Info("Album directory deleted");
}
if (IsDirectoryEmpty(artistDirectory))
{
Directory.Delete(artistDirectory);
Directory.Delete(artistDirectory);
_logger.Info("Artist directory deleted");
}
}
@@ -187,7 +219,7 @@ public class DirectoryManager : BaseManager
private class DirEnt
{
public string? Pre { get; set;}
public string? Pre { get; set; }
public string? Path { get; set; }
public string? Post { get; set; }
}
+49 -17
View File
@@ -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,11 +109,18 @@ public class SongManager : BaseManager
try
{
var songPath = songMetaData.SongPath();
File.Delete(songPath);
successful = true;
System.IO.File.Delete(songPath);
successful = !System.IO.File.Exists(songPath);
if (successful)
{
Console.WriteLine("Song successfully deleted");
}
DirectoryManager dirMgr = new DirectoryManager(_config!, songMetaData);
dirMgr.DeleteEmptyDirectories();
Console.WriteLine("Song successfully deleted");
var deletedAmount = dirMgr.DeleteEmptyDirectories(songMetaData.SongDirectory, 1);
if (deletedAmount > 0)
{
Console.WriteLine($"{deletedAmount} directories deleted");
}
}
catch (Exception ex)
{
@@ -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))
@@ -252,8 +262,8 @@ public class SongManager : BaseManager
return song;
}
public Song SaveFlacSongToFileSystem(IFormFile songFile, IFormFile coverArtData, Song song)
{
public Song SaveFlacSongToFileSystem(IFormFile songFile, IFormFile coverArtData, Song song)
{
// Save temp song (Should already be saved to the filesystem by the time it gets to this method)
// Save cover art
// Update the song's metadata with the song object
@@ -283,10 +293,10 @@ public class SongManager : BaseManager
SaveSongToDatabase(song, coverArt);
return song;
}
}
private void MoveSongToFinalDestination(string sourcePath, string targetPath)
{
private void MoveSongToFinalDestination(string sourcePath, string targetPath)
{
using (var fileStream = new FileStream(targetPath, FileMode.Create))
{
var songBytes = System.IO.File.ReadAllBytes(sourcePath);
@@ -315,8 +325,8 @@ public class SongManager : BaseManager
_logger.Info("Song successfully saved to filesystem");
}
}
}
public async Task<SongData> RetrieveSong(Song songMetaData)
{
var song = new SongData();
@@ -340,7 +350,7 @@ public class SongManager : BaseManager
private async Task<SongData> RetrieveSongFromFileSystem(Song details)
{
byte[] uncompressedSong = await System.IO.File.ReadAllBytesAsync(details.SongPath());
return new SongData
{
Data = uncompressedSong
@@ -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;
@@ -402,9 +434,9 @@ public class SongManager : BaseManager
Console.WriteLine($"Error Occurred: {ex.Message}");
}
}
private void SaveSongToDatabase(Song song, CoverArt? cover)
{
_logger.Info("Starting process to save the song to the database");
@@ -425,7 +457,7 @@ public class SongManager : BaseManager
coverMgr.SaveCoverArtToDatabase(ref song, ref cover!);
}
private bool DeleteSongFromFilesystem(Song song, bool deleteDirectory = false)
{
@@ -439,7 +471,7 @@ public class SongManager : BaseManager
DeleteEmptyDirectories(ref song, ref song);
}
catch(Exception ex)
catch (Exception ex)
{
var msg = ex.Message;
_logger.Error(msg, "An error occurred when attempting to delete the song from the filesystem");
@@ -463,7 +495,7 @@ public class SongManager : BaseManager
return true;
}
private void UpdateSongInDatabase(ref Song oldSongRecord, ref Song newSongRecord, ref SongResult result)
{
var updatedSongRecord = oldSongRecord;
+19 -14
View File
@@ -53,7 +53,7 @@ public class TokenManager : BaseManager
_logger.Info("Serializing token object into JSON");
var tokenObject = JsonConvert.SerializeObject(tokenRequest);
request.AddParameter("application/json; charset=utf-8",
request.AddParameter("application/json; charset=utf-8",
tokenObject, ParameterType.RequestBody);
request.RequestFormat = DataFormat.Json;
@@ -74,7 +74,7 @@ public class TokenManager : BaseManager
public LoginResult LoginSymmetric(User user)
{
var tokenResult = new TokenTierOne{ TokenType = "JWT" };
var tokenResult = new TokenTierOne { TokenType = "JWT" };
var payload = Payload();
payload.Add(new Claim("user_id", user.Id.ToString(), ClaimValueTypes.Integer));
@@ -91,7 +91,7 @@ public class TokenManager : BaseManager
Expires = DateTime.UtcNow.AddHours(1),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature),
Issuer = _config["Jwt:Issuer"], // Add this line
Audience = _config["Jwt:Audience"]
Audience = _config["Jwt:Audience"]
};
tokenResult.AccessToken = tokenHandler.WriteToken(tokenHandler.CreateToken(tokenDescriptor));
@@ -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
};
}
@@ -162,13 +165,13 @@ public class TokenManager : BaseManager
"download:songs",
"read:song_details",
"upload:songs",
"delete:songs",
"read:albums",
"delete:songs",
"read:albums",
"read:artists",
"update:songs",
"stream:songs",
"read:genre",
"read:year",
"update:songs",
"stream:songs",
"read:genre",
"read:year",
"download:cover_art"
};
@@ -217,15 +220,17 @@ public class TokenManager : BaseManager
{
return await System.IO.File.ReadAllTextAsync(filepath);
}
private TokenRequest RetrieveTokenRequest()
{
_logger.Info("Retrieving token object");
return new TokenRequest
{
ClientId = _clientId, ClientSecret = _clientSecret,
Audience = _audience, GrantType = _grantType
ClientId = _clientId,
ClientSecret = _clientSecret,
Audience = _audience,
GrantType = _grantType
};
}