From 168e3649979a5dbdcd6bd1f4060541d14592f052 Mon Sep 17 00:00:00 2001 From: Steven Hildreth Date: Sun, 17 Nov 2019 21:30:54 -0600 Subject: [PATCH] forgotten tasks for #29 --- Roadie.Api.Library/Data/ArtistPartial.cs | 2 +- Roadie.Api.Library/Data/ReleasePartial.cs | 2 +- .../Engines/ArtistLookupEngine.cs | 44 ++++++++---- .../Engines/ReleaseLookupEngine.cs | 71 +++++++++++-------- Roadie.Api.Library/FilePlugins/Audio.cs | 17 ++--- 5 files changed, 78 insertions(+), 58 deletions(-) diff --git a/Roadie.Api.Library/Data/ArtistPartial.cs b/Roadie.Api.Library/Data/ArtistPartial.cs index 189d448..9851bfe 100644 --- a/Roadie.Api.Library/Data/ArtistPartial.cs +++ b/Roadie.Api.Library/Data/ArtistPartial.cs @@ -23,7 +23,7 @@ namespace Roadie.Library.Data } [NotMapped] - public ICollection Images { get; set; } + public IEnumerable Images { get; set; } public bool IsNew => Id < 1; diff --git a/Roadie.Api.Library/Data/ReleasePartial.cs b/Roadie.Api.Library/Data/ReleasePartial.cs index 87a634a..d32ffd4 100644 --- a/Roadie.Api.Library/Data/ReleasePartial.cs +++ b/Roadie.Api.Library/Data/ReleasePartial.cs @@ -102,7 +102,7 @@ namespace Roadie.Library.Data /// /// /// - public string ReleaseFileFolder(string artistFolder) => FolderPathHelper.ReleasePath(artistFolder, SortTitleValue, ReleaseDate.Value); + public string ReleaseFileFolder(string artistFolder, bool createIfNotFound = false) => FolderPathHelper.ReleasePath(artistFolder, SortTitleValue, ReleaseDate.Value, createIfNotFound); public override string ToString() { diff --git a/Roadie.Api.Library/Engines/ArtistLookupEngine.cs b/Roadie.Api.Library/Engines/ArtistLookupEngine.cs index cf49c25..5502110 100644 --- a/Roadie.Api.Library/Engines/ArtistLookupEngine.cs +++ b/Roadie.Api.Library/Engines/ArtistLookupEngine.cs @@ -68,7 +68,7 @@ namespace Roadie.Library.Engines try { var artistGenreTables = artist.Genres; - var ArtistImages = artist.Images; + var artistImages = artist.Images ?? new List(); var now = DateTime.UtcNow; artist.AlternateNames = artist.AlternateNames.AddToDelimitedList(new[] { artist.Name.ToAlphanumericName() }); artist.Genres = null; @@ -122,19 +122,35 @@ namespace Roadie.Library.Engines } } - // TODO #29 save images to folder - - //if (ArtistImages != null && ArtistImages.Any(x => x.Status == Statuses.New)) - //{ - // foreach (var ArtistImage in ArtistImages) - // DbContext.Images.Add(new Library.Imaging.Image(artist.RoadieId) - // { - // Url = ArtistImage.Url, - // Signature = ArtistImage.Signature, - // Bytes = ArtistImage.Bytes - // }); - // inserted = await DbContext.SaveChangesAsync(); - //} + if (artistImages.Any(x => x.Status == Statuses.New)) + { + var artistFolder = artist.ArtistFileFolder(Configuration, true); + var looper = -1; + string releaseImageFilename; + foreach (var artistImage in artistImages) + { + if (artistImage?.Bytes == null || artistImage?.Bytes.Any() == false) + { + continue; + } + artistImage.Bytes = ImageHelper.ConvertToJpegFormat(artistImage.Bytes); + if (looper == -1) + { + releaseImageFilename = Path.Combine(artistFolder, ImageHelper.ArtistImageFilename); + } + else + { + releaseImageFilename = Path.Combine(artistFolder, string.Format(ImageHelper.ArtistSecondaryImageFilename, looper.ToString("00"))); + } + while (File.Exists(releaseImageFilename)) + { + looper++; + releaseImageFilename = Path.Combine(artistFolder, string.Format(ImageHelper.ArtistSecondaryImageFilename, looper.ToString("00"))); + } + File.WriteAllBytes(releaseImageFilename, artistImage.Bytes); + looper++; + } + } sw.Stop(); Logger.LogTrace($"Added New Artist: Elapsed Time [{ sw.ElapsedMilliseconds }], Artist `{ artist }`"); } diff --git a/Roadie.Api.Library/Engines/ReleaseLookupEngine.cs b/Roadie.Api.Library/Engines/ReleaseLookupEngine.cs index f2bf8ef..b5cd930 100644 --- a/Roadie.Api.Library/Engines/ReleaseLookupEngine.cs +++ b/Roadie.Api.Library/Engines/ReleaseLookupEngine.cs @@ -88,7 +88,7 @@ namespace Roadie.Library.Engines release.Genres = null; release.LibraryStatus = LibraryStatus.Incomplete; release.Status = Statuses.New; - var releaseImages = new List(); + var releaseImages = release.Images ?? new List(); if (!release.IsValid) { return new OperationResult @@ -149,29 +149,37 @@ namespace Roadie.Library.Engines } } - // TODO #29 save release images to release folder + if (releaseImages.Any(x => x.Status == Statuses.New)) + { + var artistFolder = release.Artist.ArtistFileFolder(Configuration, true); + var releaseFolder = release.ReleaseFileFolder(artistFolder, true); - //if (releaseImages != null && releaseImages.Any(x => x.Status == Statuses.New)) - //{ - // foreach (var releaseImage in releaseImages) - // { - // DbContext.Images.Add(new Image - // { - // ReleaseId = release.Id, - // Url = releaseImage.Url, - // Signature = releaseImage.Signature, - // Bytes = releaseImage.Bytes - // }); - // } - // try - // { - // await DbContext.SaveChangesAsync(); - // } - // catch (Exception ex) - // { - // Logger.LogError(ex); - // } - //} + var looper = -1; + string releaseImageFilename; + foreach (var releaseImage in releaseImages) + { + if(releaseImage?.Bytes == null || releaseImage?.Bytes.Any() == false) + { + continue; + } + releaseImage.Bytes = ImageHelper.ConvertToJpegFormat(releaseImage.Bytes); + if (looper == -1) + { + releaseImageFilename = Path.Combine(releaseFolder, ImageHelper.ReleaseCoverFilename); + } + else + { + releaseImageFilename = Path.Combine(releaseFolder, string.Format(ImageHelper.ReleaseSecondaryImageFilename, looper.ToString("00"))); + } + while (File.Exists(releaseImageFilename)) + { + looper++; + releaseImageFilename = Path.Combine(releaseFolder,string.Format(ImageHelper.ReleaseSecondaryImageFilename, looper.ToString("00"))); + } + File.WriteAllBytes(releaseImageFilename, releaseImage.Bytes); + looper++; + } + } if (releaseLabels != null && releaseLabels.Any(x => x.Status == Statuses.New)) { @@ -773,11 +781,20 @@ namespace Roadie.Library.Engines Logger.LogTrace($"PerformMetaDataProvidersReleaseSearch: Image Url Processing Complete [{ sw2.ElapsedMilliseconds }]"); } + if (metaData.Images != null && metaData.Images.Any()) + { + foreach(var metadataImage in metaData.Images) + { + releaseImages.Add(new Imaging.Image() + { + Bytes = metadataImage.Data + }); + } + } result.Images = releaseImages.Where(x => x.Bytes != null) .GroupBy(x => x.Signature) .Select(x => x.First()).Take(Configuration.Processing.MaximumReleaseImagesToAdd) .ToList(); - if (releaseLabels.Any()) { var sw2 = Stopwatch.StartNew(); @@ -885,12 +902,6 @@ namespace Roadie.Library.Engines Logger.LogTrace($"PerformMetaDataProvidersReleaseSearch: Release Media Processing Complete [{ sw2.ElapsedMilliseconds }]"); } - if (metaData.Images != null && metaData.Images.Any()) - { - var image = metaData.Images.FirstOrDefault(x => x.Type == AudioMetaDataImageType.FrontCover); - if (image == null) image = metaData.Images.FirstOrDefault(); - } - if (!string.IsNullOrEmpty(artistFolder)) { // If any file exist for cover that over-rides whatever if found in metadata providers. diff --git a/Roadie.Api.Library/FilePlugins/Audio.cs b/Roadie.Api.Library/FilePlugins/Audio.cs index 8006e4e..739d5c5 100644 --- a/Roadie.Api.Library/FilePlugins/Audio.cs +++ b/Roadie.Api.Library/FilePlugins/Audio.cs @@ -91,10 +91,7 @@ namespace Roadie.Library.FilePlugins } 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); - + Logger.LogTrace("Info: FileInfo [{0}], Artist Folder [{1}], Release Folder [{1}], Destination Name [{3}]", fileInfo.FullName, artistFolder, releaseFolder, destinationName); if (doJustInfo) { result.IsSuccess = metaData.IsValid; @@ -109,8 +106,7 @@ namespace Roadie.Library.FilePlugins // See if file folder parent folder (likely file is in release folder) has primary artist image if so then move to artist folder var artistImages = new List(); artistImages.AddRange(ImageHelper.FindImageTypeInDirectory(fileInfo.Directory, ImageType.Artist)); - artistImages.AddRange( - ImageHelper.FindImageTypeInDirectory(fileInfo.Directory.Parent, ImageType.Artist)); + artistImages.AddRange(ImageHelper.FindImageTypeInDirectory(fileInfo.Directory.Parent, ImageType.Artist)); if (artistImages.Any()) { var artistImage = artistImages.First(); @@ -134,10 +130,8 @@ namespace Roadie.Library.FilePlugins // See if any secondary artist images if so then move to artist folder artistImages.Clear(); - artistImages.AddRange( - ImageHelper.FindImageTypeInDirectory(fileInfo.Directory, ImageType.ArtistSecondary)); - artistImages.AddRange( - ImageHelper.FindImageTypeInDirectory(fileInfo.Directory.Parent, ImageType.Artist)); + artistImages.AddRange(ImageHelper.FindImageTypeInDirectory(fileInfo.Directory, ImageType.ArtistSecondary)); + artistImages.AddRange(ImageHelper.FindImageTypeInDirectory(fileInfo.Directory.Parent, ImageType.Artist)); if (artistImages.Any()) { var looper = 0; @@ -198,8 +192,7 @@ namespace Roadie.Library.FilePlugins } // See if folder has secondary release image if so then move to release folder - releaseImages = - ImageHelper.FindImageTypeInDirectory(fileInfo.Directory, ImageType.ReleaseSecondary); + releaseImages = ImageHelper.FindImageTypeInDirectory(fileInfo.Directory, ImageType.ReleaseSecondary); if (releaseImages.Any()) { var looper = 0;