diff --git a/Constants/FileExtensions.cs b/Constants/FileExtensions.cs index 2b20822..a47500a 100644 --- a/Constants/FileExtensions.cs +++ b/Constants/FileExtensions.cs @@ -11,6 +11,9 @@ public class FileExtensions // Contains file extension with period at the beginning public static string JPG_EXTENSION = ".jpg"; + // Contains file extension with period at the beginning + public static string FLAC_EXTENSION = ".flac"; + // Contains file extension with period at the beginning public static string ZIP_EXTENSION = ".zip"; } diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index dc53f45..f193e09 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -207,6 +207,7 @@ public class SongManager : BaseManager } } + // Change the name of this method to only focus on wav files public void SaveSongToFileSystem(IFormFile songFile, IFormFile coverArtData, Song song) { song.SongDirectory = _tempDirectoryRoot; diff --git a/Controllers/Utilities/MetadataRetriever.cs b/Controllers/Utilities/MetadataRetriever.cs index 1862226..e8c69f2 100644 --- a/Controllers/Utilities/MetadataRetriever.cs +++ b/Controllers/Utilities/MetadataRetriever.cs @@ -9,6 +9,8 @@ public class MetadataRetriever { #region Fields private static NLog.Logger _logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); + private List _supportedAudioFileTypes = new List {"wav", "flac"}; + private List _supportedImageFileTypes = new List {"jpeg", "jpg", "png"}; private Song _updatedSong; private string _message; private string _title; @@ -106,6 +108,22 @@ public class MetadataRetriever } } + public bool IsSupportedFile(IFormFile file) + { + var supportedTypes = this._supportedAudioFileTypes; + this._supportedImageFileTypes.ForEach(t => + { + if (!supportedTypes.Contains(t)) + { + supportedTypes.Add(t); + } + }); + + var extensionType = this.FileExtensionType(file).ToLower(); + + return supportedTypes.Contains(extensionType); + } + public Song RetrieveMetaData(string filePath) { Song song = new Song(); diff --git a/Controllers/v1/SongDataController.cs b/Controllers/v1/SongDataController.cs index fd97f3b..07b8fd5 100644 --- a/Controllers/v1/SongDataController.cs +++ b/Controllers/v1/SongDataController.cs @@ -103,6 +103,20 @@ public class SongDataController : BaseController { if (up.SongData.Length > 0 && up.CoverArtData.Length > 0 && !string.IsNullOrEmpty(up.SongFile)) { + var meta = new Utilities.MetadataRetriever(); + if (!meta.IsSupportedFile(up.SongData) && !meta.IsSupportedFile(up.CoverArtData)) + { + return BadRequest("Media is not supported"); + } + else if (!meta.IsSupportedFile(up.SongData)) + { + 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(up.SongFile); var tokMgr = new TokenManager(this._config); var accessToken = Request.Headers["Authorization"]; @@ -115,6 +129,9 @@ public class SongDataController : BaseController _logger.LogInformation($"Song title: {song.Title}"); + + // TODO: Identify the song file type. Then save the media. + // Create a new method to save song flac files _songMgr.SaveSongToFileSystem(up.SongData, up.CoverArtData, song); } }