* #79: Added query parameter to randomize filename when downloading songs Right now only the endpoint has this parameter added * #79: Added functionality to the comressed song endpoint * Added functionality to the download cover art
This commit was merged in pull request #96.
This commit is contained in:
@@ -10,4 +10,7 @@ public class FileExtensions
|
|||||||
|
|
||||||
// Contains file extension with period at the beginning
|
// Contains file extension with period at the beginning
|
||||||
public static string JPG_EXTENSION = ".jpg";
|
public static string JPG_EXTENSION = ".jpg";
|
||||||
|
|
||||||
|
// Contains file extension with period at the beginning
|
||||||
|
public static string ZIP_EXTENSION = ".zip";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Reflection.PortableExecutable;
|
||||||
using Icarus.Models;
|
using Icarus.Models;
|
||||||
using Icarus.Types;
|
using Icarus.Types;
|
||||||
|
|
||||||
@@ -42,13 +43,35 @@ public class DirectoryManager : BaseManager
|
|||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
// Does not include extension
|
||||||
|
public static string GenerateFilename(int length)
|
||||||
|
{
|
||||||
|
string chars = Constants.DirectoryPaths.FILENAME_CHARACTERS;
|
||||||
|
var random = new Random();
|
||||||
|
return new string(Enumerable.Repeat(chars, length).Select(s =>
|
||||||
|
s[random.Next(s.Length)]).ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GenerateDownloadFilename(int length, string extension, string title, bool? randomize)
|
||||||
|
{
|
||||||
|
if (randomize.HasValue && randomize.Value)
|
||||||
|
{
|
||||||
|
return GenerateFilename(length) + extension;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return title + extension;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateDirectory()
|
public void CreateDirectory()
|
||||||
{
|
{
|
||||||
CreateDirectory(_song);
|
this.CreateDirectory(_song);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CreateDirectory(Song song)
|
public void CreateDirectory(Song song)
|
||||||
{
|
{
|
||||||
_song = song;
|
this._song = song;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -69,6 +92,7 @@ public class DirectoryManager : BaseManager
|
|||||||
_logger.Error(msg, "An error occurred");
|
_logger.Error(msg, "An error occurred");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteEmptyDirectories()
|
public void DeleteEmptyDirectories()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -92,6 +116,7 @@ public class DirectoryManager : BaseManager
|
|||||||
Console.WriteLine($"An error occurred {exMsg}");
|
Console.WriteLine($"An error occurred {exMsg}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteEmptyDirectories(Song song)
|
public void DeleteEmptyDirectories(Song song)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -140,30 +165,42 @@ public class DirectoryManager : BaseManager
|
|||||||
{
|
{
|
||||||
_logger.Info("Generating song path");
|
_logger.Info("Generating song path");
|
||||||
|
|
||||||
var songPath = string.Empty;
|
|
||||||
var artistPath = ArtistDirectory(song);
|
var artistPath = ArtistDirectory(song);
|
||||||
var albumPath = AlbumDirectory(song);
|
var albumPath = AlbumDirectory(song);
|
||||||
|
|
||||||
if (!Directory.Exists(artistPath))
|
GenerateDirectories(new List<DirEnt>{
|
||||||
|
new DirEnt
|
||||||
|
{
|
||||||
|
Pre = "Artist path does not exist",
|
||||||
|
Path = artistPath,
|
||||||
|
Post = "Creating artist path"
|
||||||
|
},
|
||||||
|
new DirEnt
|
||||||
|
{
|
||||||
|
Pre = "Album path does not exist",
|
||||||
|
Path = albumPath,
|
||||||
|
Post = "Created album path"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return albumPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DirEnt
|
||||||
|
{
|
||||||
|
public string Pre { get; set;}
|
||||||
|
public string Path { get; set; }
|
||||||
|
public string Post { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateDirectories(List<DirEnt> dirs)
|
||||||
|
{
|
||||||
|
foreach (var di in dirs)
|
||||||
{
|
{
|
||||||
_logger.Info("Artist path does not exist");
|
_logger.Info(di.Pre);
|
||||||
|
Directory.CreateDirectory(di.Path);
|
||||||
Directory.CreateDirectory(artistPath);
|
_logger.Info(di.Post);
|
||||||
|
|
||||||
_logger.Info("Creating artist path");
|
|
||||||
}
|
}
|
||||||
if (!Directory.Exists(albumPath))
|
|
||||||
{
|
|
||||||
_logger.Info("Album path does not exist");
|
|
||||||
|
|
||||||
Directory.CreateDirectory(albumPath);
|
|
||||||
|
|
||||||
_logger.Info("Created album path");
|
|
||||||
}
|
|
||||||
|
|
||||||
songPath = albumPath;
|
|
||||||
|
|
||||||
return songPath;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Initialize(DirectoryType dirTypes = DirectoryType.Music)
|
private void Initialize(DirectoryType dirTypes = DirectoryType.Music)
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ public class SongCompression
|
|||||||
|
|
||||||
if (songDetails.Filename.Contains(Constants.FileExtensions.WAV_EXTENSION))
|
if (songDetails.Filename.Contains(Constants.FileExtensions.WAV_EXTENSION))
|
||||||
{
|
{
|
||||||
_compressedSongFilename = StripMP3Extension(songDetails.Filename);
|
_compressedSongFilename = StripExtension(songDetails.Filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
return tmpZipFilePath;
|
return tmpZipFilePath;
|
||||||
@@ -110,7 +110,7 @@ public class SongCompression
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
string StripMP3Extension(string filename)
|
string StripExtension(string filename)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Before: {filename}");
|
Console.WriteLine($"Before: {filename}");
|
||||||
int filenameLength = filename.Length;
|
int filenameLength = filename.Length;
|
||||||
@@ -119,7 +119,7 @@ public class SongCompression
|
|||||||
var startIndex = endIndex - 3;
|
var startIndex = endIndex - 3;
|
||||||
Console.WriteLine($"Starting index {startIndex} and ending index {endIndex}");
|
Console.WriteLine($"Starting index {startIndex} and ending index {endIndex}");
|
||||||
var stripped = filename.Remove(startIndex, 4);
|
var stripped = filename.Remove(startIndex, 4);
|
||||||
stripped += ".zip";
|
stripped += Constants.FileExtensions.ZIP_EXTENSION;
|
||||||
Console.WriteLine($"After {stripped}");
|
Console.WriteLine($"After {stripped}");
|
||||||
|
|
||||||
return stripped;
|
return stripped;
|
||||||
|
|||||||
@@ -74,15 +74,16 @@ public class CoverArtController : BaseController
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("data/download/{id}")]
|
[HttpGet("data/download/{id}")]
|
||||||
public async Task<IActionResult> Download(int id)
|
public async Task<IActionResult> Download(int id, [FromQuery] bool? randomizeFilename)
|
||||||
{
|
{
|
||||||
var songContext = new SongContext(_connectionString);
|
var songContext = new SongContext(_connectionString);
|
||||||
var covMgr = new CoverArtManager(this._config);
|
var covMgr = new CoverArtManager(this._config);
|
||||||
|
|
||||||
var songMetaData = songContext.RetrieveRecord(new Song { SongID = id});
|
var songMetaData = songContext.RetrieveRecord(new Song { SongID = id});
|
||||||
var filename = songMetaData.Title + Constants.FileExtensions.JPG_EXTENSION;
|
|
||||||
var c = covMgr.GetCoverArt(songMetaData);
|
var c = covMgr.GetCoverArt(songMetaData);
|
||||||
|
|
||||||
|
var filename = DirectoryManager.GenerateDownloadFilename(10, Constants.FileExtensions.JPG_EXTENSION, songMetaData.Title, randomizeFilename);
|
||||||
|
|
||||||
var data = await c.GetData();
|
var data = await c.GetData();
|
||||||
|
|
||||||
return File(data, "application/x-msdownload", filename);
|
return File(data, "application/x-msdownload", filename);
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using Icarus.Controllers.Utilities;
|
using Icarus.Controllers.Utilities;
|
||||||
using Icarus.Models;
|
using Icarus.Models;
|
||||||
using Icarus.Database.Contexts;
|
using Icarus.Database.Contexts;
|
||||||
|
using Icarus.Controllers.Managers;
|
||||||
|
|
||||||
namespace Icarus.Controllers.V1;
|
namespace Icarus.Controllers.V1;
|
||||||
|
|
||||||
@@ -26,9 +27,9 @@ public class SongCompressedDataController : BaseController
|
|||||||
#region Constructor
|
#region Constructor
|
||||||
public SongCompressedDataController(IConfiguration config)
|
public SongCompressedDataController(IConfiguration config)
|
||||||
{
|
{
|
||||||
|
_config = config;
|
||||||
_songTempDir = _config.GetValue<string>("TemporaryMusicPath");
|
_songTempDir = _config.GetValue<string>("TemporaryMusicPath");
|
||||||
_archiveDir = _config.GetValue<string>("ArchivePath");
|
_archiveDir = _config.GetValue<string>("ArchivePath");
|
||||||
_config = config;
|
|
||||||
_connectionString = _config.GetConnectionString("DefaultConnection");
|
_connectionString = _config.GetConnectionString("DefaultConnection");
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
@@ -36,18 +37,22 @@ public class SongCompressedDataController : BaseController
|
|||||||
|
|
||||||
#region API Routes
|
#region API Routes
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
public async Task<IActionResult> DownloadCompressedSong(int id)
|
public async Task<IActionResult> DownloadCompressedSong(int id, [FromQuery] bool? randomizeFilename)
|
||||||
{
|
{
|
||||||
var context = new SongContext(_connectionString);
|
var context = new SongContext(_connectionString);
|
||||||
|
|
||||||
SongCompression cmp = new SongCompression(_archiveDir);
|
SongCompression cmp = new SongCompression(_archiveDir);
|
||||||
|
|
||||||
|
|
||||||
Console.WriteLine($"Archive directory root: {_archiveDir}");
|
Console.WriteLine($"Archive directory root: {_archiveDir}");
|
||||||
|
|
||||||
Console.WriteLine("Starting process of retrieving comrpessed song");
|
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 = DirectoryManager.GenerateDownloadFilename(10, Constants.FileExtensions.ZIP_EXTENSION, sng.Title, randomizeFilename);
|
||||||
|
|
||||||
|
return File(song.Data, "application/x-msdownload", filename);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,14 +37,15 @@ public class SongDataController : BaseController
|
|||||||
|
|
||||||
|
|
||||||
[HttpGet("download/{id}")]
|
[HttpGet("download/{id}")]
|
||||||
public IActionResult Download(int id)
|
public IActionResult Download(int id, [FromQuery] bool? randomizeFilename)
|
||||||
{
|
{
|
||||||
var songContext = new SongContext(_connectionString);
|
var songContext = new SongContext(_connectionString);
|
||||||
var songMetaData = songContext.RetrieveRecord(new Song { SongID = id});
|
var songMetaData = songContext.RetrieveRecord(new Song { SongID = id});
|
||||||
|
|
||||||
var song = _songMgr.RetrieveSong(songMetaData).Result;
|
var song = _songMgr.RetrieveSong(songMetaData).Result;
|
||||||
|
var filename = DirectoryManager.GenerateDownloadFilename(10, Constants.FileExtensions.WAV_EXTENSION, songMetaData.Title, randomizeFilename);
|
||||||
|
|
||||||
return File(song.Data, "application/x-msdownload", songMetaData.Filename);
|
return File(song.Data, "application/x-msdownload", filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assumes that the song already has metadata such as
|
// Assumes that the song already has metadata such as
|
||||||
|
|||||||
+10
-7
@@ -72,9 +72,7 @@ public class Song
|
|||||||
{
|
{
|
||||||
int length = Constants.DirectoryPaths.FILENAME_LENGTH;
|
int length = Constants.DirectoryPaths.FILENAME_LENGTH;
|
||||||
string chars = Constants.DirectoryPaths.FILENAME_CHARACTERS;
|
string chars = Constants.DirectoryPaths.FILENAME_CHARACTERS;
|
||||||
var random = new Random();
|
var filename = this.Generate(length, chars);
|
||||||
var filename = new string(Enumerable.Repeat(chars, length).Select(s =>
|
|
||||||
s[random.Next(s.Length)]).ToArray());
|
|
||||||
var extension = Icarus.Constants.FileExtensions.WAV_EXTENSION;
|
var extension = Icarus.Constants.FileExtensions.WAV_EXTENSION;
|
||||||
|
|
||||||
return flag == 0 ? filename : $"{filename}{extension}";
|
return flag == 0 ? filename : $"{filename}{extension}";
|
||||||
@@ -84,15 +82,20 @@ public class Song
|
|||||||
int length = Constants.DirectoryPaths.FILENAME_LENGTH;
|
int length = Constants.DirectoryPaths.FILENAME_LENGTH;
|
||||||
string chars = Constants.DirectoryPaths.FILENAME_CHARACTERS;
|
string chars = Constants.DirectoryPaths.FILENAME_CHARACTERS;
|
||||||
var extension = Icarus.Constants.FileExtensions.WAV_EXTENSION;
|
var extension = Icarus.Constants.FileExtensions.WAV_EXTENSION;
|
||||||
var random = new Random();
|
|
||||||
var filename = await Task.Run(() =>
|
var filename = await Task.Run(() =>
|
||||||
{
|
{
|
||||||
return new string(Enumerable.Repeat(chars, length).Select(s =>
|
return this.Generate(length, chars);
|
||||||
s[random.Next(s.Length)]).ToArray());
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
return flag == 0 ? filename : $"{filename}{extension}";
|
return flag == 0 ? filename : $"{filename}{extension}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string Generate(int length, string chars)
|
||||||
|
{
|
||||||
|
var random = new Random();
|
||||||
|
var filename = new string(Enumerable.Repeat(chars, length).Select(s =>
|
||||||
|
s[random.Next(s.Length)]).ToArray());
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user