Created directory management capabilities #12
This commit is contained in:
@@ -0,0 +1,74 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
using Icarus.Models;
|
||||||
|
|
||||||
|
namespace Icarus.Controllers.Managers
|
||||||
|
{
|
||||||
|
public class DirectoryManager
|
||||||
|
{
|
||||||
|
#region Fields
|
||||||
|
private IConfiguration _config;
|
||||||
|
private Song _song;
|
||||||
|
private string _rootSongDirectory;
|
||||||
|
private string _songDirectory;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Properties
|
||||||
|
public string SongDirectory
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _songDirectory;
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_songDirectory = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
public DirectoryManager(IConfiguration config, Song song)
|
||||||
|
{
|
||||||
|
_config = config;
|
||||||
|
_song = song;
|
||||||
|
Initialize();
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
public void CreateDirectory()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_songDirectory = CreateDirectoryFromSong();
|
||||||
|
Console.WriteLine($"The song will be saved in the following" +
|
||||||
|
$" directory: {_songDirectory}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"An error occurred {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Initialize()
|
||||||
|
{
|
||||||
|
_rootSongDirectory = _config.GetValue<string>("RootMusicPath");
|
||||||
|
}
|
||||||
|
|
||||||
|
string CreateDirectoryFromSong()
|
||||||
|
{
|
||||||
|
string directory = _rootSongDirectory;
|
||||||
|
directory += $"{_song.Artist}//{_song.Album}//{_song.Title}.mp3";
|
||||||
|
|
||||||
|
return directory;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -25,7 +25,7 @@ namespace Icarus.Controllers.Managers
|
|||||||
private Song _song;
|
private Song _song;
|
||||||
private IConfiguration _config;
|
private IConfiguration _config;
|
||||||
private string _connectionString;
|
private string _connectionString;
|
||||||
private string _songRootDirectory;
|
private string _tempDirectoryRoot;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
@@ -60,10 +60,10 @@ namespace Icarus.Controllers.Managers
|
|||||||
_config = config;
|
_config = config;
|
||||||
Initialize();
|
Initialize();
|
||||||
}
|
}
|
||||||
public SongManager(IConfiguration config, string songDirectoryRoot)
|
public SongManager(IConfiguration config, string tempDirectoryRoot)
|
||||||
{
|
{
|
||||||
_config = config;
|
_config = config;
|
||||||
_songRootDirectory = songDirectoryRoot;
|
_tempDirectoryRoot = tempDirectoryRoot;
|
||||||
Initialize();
|
Initialize();
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
@@ -158,13 +158,22 @@ namespace Icarus.Controllers.Managers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var filePath = Path.Combine(_songRootDirectory, song.FileName);
|
var filePath = Path.Combine(_tempDirectoryRoot, song.FileName);
|
||||||
|
await SaveSongToFileSystemTemp(song, filePath);
|
||||||
|
System.IO.File.Delete(filePath);
|
||||||
|
|
||||||
|
|
||||||
|
DirectoryManager dirMgr = new DirectoryManager(_config, _song);
|
||||||
|
dirMgr.CreateDirectory();
|
||||||
|
filePath = dirMgr.SongDirectory;
|
||||||
|
|
||||||
using (var fileStream = new FileStream(filePath, FileMode.Create))
|
using (var fileStream = new FileStream(filePath, FileMode.Create))
|
||||||
{
|
{
|
||||||
await song.CopyToAsync(fileStream);
|
await song.CopyToAsync(fileStream);
|
||||||
_song = RetrieveMetaData(filePath);
|
|
||||||
_song.Filename = song.FileName;
|
|
||||||
SaveSongDetails();
|
Console.WriteLine($"Writing song to the directory: {filePath}");
|
||||||
|
Console.WriteLine("Song successfully saved");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -277,6 +286,17 @@ namespace Icarus.Controllers.Managers
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async Task SaveSongToFileSystemTemp(IFormFile song, string filePath)
|
||||||
|
{
|
||||||
|
using (var fileStream = new FileStream(filePath, FileMode.Create))
|
||||||
|
{
|
||||||
|
await song.CopyToAsync(fileStream);
|
||||||
|
_song = RetrieveMetaData(filePath);
|
||||||
|
_song.Filename = song.FileName;
|
||||||
|
SaveSongDetails();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Initialize()
|
void Initialize()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ namespace Icarus.Controllers
|
|||||||
#region Fields
|
#region Fields
|
||||||
private IConfiguration _config;
|
private IConfiguration _config;
|
||||||
private SongManager _songMgr;
|
private SongManager _songMgr;
|
||||||
private string _songDir;
|
private string _songTempDir;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
@@ -33,8 +33,8 @@ namespace Icarus.Controllers
|
|||||||
public SongDataController(IConfiguration config)
|
public SongDataController(IConfiguration config)
|
||||||
{
|
{
|
||||||
_config = config;
|
_config = config;
|
||||||
_songDir = _config.GetValue<string>("FilePath");
|
_songTempDir = _config.GetValue<string>("TemporaryMusicPath");
|
||||||
_songMgr = new SongManager(config, _songDir);
|
_songMgr = new SongManager(config, _songTempDir);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ namespace Icarus.Controllers
|
|||||||
{
|
{
|
||||||
Console.WriteLine("Uploading song...");
|
Console.WriteLine("Uploading song...");
|
||||||
|
|
||||||
var uploads = _songDir;
|
var uploads = _songTempDir;
|
||||||
Console.WriteLine($"Song Root Path {uploads}");
|
Console.WriteLine($"Song Root Path {uploads}");
|
||||||
foreach (var sng in songData)
|
foreach (var sng in songData)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user