Пример #1
0
 private async Task <FileOperationResult <Image> > TrackImageAction(Guid id, int?width, int?height, EntityTagHeaderValue etag = null)
 {
     try
     {
         var track = this.GetTrack(id);
         if (track == null)
         {
             return(new FileOperationResult <Image>(true, string.Format("Track Not Found [{0}]", id)));
         }
         var imageBytes           = track.Thumbnail;
         var trackThumbnailImages = ImageHelper.FindImageTypeInDirectory(new DirectoryInfo(track.PathToTrackThumbnail(this.Configuration, this.Configuration.LibraryFolder)), Library.Enums.ImageType.Track, SearchOption.TopDirectoryOnly);
         if (trackThumbnailImages.Any())
         {
             imageBytes = File.ReadAllBytes(trackThumbnailImages.First().FullName);
         }
         var image = new data.Image
         {
             Bytes       = track.Thumbnail,
             CreatedDate = track.CreatedDate,
             LastUpdated = track.LastUpdated
         };
         if (track.Thumbnail == null || !track.Thumbnail.Any())
         {
             // If no track image is found then return image for release
             return(await this.ReleaseImage(track.ReleaseMedia.Release.RoadieId, width, height, etag));
         }
         return(GenerateFileOperationResult(id, image, etag));
     }
     catch (Exception ex)
     {
         this.Logger.LogError($"Error fetching Track Thumbnail [{ id }]", ex);
     }
     return(new FileOperationResult <Image>(OperationMessages.ErrorOccured));
 }
Пример #2
0
 private Task <FileOperationResult <Image> > UserImageAction(Guid id, EntityTagHeaderValue etag = null)
 {
     try
     {
         var user = this.GetUser(id);
         if (user == null)
         {
             return(Task.FromResult(new FileOperationResult <Image>(true, string.Format("User Not Found [{0}]", id))));
         }
         var image = new data.Image
         {
             Bytes       = user.Avatar,
             CreatedDate = user.CreatedDate.Value,
             LastUpdated = user.LastUpdated
         };
         if (user.Avatar == null || !user.Avatar.Any())
         {
             image = this.DefaultNotFoundImages.User;
         }
         return(Task.FromResult(GenerateFileOperationResult(id, image, etag, "image/png")));
     }
     catch (Exception ex)
     {
         this.Logger.LogError($"Error fetching User Thumbnail [{ id }]", ex);
     }
     return(Task.FromResult(new FileOperationResult <Image>(OperationMessages.ErrorOccured)));
 }
Пример #3
0
 private Task <FileOperationResult <Image> > PlaylistImageAction(Guid id, EntityTagHeaderValue etag = null)
 {
     try
     {
         var playlist = this.GetPlaylist(id);
         if (playlist == null)
         {
             return(Task.FromResult(new FileOperationResult <Image>(true, string.Format("Playlist Not Found [{0}]", id))));
         }
         var image = new data.Image
         {
             Bytes       = playlist.Thumbnail,
             CreatedDate = playlist.CreatedDate,
             LastUpdated = playlist.LastUpdated
         };
         if (playlist.Thumbnail == null || !playlist.Thumbnail.Any())
         {
             image = this.DefaultNotFoundImages.Playlist;
         }
         return(Task.FromResult(GenerateFileOperationResult(id, image, etag)));
     }
     catch (Exception ex)
     {
         this.Logger.LogError($"Error fetching Playlist Thumbnail [{ id }]", ex);
     }
     return(Task.FromResult(new FileOperationResult <Image>(OperationMessages.ErrorOccured)));
 }
