Partially implemented song compression #14
This commit is contained in:
@@ -20,14 +20,8 @@ namespace Icarus.Controllers.Managers
|
|||||||
#region Properties
|
#region Properties
|
||||||
public string SongDirectory
|
public string SongDirectory
|
||||||
{
|
{
|
||||||
get
|
get => _songDirectory;
|
||||||
{
|
set => _songDirectory = value;
|
||||||
return _songDirectory;
|
|
||||||
}
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_songDirectory = value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|||||||
@@ -37,14 +37,8 @@ namespace Icarus.Controllers.Managers
|
|||||||
#region Properties
|
#region Properties
|
||||||
public Song SongDetails
|
public Song SongDetails
|
||||||
{
|
{
|
||||||
get
|
get => _song;
|
||||||
{
|
set => _song = value;
|
||||||
return _song;
|
|
||||||
}
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_song = value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -288,6 +282,11 @@ namespace Icarus.Controllers.Managers
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
_song = await RetrieveSongDetails(id);
|
_song = await RetrieveSongDetails(id);
|
||||||
|
Console.WriteLine("Retrieved details of song");
|
||||||
|
song = RetrieveSongFromFileSystem(_song);
|
||||||
|
Console.WriteLine("Retrieved song from filesystem");
|
||||||
|
SongCompression compressed = new SongCompression();
|
||||||
|
song.Data = compressed.CompressedSong(song.Data);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Configuration;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
using Icarus.Controllers.Managers;
|
||||||
|
using Icarus.Models;
|
||||||
|
|
||||||
|
namespace Icarus.Controllers
|
||||||
|
{
|
||||||
|
[Route("api/song/compressed/data")]
|
||||||
|
[ApiController]
|
||||||
|
public class SongCompressedDataController : ControllerBase
|
||||||
|
{
|
||||||
|
#region Fields
|
||||||
|
private IConfiguration _config;
|
||||||
|
private SongManager _songMgr;
|
||||||
|
private string _songTempDir;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Properties
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Constructor
|
||||||
|
public SongCompressedDataController(IConfiguration config)
|
||||||
|
{
|
||||||
|
_config = config;
|
||||||
|
_songTempDir = _config.GetValue<string>("TemporaryMusicPath");
|
||||||
|
_songMgr = new SongManager(config, _songTempDir);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public ActionResult<IEnumerable<SongData>> Get()
|
||||||
|
{
|
||||||
|
List<SongData> songs = new List<SongData>();
|
||||||
|
|
||||||
|
return songs;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
public async Task<IActionResult> Get(int id)
|
||||||
|
{
|
||||||
|
SongData song = new SongData();
|
||||||
|
|
||||||
|
Console.WriteLine("Starting process of retrieving comrpessed song");
|
||||||
|
song = await _songMgr.RetrieveCompressedSong(id);
|
||||||
|
|
||||||
|
return File(song.Data, "application/x-msdownload", "demo.zip");
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public async Task Post([FromForm(Name = "file")] List<IFormFile> songData)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Console.WriteLine("Uploading song...");
|
||||||
|
|
||||||
|
var uploads = _songTempDir;
|
||||||
|
Console.WriteLine($"Song Root Path {uploads}");
|
||||||
|
foreach (var sng in songData)
|
||||||
|
{
|
||||||
|
if (sng.Length > 0) {
|
||||||
|
await _songMgr.SaveSongToFileSystem(sng);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"An error occurred: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut("{id}")]
|
||||||
|
public void Put(int id, [FromBody] SongData song)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete("{id}")]
|
||||||
|
public void Delete(int id)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,7 +14,7 @@ using Icarus.Models;
|
|||||||
|
|
||||||
namespace Icarus.Controllers
|
namespace Icarus.Controllers
|
||||||
{
|
{
|
||||||
[Route("api/Song/data")]
|
[Route("api/song/data")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class SongDataController : ControllerBase
|
public class SongDataController : ControllerBase
|
||||||
{
|
{
|
||||||
@@ -56,7 +56,6 @@ namespace Icarus.Controllers
|
|||||||
song = await _songMgr.RetrieveSong(id);
|
song = await _songMgr.RetrieveSong(id);
|
||||||
|
|
||||||
return File(song.Data, "application/x-msdownload", _songMgr.SongDetails.Filename);
|
return File(song.Data, "application/x-msdownload", _songMgr.SongDetails.Filename);
|
||||||
//return File(song.Data, "application/x-msdownload", "demo.zip");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ using System.IO;
|
|||||||
using System.IO.Compression;
|
using System.IO.Compression;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
|
||||||
using SevenZip.Compression.LZMA;
|
using SevenZip.Compression.LZMA;
|
||||||
|
|
||||||
namespace Icarus.Controllers.Utilities
|
namespace Icarus.Controllers.Utilities
|
||||||
@@ -31,19 +30,41 @@ namespace Icarus.Controllers.Utilities
|
|||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
//public async Task<byte[]> CompressedSong(byte[] uncompressedSong)
|
public byte[] CompressedSong(byte[] uncompressedSong)
|
||||||
public async Task<byte[]> CompressedSong(byte[] uncompressedSong)
|
|
||||||
{
|
{
|
||||||
byte[] compressedSong = null;
|
byte[] compressedSong = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//compressedSong = SevenZip.Compression.LZMA.SevenZipHelper.Compress(uncompressedSong);
|
/**
|
||||||
MemoryStream output = new MemoryStream();
|
MemoryStream output = new MemoryStream();
|
||||||
using (DeflateStream dstream = new DeflateStream(output, CompressionLevel.Optimal))
|
using (DeflateStream dstream = new DeflateStream(output, CompressionLevel.Optimal))
|
||||||
{
|
{
|
||||||
dstream.Write(uncompressedSong, 0, uncompressedSong.Length);
|
dstream.Write(uncompressedSong, 0, uncompressedSong.Length);
|
||||||
compressedSong = output.ToArray();
|
compressedSong = output.ToArray();
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
// TODO: Implement song compression
|
||||||
|
|
||||||
|
FileStream sourceFile = File.ReadAllBytes(uncompressedSong);
|
||||||
|
FileStream destinationFile = File.Create(path + ".gz");
|
||||||
|
|
||||||
|
byte[] buffer = new byte[sourceFile.Length];
|
||||||
|
sourceFile.Read(uncompressedSong, 0, uncompressedSong.Length);
|
||||||
|
|
||||||
|
using (GZipStream output = new GZipStream(destinationFile,
|
||||||
|
CompressionMode.Compress))
|
||||||
|
{
|
||||||
|
Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name,
|
||||||
|
destinationFile.Name, false);
|
||||||
|
|
||||||
|
output.Write(buffer, 0, buffer.Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceFile.Close();
|
||||||
|
destinationFile.Close();
|
||||||
|
|
||||||
|
Console.WriteLine("Song has been successfully compressed");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
|
|
||||||
namespace Icarus.Controllers
|
|
||||||
{
|
|
||||||
[Route("api/[controller]")]
|
|
||||||
[ApiController]
|
|
||||||
public class ValuesController : ControllerBase
|
|
||||||
{
|
|
||||||
// GET api/values
|
|
||||||
[HttpGet]
|
|
||||||
public ActionResult<IEnumerable<string>> Get()
|
|
||||||
{
|
|
||||||
return new string[] { "value1", "value2" };
|
|
||||||
}
|
|
||||||
|
|
||||||
// GET api/values/5
|
|
||||||
[HttpGet("{id}")]
|
|
||||||
public ActionResult<string> Get(int id)
|
|
||||||
{
|
|
||||||
return "value";
|
|
||||||
}
|
|
||||||
|
|
||||||
// POST api/values
|
|
||||||
[HttpPost]
|
|
||||||
public void Post([FromBody] string value)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
// PUT api/values/5
|
|
||||||
[HttpPut("{id}")]
|
|
||||||
public void Put(int id, [FromBody] string value)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
// DELETE api/values/5
|
|
||||||
[HttpDelete("{id}")]
|
|
||||||
public void Delete(int id)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user