diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index 650eaa5..caf3356 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -31,6 +31,8 @@ namespace Icarus.Controllers.Managers private IConfiguration _config; private string _connectionString; private string _tempDirectoryRoot; + private string _archiveDirectoryRoot; + private string _compressedSongFilename; #endregion @@ -40,6 +42,17 @@ namespace Icarus.Controllers.Managers get => _song; set => _song = value; } + + public string ArchiveDirectoryRoot + { + get => _archiveDirectoryRoot; + set => _archiveDirectoryRoot = value; + } + public string CompressedSongFilename + { + get => _compressedSongFilename; + set => _compressedSongFilename = value; + } #endregion @@ -283,10 +296,19 @@ namespace Icarus.Controllers.Managers { _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); + + SongCompression compressed = new SongCompression(_archiveDirectoryRoot); + Console.WriteLine("SongCompression Initialized"); + + var compressedPath = compressed.RetrieveCompressesSongPath(_song); + Console.WriteLine($"Path of compressed song: {compressedPath}"); + + song.Data = System.IO.File.ReadAllBytes(compressedPath); + + _compressedSongFilename = compressed.CompressedSongFilename; } catch (Exception ex) { diff --git a/Controllers/SongCompressedDataControllers.cs b/Controllers/SongCompressedDataControllers.cs index a899d48..49847fd 100644 --- a/Controllers/SongCompressedDataControllers.cs +++ b/Controllers/SongCompressedDataControllers.cs @@ -22,6 +22,7 @@ namespace Icarus.Controllers private IConfiguration _config; private SongManager _songMgr; private string _songTempDir; + private string _archiveDir; #endregion @@ -34,60 +35,26 @@ namespace Icarus.Controllers { _config = config; _songTempDir = _config.GetValue("TemporaryMusicPath"); + _archiveDir = _config.GetValue("ArchivePath"); _songMgr = new SongManager(config, _songTempDir); + _songMgr.ArchiveDirectoryRoot = _archiveDir; } #endregion - [HttpGet] - public ActionResult> Get() - { - List songs = new List(); - - return songs; - } - + #region API Routes [HttpGet("{id}")] public async Task Get(int id) { SongData song = new SongData(); + Console.WriteLine($"Archive directory root: {_archiveDir}"); + 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) - { + return File(song.Data, "application/x-msdownload", _songMgr.CompressedSongFilename); } + #endregion } } diff --git a/Controllers/Utilities/SongCompression.cs b/Controllers/Utilities/SongCompression.cs index 9654bc0..bcf7ccb 100644 --- a/Controllers/Utilities/SongCompression.cs +++ b/Controllers/Utilities/SongCompression.cs @@ -1,20 +1,28 @@ using System; using System.IO; -using System.IO.Compression; using System.Threading.Tasks; -using SevenZip.Compression.LZMA; +using Ionic.Zip; + +using Icarus.Models; namespace Icarus.Controllers.Utilities { public class SongCompression { #region Fields + string _compressedSongFilename; + string _tempDirectory; byte[] _uncompressedSong; #endregion #region Propterties + public string CompressedSongFilename + { + get => _compressedSongFilename; + set => _compressedSongFilename = value; + } #endregion @@ -22,6 +30,10 @@ namespace Icarus.Controllers.Utilities public SongCompression() { } + public SongCompression(string tempDirectory) + { + _tempDirectory = tempDirectory; + } public SongCompression(byte[] uncompressedSong) { _uncompressedSong = uncompressedSong; @@ -30,40 +42,40 @@ namespace Icarus.Controllers.Utilities #region Methods + public string RetrieveCompressesSongPath(Song songDetails) + { + string tmpZipFilePath = _tempDirectory + songDetails.Filename; + + try + { + using (ZipFile zip = new ZipFile()) + { + zip.AddFile(songDetails.SongPath); + zip.Save(tmpZipFilePath); + } + Console.WriteLine("Successfully compressed"); + } + catch (Exception ex) + { + var exMsg = ex.Message; + Console.WriteLine("An error ocurred"); + Console.WriteLine(exMsg); + } + + if (songDetails.Filename.Contains(".mp3")) + { + _compressedSongFilename = StripMP3Extension(songDetails.Filename); + } + + return tmpZipFilePath; + } + + // Method not being used public byte[] CompressedSong(byte[] uncompressedSong) { byte[] compressedSong = null; try { - /** - 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) @@ -75,6 +87,22 @@ namespace Icarus.Controllers.Utilities return compressedSong; } + + + string StripMP3Extension(string filename) + { + Console.WriteLine($"Before: {filename}"); + int filenameLength = filename.Length; + Console.WriteLine($"Filename length {filenameLength}"); + var endIndex = filenameLength - 1; + var startIndex = endIndex - 3; + Console.WriteLine($"Starting index {startIndex} and ending index {endIndex}"); + var stripped = filename.Remove(startIndex, 4); + stripped += ".zip"; + Console.WriteLine($"After {stripped}"); + + return stripped; + } #endregion } } diff --git a/Icarus.csproj b/Icarus.csproj index 8e295bf..475765e 100644 --- a/Icarus.csproj +++ b/Icarus.csproj @@ -6,6 +6,7 @@ + diff --git a/appsettings.Development.json b/appsettings.Development.json index 7a55dbe..c1e040a 100644 --- a/appsettings.Development.json +++ b/appsettings.Development.json @@ -9,6 +9,7 @@ "ConnectionStrings": { "IcarusDev":"Server=; User=; Password=; Database=; Port=" }, - "RootMusicPath":"/root/music/path/", - "TemporaryMusicPath":"/root/temp/music/path/" + "RootMusicPath":"/root/of/music/path/", + "TemporaryMusicPath":"/root/temp/music/path/", + "ArchivePath":"/root/of/archive/path/" } diff --git a/appsettings.json b/appsettings.json index b5e6912..dbd1be1 100644 --- a/appsettings.json +++ b/appsettings.json @@ -9,5 +9,6 @@ "IcarusProd":"Server=; User=; Password=; Database=; Port=" }, "RootMusicPath":"/root/of/music/path/", - "TemporaryMusicPath":"/root/temp/music/path/" + "TemporaryMusicPath":"/root/temp/music/path/", + "ArchivePath":"/root/of/archive/path/" }