Пример #4
0
 private Task <FileOperationResult <Image> > ReleaseImageAction(Guid id, EntityTagHeaderValue etag = null)
 {
     try
     {
         var release = this.GetRelease(id);
         if (release == null)
         {
             return(Task.FromResult(new FileOperationResult <Image>(true, string.Format("Release Not Found [{0}]", id))));
         }
         byte[] imageBytes    = null;
         string artistFolder  = null;
         string releaseFolder = null;
         try
         {
             // See if cover art file exists in release folder
             artistFolder = release.Artist.ArtistFileFolder(this.Configuration, this.Configuration.LibraryFolder);
             if (!Directory.Exists(artistFolder))
             {
                 this.Logger.LogWarning($"Artist Folder [{ artistFolder }], Not Found For Artist `{ release.Artist.ToString() }`");
             }
             else
             {
                 releaseFolder = release.ReleaseFileFolder(artistFolder);
                 if (!Directory.Exists(releaseFolder))
                 {
                     this.Logger.LogWarning($"Release Folder [{ releaseFolder }], Not Found For Release `{ release.ToString() }`");
                 }
                 else
                 {
                     var releaseCoverFiles = ImageHelper.FindImageTypeInDirectory(new DirectoryInfo(releaseFolder), Library.Enums.ImageType.Release);
                     if (releaseCoverFiles.Any())
                     {
                         imageBytes = File.ReadAllBytes(releaseCoverFiles.First().FullName);
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             this.Logger.LogError(ex, $"Error Reading Release Folder [{ releaseFolder }] Artist Folder [{ artistFolder }] For Artist `{ release.Artist.Id }`");
         }
         imageBytes = imageBytes ?? release.Thumbnail;
         var image = new data.Image
         {
             Bytes       = imageBytes,
             CreatedDate = release.CreatedDate,
             LastUpdated = release.LastUpdated
         };
         if (release.Thumbnail == null || !release.Thumbnail.Any())
         {
             image = this.DefaultNotFoundImages.Release;
         }
         return(Task.FromResult(GenerateFileOperationResult(id, image, etag)));
     }
     catch (Exception ex)
     {
         this.Logger.LogError($"Error fetching Release Thumbnail [{ id }]", ex);
     }
     return(Task.FromResult(new FileOperationResult <Image>(OperationMessages.ErrorOccured)));
 }
Пример #5
0
 private Task <FileOperationResult <Image> > CollectionImageAction(Guid id, EntityTagHeaderValue etag = null)
 {
     try
     {
         var collection = this.GetCollection(id);
         if (collection == null)
         {
             return(Task.FromResult(new FileOperationResult <Image>(true, string.Format("Collection Not Found [{0}]", id))));
         }
         var image = new data.Image
         {
             Bytes       = collection.Thumbnail,
             CreatedDate = collection.CreatedDate,
             LastUpdated = collection.LastUpdated
         };
         if (collection.Thumbnail == null || !collection.Thumbnail.Any())
         {
             image = this.DefaultNotFoundImages.Collection;
         }
         return(Task.FromResult(GenerateFileOperationResult(id, image, etag)));
     }
     catch (Exception ex)
     {
         this.Logger.LogError($"Error fetching Collection Thumbnail [{ id }]", ex);
     }
     return(Task.FromResult(new FileOperationResult <Image>(OperationMessages.ErrorOccured)));
 }
Пример #6
0
        private Task <FileOperationResult <Image> > LabelImageAction(Guid id, EntityTagHeaderValue etag = null)
        {
            try
            {
                var label = GetLabel(id);
                if (label == null)
                {
                    return(Task.FromResult(new FileOperationResult <Image>(true,
                                                                           string.Format("Label Not Found [{0}]", id))));
                }
                var image = new data.Image
                {
                    Bytes       = label.Thumbnail,
                    CreatedDate = label.CreatedDate,
                    LastUpdated = label.LastUpdated
                };
                if (label.Thumbnail == null || !label.Thumbnail.Any())
                {
                    image = DefaultNotFoundImages.Label;
                }
                return(Task.FromResult(GenerateFileOperationResult(id, image, etag)));
            }
            catch (Exception ex)
            {
                Logger.LogError($"Error fetching Label Thumbnail [{id}]", ex);
            }

            return(Task.FromResult(new FileOperationResult <Image>(OperationMessages.ErrorOccured)));
        }
Пример #7
0
        private Task <FileOperationResult <Image> > ArtistImageAction(Guid id, EntityTagHeaderValue etag = null)
        {
            try
            {
                var artist = GetArtist(id);
                if (artist == null)
                {
                    return(Task.FromResult(new FileOperationResult <Image>(true,
                                                                           string.Format("Artist Not Found [{0}]", id))));
                }
                byte[] imageBytes   = null;
                string artistFolder = null;
                try
                {
                    // See if artist images exists in artist folder
                    artistFolder = artist.ArtistFileFolder(Configuration, Configuration.LibraryFolder);
                    if (!Directory.Exists(artistFolder))
                    {
                        Logger.LogWarning($"Artist Folder [{artistFolder}], Not Found For Artist `{artist}`");
                    }
                    else
                    {
                        var artistImages =
                            ImageHelper.FindImageTypeInDirectory(new DirectoryInfo(artistFolder), ImageType.Artist);
                        if (artistImages.Any())
                        {
                            imageBytes = File.ReadAllBytes(artistImages.First().FullName);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, $"Error Reading Folder [{artistFolder}] For Artist [{artist.Id}]");
                }

                imageBytes = imageBytes ?? artist.Thumbnail;
                var image = new data.Image
                {
                    Bytes       = imageBytes,
                    CreatedDate = artist.CreatedDate,
                    LastUpdated = artist.LastUpdated
                };
                if (imageBytes == null || !imageBytes.Any())
                {
                    image = DefaultNotFoundImages.Artist;
                }
                return(Task.FromResult(GenerateFileOperationResult(id, image, etag)));
            }
            catch (Exception ex)
            {
                Logger.LogError($"Error fetching Artist Thumbnail [{id}]", ex);
            }

            return(Task.FromResult(new FileOperationResult <Image>(OperationMessages.ErrorOccured)));
        }
Пример #8
0
        private Task <FileOperationResult <Image> > ArtistSecondaryImageAction(Guid id, int imageId,
                                                                               EntityTagHeaderValue etag = null)
        {
            try
            {
                var artist = GetArtist(id);
                if (artist == null)
                {
                    return(Task.FromResult(new FileOperationResult <Image>(true,
                                                                           string.Format("Release Not Found [{0}]", id))));
                }
                byte[] imageBytes   = null;
                string artistFolder = null;
                try
                {
                    // See if cover art file exists in release folder
                    artistFolder = artist.ArtistFileFolder(Configuration, Configuration.LibraryFolder);
                    if (!Directory.Exists(artistFolder))
                    {
                        Logger.LogWarning($"Artist Folder [{artistFolder}], Not Found For Artist `{artist}`");
                    }
                    else
                    {
                        var artistSecondaryImages = ImageHelper
                                                    .FindImageTypeInDirectory(new DirectoryInfo(artistFolder), ImageType.ArtistSecondary)
                                                    .ToArray();
                        if (artistSecondaryImages.Length >= imageId && artistSecondaryImages[imageId] != null)
                        {
                            imageBytes = File.ReadAllBytes(artistSecondaryImages[imageId].FullName);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, $"Error Reading Artist Folder [{artistFolder}] For Artist `{artist}`");
                }

                var image = new data.Image
                {
                    Bytes       = imageBytes,
                    CreatedDate = artist.CreatedDate,
                    LastUpdated = artist.LastUpdated
                };
                return(Task.FromResult(GenerateFileOperationResult(id, image, etag)));
            }
            catch (Exception ex)
            {
                Logger.LogError($"Error fetching Release Thumbnail [{id}]", ex);
            }

            return(Task.FromResult(new FileOperationResult <Image>(OperationMessages.ErrorOccured)));
        }
Пример #9
0
        private FileOperationResult <Image> GenerateFileOperationResult(Guid id, data.Image image, EntityTagHeaderValue etag = null, string contentType = "image/jpeg")
        {
            var imageEtag = EtagHelper.GenerateETag(this.HttpEncoder, image.Bytes);

            if (EtagHelper.CompareETag(this.HttpEncoder, etag, imageEtag))
            {
                return(new FileOperationResult <Image>(OperationMessages.NotModified));
            }
            if (!image?.Bytes?.Any() ?? false)
            {
                return(new FileOperationResult <Image>(string.Format("ImageById Not Set [{0}]", id)));
            }
            return(new FileOperationResult <Image>(image?.Bytes?.Any() ?? false ? OperationMessages.OkMessage : OperationMessages.NoImageDataFound)
            {
                IsSuccess = true,
                Data = image.Adapt <Image>(),
                ContentType = contentType,
                LastModified = (image.LastUpdated ?? image.CreatedDate),
                ETag = imageEtag
            });
        }