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 IConfiguration _config;
|
||||
private string _connectionString;
|
||||
private string _songRootDirectory;
|
||||
private string _tempDirectoryRoot;
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -60,10 +60,10 @@ namespace Icarus.Controllers.Managers
|
||||
_config = config;
|
||||
Initialize();
|
||||
}
|
||||
public SongManager(IConfiguration config, string songDirectoryRoot)
|
||||
public SongManager(IConfiguration config, string tempDirectoryRoot)
|
||||
{
|
||||
_config = config;
|
||||
_songRootDirectory = songDirectoryRoot;
|
||||
_tempDirectoryRoot = tempDirectoryRoot;
|
||||
Initialize();
|
||||
}
|
||||
#endregion
|
||||
@@ -158,13 +158,22 @@ namespace Icarus.Controllers.Managers
|
||||
{
|
||||
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))
|
||||
{
|
||||
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)
|
||||
@@ -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()
|
||||
{
|
||||
try
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace Icarus.Controllers
|
||||
#region Fields
|
||||
private IConfiguration _config;
|
||||
private SongManager _songMgr;
|
||||
private string _songDir;
|
||||
private string _songTempDir;
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -33,8 +33,8 @@ namespace Icarus.Controllers
|
||||
public SongDataController(IConfiguration config)
|
||||
{
|
||||
_config = config;
|
||||
_songDir = _config.GetValue<string>("FilePath");
|
||||
_songMgr = new SongManager(config, _songDir);
|
||||
_songTempDir = _config.GetValue<string>("TemporaryMusicPath");
|
||||
_songMgr = new SongManager(config, _songTempDir);
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -65,7 +65,7 @@ namespace Icarus.Controllers
|
||||
{
|
||||
Console.WriteLine("Uploading song...");
|
||||
|
||||
var uploads = _songDir;
|
||||
var uploads = _songTempDir;
|
||||
Console.WriteLine($"Song Root Path {uploads}");
|
||||
foreach (var sng in songData)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user