From 2d26d6ecdc4feacb827eec6136c58f6408a5f784 Mon Sep 17 00:00:00 2001 From: amazing-username Date: Mon, 8 Apr 2019 22:29:52 -0400 Subject: [PATCH] Partially implemented song compression #14 --- Controllers/Managers/DirectoryManager.cs | 10 +-- Controllers/Managers/SongManager.cs | 15 ++-- Controllers/SongCompressedDataControllers.cs | 93 ++++++++++++++++++++ Controllers/SongDataController.cs | 3 +- Controllers/Utilities/SongCompression.cs | 29 +++++- Controllers/ValuesController.cs | 45 ---------- 6 files changed, 128 insertions(+), 67 deletions(-) create mode 100644 Controllers/SongCompressedDataControllers.cs delete mode 100644 Controllers/ValuesController.cs diff --git a/Controllers/Managers/DirectoryManager.cs b/Controllers/Managers/DirectoryManager.cs index d937695..b46d875 100644 --- a/Controllers/Managers/DirectoryManager.cs +++ b/Controllers/Managers/DirectoryManager.cs @@ -20,14 +20,8 @@ namespace Icarus.Controllers.Managers #region Properties public string SongDirectory { - get - { - return _songDirectory; - } - set - { - _songDirectory = value; - } + get => _songDirectory; + set => _songDirectory = value; } #endregion diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index b8608ae..650eaa5 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -37,14 +37,8 @@ namespace Icarus.Controllers.Managers #region Properties public Song SongDetails { - get - { - return _song; - } - set - { - _song = value; - } + get => _song; + set => _song = value; } #endregion @@ -288,6 +282,11 @@ namespace Icarus.Controllers.Managers try { _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) { diff --git a/Controllers/SongCompressedDataControllers.cs b/Controllers/SongCompressedDataControllers.cs new file mode 100644 index 0000000..a899d48 --- /dev/null +++ b/Controllers/SongCompressedDataControllers.cs @@ -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("TemporaryMusicPath"); + _songMgr = new SongManager(config, _songTempDir); + } + #endregion + + + [HttpGet] + public ActionResult> Get() + { + List songs = new List(); + + return songs; + } + + [HttpGet("{id}")] + public async Task 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 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) + { + } + } +} diff --git a/Controllers/SongDataController.cs b/Controllers/SongDataController.cs index b74dd1d..03099e4 100644 --- a/Controllers/SongDataController.cs +++ b/Controllers/SongDataController.cs @@ -14,7 +14,7 @@ using Icarus.Models; namespace Icarus.Controllers { - [Route("api/Song/data")] + [Route("api/song/data")] [ApiController] public class SongDataController : ControllerBase { @@ -56,7 +56,6 @@ namespace Icarus.Controllers song = await _songMgr.RetrieveSong(id); return File(song.Data, "application/x-msdownload", _songMgr.SongDetails.Filename); - //return File(song.Data, "application/x-msdownload", "demo.zip"); } [HttpPost] diff --git a/Controllers/Utilities/SongCompression.cs b/Controllers/Utilities/SongCompression.cs index eb9be1d..9654bc0 100644 --- a/Controllers/Utilities/SongCompression.cs +++ b/Controllers/Utilities/SongCompression.cs @@ -3,7 +3,6 @@ using System.IO; using System.IO.Compression; using System.Threading.Tasks; - using SevenZip.Compression.LZMA; namespace Icarus.Controllers.Utilities @@ -31,19 +30,41 @@ namespace Icarus.Controllers.Utilities #region Methods - //public async Task CompressedSong(byte[] uncompressedSong) - public async Task CompressedSong(byte[] uncompressedSong) + public byte[] CompressedSong(byte[] uncompressedSong) { byte[] compressedSong = null; try { - //compressedSong = SevenZip.Compression.LZMA.SevenZipHelper.Compress(uncompressedSong); + /** MemoryStream output = new MemoryStream(); using (DeflateStream dstream = new DeflateStream(output, CompressionLevel.Optimal)) { dstream.Write(uncompressedSong, 0, uncompressedSong.Length); 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) { diff --git a/Controllers/ValuesController.cs b/Controllers/ValuesController.cs deleted file mode 100644 index 2ba1d29..0000000 --- a/Controllers/ValuesController.cs +++ /dev/null @@ -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> Get() - { - return new string[] { "value1", "value2" }; - } - - // GET api/values/5 - [HttpGet("{id}")] - public ActionResult 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) - { - } - } -}