From 0394ad38da805d4c12c15160cf0d46de161a30ef Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 16 Jun 2024 17:56:25 -0400 Subject: [PATCH] #79: Added functionality to the comressed song endpoint --- Constants/FileExtensions.cs | 3 +++ Controllers/Utilities/SongCompression.cs | 6 +++--- .../v1/SongCompressedDataControllers.cs | 21 +++++++++++++++---- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Constants/FileExtensions.cs b/Constants/FileExtensions.cs index 281526e..2b20822 100644 --- a/Constants/FileExtensions.cs +++ b/Constants/FileExtensions.cs @@ -10,4 +10,7 @@ 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 ZIP_EXTENSION = ".zip"; } diff --git a/Controllers/Utilities/SongCompression.cs b/Controllers/Utilities/SongCompression.cs index 1b4dca4..4c6d4aa 100644 --- a/Controllers/Utilities/SongCompression.cs +++ b/Controllers/Utilities/SongCompression.cs @@ -85,7 +85,7 @@ public class SongCompression if (songDetails.Filename.Contains(Constants.FileExtensions.WAV_EXTENSION)) { - _compressedSongFilename = StripMP3Extension(songDetails.Filename); + _compressedSongFilename = StripExtension(songDetails.Filename); } return tmpZipFilePath; @@ -110,7 +110,7 @@ public class SongCompression } - string StripMP3Extension(string filename) + string StripExtension(string filename) { Console.WriteLine($"Before: {filename}"); int filenameLength = filename.Length; @@ -119,7 +119,7 @@ public class SongCompression var startIndex = endIndex - 3; Console.WriteLine($"Starting index {startIndex} and ending index {endIndex}"); var stripped = filename.Remove(startIndex, 4); - stripped += ".zip"; + stripped += Constants.FileExtensions.ZIP_EXTENSION; Console.WriteLine($"After {stripped}"); return stripped; diff --git a/Controllers/v1/SongCompressedDataControllers.cs b/Controllers/v1/SongCompressedDataControllers.cs index 883ad6b..8a3c0c2 100644 --- a/Controllers/v1/SongCompressedDataControllers.cs +++ b/Controllers/v1/SongCompressedDataControllers.cs @@ -36,9 +36,9 @@ public class SongCompressedDataController : BaseController #region Constructor public SongCompressedDataController(IConfiguration config) { + _config = config; _songTempDir = _config.GetValue("TemporaryMusicPath"); _archiveDir = _config.GetValue("ArchivePath"); - _config = config; _connectionString = _config.GetConnectionString("DefaultConnection"); } #endregion @@ -46,18 +46,31 @@ public class SongCompressedDataController : BaseController #region API Routes [HttpGet("{id}")] - public async Task DownloadCompressedSong(int id) + public async Task 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"); - SongData song = await cmp.RetrieveCompressedSong(context.RetrieveRecord(new Song{ SongID = id })); + var sng = context.RetrieveRecord(new Song{ SongID = id }); + SongData song = await cmp.RetrieveCompressedSong(sng); - return File(song.Data, "application/x-msdownload", cmp.CompressedSongFilename); + var filename = string.Empty; + + if (randomizeFilename.HasValue && randomizeFilename.Value) + { + filename = Managers.DirectoryManager.GenerateFilename(10) + Constants.FileExtensions.ZIP_EXTENSION; + } + else + { + filename = sng.Title + Constants.FileExtensions.ZIP_EXTENSION; + } + + return File(song.Data, "application/x-msdownload", filename); } #endregion }