8900afdfd2
* Added script shell to convert wav files to flac * #93: Minor change to script to convert wav files to flac * #93: Added more code to for the music file conversion * #93: Script to convert wav files to flac files is done * #93: Clean up * #93: Starting work to implement flac support * #93: Minor update to conversion script * #93: Added dotnet manifest file Includes the dotnet-ef tool * #93: Updated README and migration script * #93: Created skeleton for saving flac files * #93: Making some changes to how the file extension is determined * #93: Updated gitignore * #93: Another gitignore update * #93: Making some changes to the process of saving a song to the filesystem * #93: Adding SQL script to drop and create database * #93: Saving uploaded song to a temporary location for processing * #93: Initializing some properties in the song model earlier * #93: Uploading wav songs works. Moving on to flac support * #93: Added more code to save flac files * #93: Updated some code that affects filename generation for audio files * #93: Song Model changes and support for different file types * #93: Working on build errors, removed DotnetZip dependency, and added new Ionic.Zlip.Netstandard dependency * #93: Reduce build errors * #93: Removed ID3 dependency * #93: Cleanup
262 lines
6.7 KiB
C#
262 lines
6.7 KiB
C#
using Icarus.Models;
|
|
using Icarus.Types;
|
|
|
|
namespace Icarus.Controllers.Managers;
|
|
|
|
// NOTE: Do not use metadata for the song's metadata
|
|
public class DirectoryManager : BaseManager
|
|
{
|
|
#region Fields
|
|
private Song? _song;
|
|
private string? _rootSongDirectory;
|
|
private string? _songDirectory;
|
|
#endregion
|
|
|
|
|
|
#region Properties
|
|
public string? SongDirectory
|
|
{
|
|
get => _songDirectory;
|
|
set => _songDirectory = value;
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region Constructors
|
|
public DirectoryManager(IConfiguration config, Song song)
|
|
{
|
|
_config = config;
|
|
_song = song;
|
|
Initialize();
|
|
}
|
|
public DirectoryManager(IConfiguration config)
|
|
{
|
|
_config = config;
|
|
Initialize();
|
|
}
|
|
public DirectoryManager(string rootDirectory)
|
|
{
|
|
_rootSongDirectory = rootDirectory;
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region Methods
|
|
// Does not include extension
|
|
public static string GenerateFilename(int length)
|
|
{
|
|
string chars = Constants.DirectoryPaths.FILENAME_CHARACTERS;
|
|
var random = new Random();
|
|
return new string(Enumerable.Repeat(chars, length).Select(s =>
|
|
s[random.Next(s.Length)]).ToArray());
|
|
}
|
|
|
|
public static string GenerateDownloadFilename(int length, string extension, string title, bool? randomize)
|
|
{
|
|
if (randomize.HasValue && randomize.Value)
|
|
{
|
|
return GenerateFilename(length) + extension;
|
|
}
|
|
else
|
|
{
|
|
return title + extension;
|
|
}
|
|
}
|
|
|
|
public void CreateDirectory()
|
|
{
|
|
this.CreateDirectory(_song!);
|
|
}
|
|
|
|
public void CreateDirectory(Song song)
|
|
{
|
|
this._song = song;
|
|
|
|
try
|
|
{
|
|
_songDirectory = AlbumDirectory();
|
|
|
|
if (!Directory.Exists(_songDirectory))
|
|
{
|
|
Directory.CreateDirectory(_songDirectory);
|
|
Console.WriteLine($"The directory has been created");
|
|
}
|
|
|
|
Console.WriteLine($"The song will be saved in the following" +
|
|
$" directory: {_songDirectory}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var msg = ex.Message;
|
|
_logger.Error(msg, "An error occurred");
|
|
}
|
|
}
|
|
|
|
public void DeleteEmptyDirectories()
|
|
{
|
|
try
|
|
{
|
|
var albumDirectory = AlbumDirectory();
|
|
var artistDirectory = ArtistDirectory();
|
|
if (IsDirectoryEmpty(albumDirectory))
|
|
{
|
|
Directory.Delete(albumDirectory);
|
|
Console.WriteLine($"directory {albumDirectory} deleted");
|
|
}
|
|
if (IsDirectoryEmpty(artistDirectory))
|
|
{
|
|
Directory.Delete(artistDirectory);
|
|
Console.WriteLine($"directory {artistDirectory} deleted");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var exMsg = ex.Message;
|
|
Console.WriteLine($"An error occurred {exMsg}");
|
|
}
|
|
}
|
|
|
|
public void DeleteEmptyDirectories(Song song)
|
|
{
|
|
try
|
|
{
|
|
var albumDirectory = AlbumDirectory(song);
|
|
var artistDirectory = ArtistDirectory(song);
|
|
|
|
if (IsDirectoryEmpty(albumDirectory))
|
|
{
|
|
Directory.Delete(albumDirectory);
|
|
_logger.Info("Album directory deleted");
|
|
}
|
|
if (IsDirectoryEmpty(artistDirectory))
|
|
{
|
|
Directory.Delete(artistDirectory);
|
|
_logger.Info("Artist directory deleted");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var msg = ex.Message;
|
|
_logger.Error(msg, "An error occurred");
|
|
}
|
|
}
|
|
|
|
public string RetrieveAlbumPath(Song song)
|
|
{
|
|
_logger.Info("Retrieving album song path");
|
|
|
|
var albumPath = string.Empty;
|
|
albumPath = AlbumDirectory(song);
|
|
|
|
return albumPath;
|
|
}
|
|
public string RetrieveArtistPath(Song song)
|
|
{
|
|
_logger.Info("Retrieving artist path");
|
|
|
|
var artistPath = string.Empty;
|
|
artistPath = ArtistDirectory(song);
|
|
|
|
return artistPath;
|
|
}
|
|
|
|
public string GenerateSongPath(Song song)
|
|
{
|
|
_logger.Info("Generating song path");
|
|
|
|
var artistPath = ArtistDirectory(song);
|
|
var albumPath = AlbumDirectory(song);
|
|
|
|
GenerateDirectories(new List<DirEnt>{
|
|
new DirEnt
|
|
{
|
|
Pre = "Artist path does not exist",
|
|
Path = artistPath,
|
|
Post = "Creating artist path"
|
|
},
|
|
new DirEnt
|
|
{
|
|
Pre = "Album path does not exist",
|
|
Path = albumPath,
|
|
Post = "Created album path"
|
|
}
|
|
});
|
|
|
|
return albumPath;
|
|
}
|
|
|
|
private class DirEnt
|
|
{
|
|
public string? Pre { get; set;}
|
|
public string? Path { get; set; }
|
|
public string? Post { get; set; }
|
|
}
|
|
|
|
private void GenerateDirectories(List<DirEnt> dirs)
|
|
{
|
|
foreach (var di in dirs)
|
|
{
|
|
_logger.Info(di.Pre);
|
|
Directory.CreateDirectory(di.Path!);
|
|
_logger.Info(di.Post);
|
|
}
|
|
}
|
|
|
|
private void Initialize(DirectoryType dirTypes = DirectoryType.Music)
|
|
{
|
|
switch (dirTypes)
|
|
{
|
|
case DirectoryType.Music:
|
|
_rootSongDirectory = _config!.GetValue<string>("RootMusicPath")!;
|
|
break;
|
|
case DirectoryType.CoverArt:
|
|
_rootSongDirectory = _config!.GetValue<string>("CoverArtPath")!;
|
|
break;
|
|
}
|
|
}
|
|
|
|
private bool IsDirectoryEmpty(string path)
|
|
{
|
|
return !Directory.EnumerateFileSystemEntries(path).Any();
|
|
}
|
|
|
|
private string AlbumDirectory()
|
|
{
|
|
return AlbumDirectory(_song!);
|
|
}
|
|
private string AlbumDirectory(Song song)
|
|
{
|
|
var directory = ArtistDirectory(song);
|
|
var segment = SerializeValue(song.AlbumTitle!);
|
|
directory += $@"{segment}/";
|
|
Console.WriteLine($"Album directory {directory}");
|
|
|
|
return directory;
|
|
}
|
|
private string ArtistDirectory()
|
|
{
|
|
return ArtistDirectory(_song!);
|
|
}
|
|
private string ArtistDirectory(Song song)
|
|
{
|
|
var directory = _rootSongDirectory;
|
|
var segment = SerializeValue(song.Artist!);
|
|
directory += $@"{segment}/";
|
|
Console.WriteLine($"Artist directory {directory}");
|
|
|
|
return directory;
|
|
}
|
|
|
|
private string SerializeValue(string value)
|
|
{
|
|
const int length = 15;
|
|
const string chars = "ABCDEF0123456789";
|
|
var random = new Random();
|
|
var output = new string(Enumerable.Repeat(chars, length).Select(s =>
|
|
s[random.Next(s.Length)]).ToArray());
|
|
|
|
return output;
|
|
}
|
|
#endregion
|
|
}
|