#79: Added query parameter to randomize filename when downloading songs #96

Merged
kdeng00 merged 4 commits from tsk-79 into master 2024-06-16 19:04:10 -04:00
3 changed files with 23 additions and 7 deletions
Showing only changes of commit 0394ad38da - Show all commits
+3
View File
@@ -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";
}
+3 -3
View File
@@ -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;
@@ -36,9 +36,9 @@ public class SongCompressedDataController : BaseController
#region Constructor
public SongCompressedDataController(IConfiguration config)
{
_config = config;
_songTempDir = _config.GetValue<string>("TemporaryMusicPath");
_archiveDir = _config.GetValue<string>("ArchivePath");
_config = config;
_connectionString = _config.GetConnectionString("DefaultConnection");
}
#endregion
@@ -46,18 +46,31 @@ public class SongCompressedDataController : BaseController
#region API Routes
[HttpGet("{id}")]
public async Task<IActionResult> DownloadCompressedSong(int id)
public async Task<IActionResult> 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
}