Compression of songs implemented #14

This commit is contained in:
amazing-username
2019-04-09 03:50:42 -04:00
parent 2d26d6ecdc
commit a17e892a8b
6 changed files with 97 additions and 77 deletions
+24 -2
View File
@@ -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)
{
+8 -41
View File
@@ -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<string>("TemporaryMusicPath");
_archiveDir = _config.GetValue<string>("ArchivePath");
_songMgr = new SongManager(config, _songTempDir);
_songMgr.ArchiveDirectoryRoot = _archiveDir;
}
#endregion
[HttpGet]
public ActionResult<IEnumerable<SongData>> Get()
{
List<SongData> songs = new List<SongData>();
return songs;
}
#region API Routes
[HttpGet("{id}")]
public async Task<IActionResult> 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<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)
{
return File(song.Data, "application/x-msdownload", _songMgr.CompressedSongFilename);
}
#endregion
}
}
+59 -31
View File
@@ -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
}
}
+1
View File
@@ -6,6 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DotNetZip" Version="1.13.3" />
<PackageReference Include="ID3" Version="0.6.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
+3 -2
View File
@@ -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/"
}
+2 -1
View File
@@ -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/"
}