Added functionality to the download cover art
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Reflection.PortableExecutable;
|
||||||
using Icarus.Models;
|
using Icarus.Models;
|
||||||
using Icarus.Types;
|
using Icarus.Types;
|
||||||
|
|
||||||
@@ -50,13 +51,27 @@ public class DirectoryManager : BaseManager
|
|||||||
return new string(Enumerable.Repeat(chars, length).Select(s =>
|
return new string(Enumerable.Repeat(chars, length).Select(s =>
|
||||||
s[random.Next(s.Length)]).ToArray());
|
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
|
||||||
{
|
{
|
||||||
@@ -77,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
|
||||||
@@ -100,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
|
||||||
@@ -148,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)
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
@@ -49,16 +50,7 @@ public class SongCompressedDataController : BaseController
|
|||||||
var sng = context.RetrieveRecord(new Song{ SongID = id });
|
var sng = context.RetrieveRecord(new Song{ SongID = id });
|
||||||
SongData song = await cmp.RetrieveCompressedSong(sng);
|
SongData song = await cmp.RetrieveCompressedSong(sng);
|
||||||
|
|
||||||
var filename = string.Empty;
|
var filename = DirectoryManager.GenerateDownloadFilename(10, Constants.FileExtensions.ZIP_EXTENSION, sng.Title, randomizeFilename);
|
||||||
|
|
||||||
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);
|
return File(song.Data, "application/x-msdownload", filename);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,16 +43,7 @@ public class SongDataController : BaseController
|
|||||||
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 = "";
|
var filename = DirectoryManager.GenerateDownloadFilename(10, Constants.FileExtensions.WAV_EXTENSION, songMetaData.Title, randomizeFilename);
|
||||||
|
|
||||||
if (randomizeFilename.HasValue && randomizeFilename.Value)
|
|
||||||
{
|
|
||||||
filename = Managers.DirectoryManager.GenerateFilename(10) + Constants.FileExtensions.WAV_EXTENSION;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
filename = songMetaData.Title + Constants.FileExtensions.WAV_EXTENSION;
|
|
||||||
}
|
|
||||||
|
|
||||||
return File(song.Data, "application/x-msdownload", filename);
|
return File(song.Data, "application/x-msdownload", filename);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user