diff --git a/Roadie.Api.Library/FilePlugins/Audio.cs b/Roadie.Api.Library/FilePlugins/Audio.cs index 6403566..c152bb1 100644 --- a/Roadie.Api.Library/FilePlugins/Audio.cs +++ b/Roadie.Api.Library/FilePlugins/Audio.cs @@ -90,7 +90,7 @@ namespace Roadie.Library.FilePlugins return new OperationResult("Unable To Find Release Folder"); } - destinationName = FolderPathHelper.TrackFullPath(Configuration, metaData, Configuration.LibraryFolder, artistFolder, releaseFolder); + destinationName = FolderPathHelper.TrackFullPath(Configuration, metaData, artistFolder, releaseFolder); Logger.LogTrace( "Info: FileInfo [{0}], Artist Folder [{1}], Release Folder [{1}], Destination Name [{3}]", fileInfo.FullName, artistFolder, releaseFolder, destinationName); diff --git a/Roadie.Api.Library/Utility/FolderPathHelper.cs b/Roadie.Api.Library/Utility/FolderPathHelper.cs index f6686ae..3735fc1 100644 --- a/Roadie.Api.Library/Utility/FolderPathHelper.cs +++ b/Roadie.Api.Library/Utility/FolderPathHelper.cs @@ -30,12 +30,15 @@ namespace Roadie.Library.Utility public static void DeleteEmptyDirs(string dir, bool deleteDirIfEmpty = true) { if (string.IsNullOrEmpty(dir)) + { throw new ArgumentException("Starting directory is a null reference or an empty string", "dir"); + } try { foreach (var d in Directory.EnumerateDirectories(dir)) DeleteEmptyDirs(d); var entries = Directory.EnumerateFileSystemEntries(dir); if (!entries.Any() && deleteDirIfEmpty) + { try { Directory.Delete(dir); @@ -46,6 +49,7 @@ namespace Roadie.Library.Utility catch (DirectoryNotFoundException) { } + } } catch (UnauthorizedAccessException) { @@ -101,12 +105,8 @@ namespace Roadie.Library.Utility /// For given artist delete any empty folders /// /// Populated Artist database record - /// Optional Root folder defaults to Library Folder from Settings - /// - public static bool DeleteEmptyFoldersForArtist(IRoadieSettings configuration, Artist artist, - string destinationFolder = null) + public static bool DeleteEmptyFoldersForArtist(IRoadieSettings configuration, Artist artist) { - destinationFolder = destinationFolder ?? configuration.LibraryFolder; SimpleContract.Requires(artist != null, "Invalid Artist"); return DeleteEmptyFolders(new DirectoryInfo(artist.ArtistFileFolder(configuration))); } @@ -134,7 +134,10 @@ namespace Roadie.Library.Utility public static string PathForTrackThumbnail(IRoadieSettings configuration, Track track, string destinationFolder = null) { destinationFolder = destinationFolder ?? configuration.LibraryFolder; - if (string.IsNullOrEmpty(track.FilePath) || string.IsNullOrEmpty(track.FileName)) return null; + if (string.IsNullOrEmpty(track.FilePath) || string.IsNullOrEmpty(track.FileName)) + { + return null; + } var fileName = Path.ChangeExtension(track.FileName, ".jpg"); var directoryInfo = new DirectoryInfo(Path.Combine(destinationFolder, track.FilePath, fileName)); return directoryInfo.FullName; @@ -152,9 +155,7 @@ namespace Roadie.Library.Utility SimpleContract.Requires(!string.IsNullOrEmpty(releaseTitle), "Invalid Release Title"); SimpleContract.Requires(releaseDate != DateTime.MinValue, "Invalid Release Date"); - var directoryInfo = new DirectoryInfo(Path.Combine(artistFolder, - string.Format("{1}{0}", releaseTitle.ToTitleCase(false).ToFolderNameFriendly(), - string.Format("[{0}] ", releaseDate.ToString("yyyy"))))); + var directoryInfo = new DirectoryInfo(Path.Combine(artistFolder, string.Format("{1}{0}", releaseTitle.ToTitleCase(false).ToFolderNameFriendly(), string.Format("[{0}] ", releaseDate.ToString("yyyy"))))); return directoryInfo.FullName; } @@ -211,14 +212,12 @@ namespace Roadie.Library.Utility /// Returns the Full path (FQDN) for given Track details /// /// Populated Track MetaData - /// Optional Root folder defaults to Library Folder from Settings /// Optional ArtistFolder default is to get from MetaData artist - public static string TrackFullPath(IRoadieSettings configuration, AudioMetaData metaData, - string destinationFolder = null, string artistFolder = null, string releaseFolder = null) + public static string TrackFullPath(IRoadieSettings configuration, AudioMetaData metaData, string artistFolder = null, string releaseFolder = null) { return TrackFullPath(configuration, metaData.Artist, metaData.Release, SafeParser.ToDateTime(metaData.Year).Value, - metaData.Title, metaData.TrackNumber ?? 0, destinationFolder, metaData.Disc ?? 0, + metaData.Title, metaData.TrackNumber ?? 0, metaData.Disc ?? 0, metaData.TotalTrackNumbers ?? 0, artistFolder: artistFolder, releaseFolder: releaseFolder); @@ -232,12 +231,7 @@ namespace Roadie.Library.Utility /// Track /// Optional Root folder defaults to Library Folder from Settings /// - public static string TrackFullPath(IRoadieSettings configuration, Artist artist, Release release, Track track, - string destinationFolder = null) - { - return TrackFullPath(configuration, artist.SortNameValue, release.Title, release.ReleaseDate.Value, - track.Title, track.TrackNumber, destinationFolder); - } + public static string TrackFullPath(IRoadieSettings configuration, Artist artist, Release release, Track track) => TrackFullPath(configuration, artist.SortNameValue, release.Title, release.ReleaseDate.Value, track.Title, track.TrackNumber); /// /// Return the full path (FQDN) for given Track details @@ -251,15 +245,12 @@ namespace Roadie.Library.Utility /// Optional Total Tracks defaults to TrackNumber /// Optional File Extension defaults to mp3 public static string TrackFullPath(IRoadieSettings configuration, string artistSortName, string releaseTitle, - DateTime releaseDate, string trackTitle, short trackNumber, string destinationFolder = null, - int? discNumber = null, int? totalTrackNumber = null, string fileExtension = "mp3", + DateTime releaseDate, string trackTitle, short trackNumber, int? discNumber = null, int? totalTrackNumber = null, string fileExtension = "mp3", string artistFolder = null, string releaseFolder = null) { - destinationFolder = destinationFolder ?? configuration.LibraryFolder; artistFolder = artistFolder ?? ArtistPath(configuration, artistSortName); releaseFolder = releaseFolder ?? ReleasePath(artistFolder, releaseTitle, releaseDate); - var trackFileName = TrackFileName(configuration, trackTitle, trackNumber, discNumber, totalTrackNumber, - fileExtension); + var trackFileName = TrackFileName(configuration, trackTitle, trackNumber, discNumber, totalTrackNumber, fileExtension); var result = Path.Combine(artistFolder, releaseFolder, trackFileName); var resultInfo = new DirectoryInfo(result); @@ -290,11 +281,9 @@ namespace Roadie.Library.Utility /// Artist For release /// Release /// Track - /// Optional Root folder defaults to Library Folder from Settings - public static string TrackPath(IRoadieSettings configuration, Artist artist, Release release, Track track, - string destinationFolder = null) + public static string TrackPath(IRoadieSettings configuration, Artist artist, Release release, Track track) { - var fileInfo = new FileInfo(TrackFullPath(configuration, artist.SortNameValue, release.Title, release.ReleaseDate.Value, track.Title, track.TrackNumber, destinationFolder)); + var fileInfo = new FileInfo(TrackFullPath(configuration, artist.SortNameValue, release.Title, release.ReleaseDate.Value, track.Title, track.TrackNumber)); return fileInfo.Directory.Name; } @@ -308,13 +297,10 @@ namespace Roadie.Library.Utility /// Optional Root folder defaults to Library Folder from Settings /// Optional disc number defaults to 0 /// Optional Total Tracks defaults to TrackNumber - /// Optional File Extension defaults to mp3 public static string TrackPath(IRoadieSettings configuration, string artistSortName, string releaseTitle, - DateTime releaseDate, string trackTitle, short trackNumber, string destinationFolder = null, - int? discNumber = null, int? totalTrackNumber = null, string fileExtension = "mp3") + DateTime releaseDate, string trackTitle, short trackNumber, int? discNumber = null, int? totalTrackNumber = null) { - var fileInfo = new FileInfo(TrackFullPath(configuration, artistSortName, releaseTitle, releaseDate, - trackTitle, trackNumber, destinationFolder, discNumber, totalTrackNumber)); + var fileInfo = new FileInfo(TrackFullPath(configuration, artistSortName, releaseTitle, releaseDate, trackTitle, trackNumber, discNumber, totalTrackNumber)); return fileInfo.Directory.Name; } } diff --git a/Roadie.Api.Services/ReleaseService.cs b/Roadie.Api.Services/ReleaseService.cs index df4b028..d6cf522 100644 --- a/Roadie.Api.Services/ReleaseService.cs +++ b/Roadie.Api.Services/ReleaseService.cs @@ -790,7 +790,7 @@ namespace Roadie.Api.Services } /// - /// Merge one release into another one + /// Merge one release into another one /// /// The release to be merged /// The release to merge into @@ -905,12 +905,13 @@ namespace Roadie.Api.Services // Move tracks for releaseToMergeInto into correct folders if (mergedTracksToMove.Any()) + { foreach (var track in mergedTracksToMove) { var oldTrackPath = track.PathToTrack(Configuration); - var newTrackPath = FolderPathHelper.TrackFullPath(Configuration, releaseToMerge.Artist, releaseToMerge, track); + var newTrackPath = FolderPathHelper.TrackFullPath(Configuration, releaseToMergeInto.Artist, releaseToMergeInto, track); var trackFile = new FileInfo(oldTrackPath); - if (!newTrackPath.ToLower().Equals(oldTrackPath.ToLower())) + if(!newTrackPath.Equals(oldTrackPath, StringComparison.OrdinalIgnoreCase)) { var audioMetaData = await AudioMetaDataHelper.GetInfo(trackFile); track.FilePath = FolderPathHelper.TrackPath(Configuration, releaseToMergeInto.Artist, releaseToMergeInto, track); @@ -919,6 +920,7 @@ namespace Roadie.Api.Services File.Move(oldTrackPath, newTrackPath); } } + } // Cleanup folders Services.FileDirectoryProcessorService.DeleteEmptyFolders(new DirectoryInfo(releaseToMergeIntoArtistFolder), Logger);