2019-11-04 03:19:04 +00:00
using Microsoft.EntityFrameworkCore ;
2018-11-11 01:11:58 +00:00
using Microsoft.Extensions.Logging ;
using Microsoft.Net.Http.Headers ;
using Roadie.Library ;
using Roadie.Library.Caching ;
using Roadie.Library.Configuration ;
2019-11-17 14:10:17 +00:00
using Roadie.Library.Data.Context ;
2018-11-11 01:11:58 +00:00
using Roadie.Library.Encoding ;
2019-06-30 22:14:36 +00:00
using Roadie.Library.Enums ;
2018-11-11 20:10:10 +00:00
using Roadie.Library.Identity ;
2018-11-11 14:46:09 +00:00
using Roadie.Library.Imaging ;
2018-11-11 01:11:58 +00:00
using Roadie.Library.Models ;
2018-11-11 21:13:19 +00:00
using Roadie.Library.SearchEngines.Imaging ;
2018-11-11 01:11:58 +00:00
using Roadie.Library.Utility ;
using System ;
2018-11-11 21:13:19 +00:00
using System.Collections.Generic ;
2018-11-11 01:11:58 +00:00
using System.Diagnostics ;
2018-11-22 17:31:59 +00:00
using System.IO ;
2018-11-11 01:11:58 +00:00
using System.Linq ;
using System.Threading.Tasks ;
using data = Roadie . Library . Data ;
namespace Roadie.Api.Services
{
public class ImageService : ServiceBase , IImageService
{
2019-08-22 20:33:07 +00:00
public string Referrer { get ; set ; }
public string RequestIp { get ; set ; }
2019-01-08 22:40:26 +00:00
private IDefaultNotFoundImages DefaultNotFoundImages { get ; }
2019-06-30 22:14:36 +00:00
2019-07-07 03:16:33 +00:00
private IImageSearchManager ImageSearchManager { get ; }
2018-12-21 22:59:33 +00:00
2018-11-11 01:11:58 +00:00
public ImageService ( IRoadieSettings configuration ,
2019-07-07 03:16:33 +00:00
IHttpEncoder httpEncoder ,
2019-06-30 22:14:36 +00:00
IHttpContext httpContext ,
2019-11-17 14:10:17 +00:00
IRoadieDbContext context ,
2019-06-30 22:14:36 +00:00
ICacheManager cacheManager ,
ILogger < ImageService > logger ,
2019-07-07 03:16:33 +00:00
IDefaultNotFoundImages defaultNotFoundImages ,
IImageSearchManager imageSearchManager )
2018-11-11 01:11:58 +00:00
: base ( configuration , httpEncoder , context , cacheManager , logger , httpContext )
{
2019-06-30 22:14:36 +00:00
DefaultNotFoundImages = defaultNotFoundImages ;
2019-07-07 03:16:33 +00:00
ImageSearchManager = imageSearchManager ;
2018-11-11 01:11:58 +00:00
}
2019-11-17 14:10:17 +00:00
public ImageService ( IRoadieSettings configuration , IRoadieDbContext dbContext , ICacheManager cacheManager ,
2019-09-05 02:04:20 +00:00
ILogger logger , DefaultNotFoundImages defaultNotFoundImages )
: base ( configuration , null , dbContext , cacheManager , logger , null )
{
DefaultNotFoundImages = defaultNotFoundImages ;
}
2019-11-04 03:19:04 +00:00
public async Task < FileOperationResult < IImage > > ArtistImage ( Guid id , int? width , int? height , EntityTagHeaderValue etag = null )
2018-11-11 01:11:58 +00:00
{
2019-06-30 22:14:36 +00:00
return await GetImageFileOperation ( "ArtistImage" ,
data . Artist . CacheRegionUrn ( id ) ,
id ,
width ,
height ,
async ( ) = > { return await ArtistImageAction ( id , etag ) ; } ,
etag ) ;
2018-11-11 01:11:58 +00:00
}
2019-11-04 03:19:04 +00:00
public async Task < FileOperationResult < IImage > > ArtistSecondaryImage ( Guid id , int imageId , int? width , int? height , EntityTagHeaderValue etag = null )
2019-02-10 00:19:26 +00:00
{
2019-11-04 03:19:04 +00:00
return await GetImageFileOperation ( $"ArtistSecondaryThumbnail.{imageId}" ,
data . Artist . CacheRegionUrn ( id ) ,
2019-06-30 22:14:36 +00:00
id ,
width ,
height ,
async ( ) = > { return await ArtistSecondaryImageAction ( id , imageId , etag ) ; } ,
etag ) ;
2019-02-10 00:19:26 +00:00
}
2019-11-04 03:19:04 +00:00
public async Task < FileOperationResult < IImage > > ById ( Guid id , int? width , int? height , EntityTagHeaderValue etag = null )
2018-11-16 03:37:00 +00:00
{
2019-06-30 22:14:36 +00:00
return await GetImageFileOperation ( "ImageById" ,
2019-11-04 03:19:04 +00:00
Library . Imaging . Image . CacheRegionUrn ( id ) ,
2019-06-30 22:14:36 +00:00
id ,
width ,
height ,
async ( ) = > { return await ImageByIdAction ( id , etag ) ; } ,
etag ) ;
2018-11-16 03:37:00 +00:00
}
2019-11-04 03:19:04 +00:00
public async Task < FileOperationResult < IImage > > CollectionImage ( Guid id , int? width , int? height , EntityTagHeaderValue etag = null )
2018-11-11 16:20:33 +00:00
{
2019-06-30 22:14:36 +00:00
return await GetImageFileOperation ( "CollectionThumbnail" ,
data . Collection . CacheRegionUrn ( id ) ,
id ,
width ,
height ,
async ( ) = > { return await CollectionImageAction ( id , etag ) ; } ,
etag ) ;
2018-11-11 16:20:33 +00:00
}
2019-11-04 03:19:04 +00:00
public async Task < FileOperationResult < IImage > > GenreImage ( Guid id , int? width , int? height , EntityTagHeaderValue etag = null )
2018-11-11 20:10:10 +00:00
{
2019-08-22 20:33:07 +00:00
return await GetImageFileOperation ( "GenreThumbnail" ,
2019-11-04 03:19:04 +00:00
data . Genre . CacheRegionUrn ( id ) ,
2019-06-30 22:14:36 +00:00
id ,
width ,
height ,
2019-09-05 02:04:20 +00:00
async ( ) = > await GenreImageAction ( id , etag ) ,
2019-06-30 22:14:36 +00:00
etag ) ;
2018-11-11 20:10:10 +00:00
}
2019-11-04 03:19:04 +00:00
public async Task < FileOperationResult < IImage > > LabelImage ( Guid id , int? width , int? height , EntityTagHeaderValue etag = null )
2019-08-02 20:59:24 +00:00
{
2019-08-22 20:33:07 +00:00
return await GetImageFileOperation ( "LabelThumbnail" ,
2019-08-02 20:59:24 +00:00
data . Label . CacheRegionUrn ( id ) ,
id ,
width ,
height ,
2019-09-05 02:04:20 +00:00
async ( ) = > await LabelImageAction ( id , etag ) ,
2019-08-02 20:59:24 +00:00
etag ) ;
}
2019-11-04 03:19:04 +00:00
public async Task < FileOperationResult < IImage > > PlaylistImage ( Guid id , int? width , int? height , EntityTagHeaderValue etag = null )
2018-11-11 20:10:10 +00:00
{
2019-06-30 22:14:36 +00:00
return await GetImageFileOperation ( "PlaylistThumbnail" ,
data . Playlist . CacheRegionUrn ( id ) ,
id ,
width ,
height ,
2019-09-05 02:04:20 +00:00
async ( ) = > await PlaylistImageAction ( id , etag ) ,
2019-06-30 22:14:36 +00:00
etag ) ;
2018-11-11 20:10:10 +00:00
}
2019-11-04 03:19:04 +00:00
public async Task < FileOperationResult < IImage > > ReleaseImage ( Guid id , int? width , int? height , EntityTagHeaderValue etag = null )
2018-11-11 20:10:10 +00:00
{
2019-06-30 22:14:36 +00:00
return await GetImageFileOperation ( "ReleaseThumbnail" ,
data . Release . CacheRegionUrn ( id ) ,
id ,
width ,
height ,
2019-09-05 02:04:20 +00:00
async ( ) = > await ReleaseImageAction ( id , etag ) ,
2019-06-30 22:14:36 +00:00
etag ) ;
2018-11-11 20:10:10 +00:00
}
2019-11-04 03:19:04 +00:00
public async Task < FileOperationResult < IImage > > ReleaseSecondaryImage ( Guid id , int imageId , int? width , int? height , EntityTagHeaderValue etag = null )
2019-02-10 00:19:26 +00:00
{
2019-06-30 22:14:36 +00:00
return await GetImageFileOperation ( $"ReleaseSecondaryThumbnail-{imageId}" ,
data . Release . CacheRegionUrn ( id ) ,
id ,
width ,
height ,
async ( ) = > { return await ReleaseSecondaryImageAction ( id , imageId , etag ) ; } ,
etag ) ;
2019-02-10 00:19:26 +00:00
}
2018-12-21 22:59:33 +00:00
public async Task < OperationResult < IEnumerable < ImageSearchResult > > > Search ( string query , int resultsCount = 10 )
{
2019-07-07 03:16:33 +00:00
var sw = new Stopwatch ( ) ;
sw . Start ( ) ;
var errors = new List < Exception > ( ) ;
IEnumerable < ImageSearchResult > searchResults = null ;
try
2018-12-21 22:59:33 +00:00
{
2019-07-07 03:16:33 +00:00
searchResults = await ImageSearchManager . ImageSearch ( query ) ;
}
catch ( Exception ex )
{
Logger . LogError ( ex ) ;
errors . Add ( ex ) ;
2018-12-21 22:59:33 +00:00
}
2019-06-30 22:14:36 +00:00
2018-12-21 22:59:33 +00:00
sw . Stop ( ) ;
return new OperationResult < IEnumerable < ImageSearchResult > >
{
2019-07-07 03:16:33 +00:00
Data = searchResults ,
IsSuccess = ! errors . Any ( ) ,
OperationTime = sw . ElapsedMilliseconds ,
Errors = errors
2018-12-21 22:59:33 +00:00
} ;
}
2019-11-04 03:19:04 +00:00
public async Task < FileOperationResult < IImage > > TrackImage ( Guid id , int? width , int? height , EntityTagHeaderValue etag = null )
2019-01-08 22:40:26 +00:00
{
2019-06-30 22:14:36 +00:00
return await GetImageFileOperation ( "TrackThumbnail" ,
data . Track . CacheRegionUrn ( id ) ,
id ,
width ,
height ,
2019-09-05 02:04:20 +00:00
async ( ) = > await TrackImageAction ( id , width , height , etag ) ,
2019-06-30 22:14:36 +00:00
etag ) ;
2019-01-08 22:40:26 +00:00
}
2019-11-04 03:19:04 +00:00
public async Task < FileOperationResult < IImage > > UserImage ( Guid id , int? width , int? height , EntityTagHeaderValue etag = null )
2019-01-08 22:40:26 +00:00
{
2019-06-30 22:14:36 +00:00
return await GetImageFileOperation ( "UserById" ,
ApplicationUser . CacheRegionUrn ( id ) ,
id ,
width ,
height ,
2019-09-05 02:04:20 +00:00
async ( ) = > await UserImageAction ( id , etag ) ,
2019-06-30 22:14:36 +00:00
etag ) ;
2019-01-08 22:40:26 +00:00
}
2018-12-21 22:59:33 +00:00
2019-07-25 15:43:11 +00:00
/// <summary>
/// Get image for an artist, see if the artist has an image in their folder and use that else use Artist.Thumbnail, is also not found use Artist DefaultNotFound image.
/// </summary>
2019-11-04 03:19:04 +00:00
private Task < FileOperationResult < IImage > > ArtistImageAction ( Guid id , EntityTagHeaderValue etag = null )
2018-11-11 01:11:58 +00:00
{
try
{
2019-06-30 22:14:36 +00:00
var artist = GetArtist ( id ) ;
2018-11-11 17:27:13 +00:00
if ( artist = = null )
2019-07-25 15:43:11 +00:00
{
2019-11-04 03:19:04 +00:00
return Task . FromResult ( new FileOperationResult < IImage > ( true , string . Format ( "Artist Not Found [{0}]" , id ) ) ) ;
2019-07-25 15:43:11 +00:00
}
2018-11-22 17:31:59 +00:00
byte [ ] imageBytes = null ;
string artistFolder = null ;
try
{
2018-12-26 21:18:51 +00:00
// See if artist images exists in artist folder
2019-07-07 03:16:33 +00:00
artistFolder = artist . ArtistFileFolder ( Configuration ) ;
2018-12-26 21:18:51 +00:00
if ( ! Directory . Exists ( artistFolder ) )
2018-11-22 17:31:59 +00:00
{
2019-08-03 22:59:20 +00:00
Logger . LogTrace ( $"Artist Folder [{artistFolder}], Not Found For Artist `{artist}`" ) ;
2018-12-26 21:18:51 +00:00
}
else
{
2019-07-25 15:43:11 +00:00
var artistImages = ImageHelper . FindImageTypeInDirectory ( new DirectoryInfo ( artistFolder ) , ImageType . Artist ) ;
if ( artistImages . Any ( ) )
{
imageBytes = File . ReadAllBytes ( artistImages . First ( ) . FullName ) ;
}
2018-11-22 17:31:59 +00:00
}
}
catch ( Exception ex )
{
2019-06-30 22:14:36 +00:00
Logger . LogError ( ex , $"Error Reading Folder [{artistFolder}] For Artist [{artist.Id}]" ) ;
2018-11-22 17:31:59 +00:00
}
2019-06-30 22:14:36 +00:00
2019-11-04 03:19:04 +00:00
IImage image = new Library . Imaging . Image ( id )
2018-11-11 16:20:33 +00:00
{
2018-11-22 17:31:59 +00:00
Bytes = imageBytes ,
2018-11-11 16:20:33 +00:00
} ;
2019-07-25 15:43:11 +00:00
if ( imageBytes = = null | | ! imageBytes . Any ( ) )
{
image = DefaultNotFoundImages . Artist ;
}
2018-12-24 19:40:49 +00:00
return Task . FromResult ( GenerateFileOperationResult ( id , image , etag ) ) ;
2018-11-11 16:20:33 +00:00
}
catch ( Exception ex )
{
2019-06-30 22:14:36 +00:00
Logger . LogError ( $"Error fetching Artist Thumbnail [{id}]" , ex ) ;
2018-11-11 16:20:33 +00:00
}
2019-06-30 22:14:36 +00:00
2019-11-04 03:19:04 +00:00
return Task . FromResult ( new FileOperationResult < IImage > ( OperationMessages . ErrorOccured ) ) ;
2018-11-11 16:20:33 +00:00
}
2018-11-11 01:11:58 +00:00
2019-11-04 03:19:04 +00:00
private Task < FileOperationResult < IImage > > ArtistSecondaryImageAction ( Guid id , int imageId , EntityTagHeaderValue etag = null )
2019-05-29 22:25:40 +00:00
{
try
{
2019-06-30 22:14:36 +00:00
var artist = GetArtist ( id ) ;
2019-05-29 22:25:40 +00:00
if ( artist = = null )
2019-11-04 03:19:04 +00:00
{
return Task . FromResult ( new FileOperationResult < IImage > ( true , string . Format ( "Release Not Found [{0}]" , id ) ) ) ;
}
2019-05-29 22:25:40 +00:00
byte [ ] imageBytes = null ;
string artistFolder = null ;
try
{
// See if cover art file exists in release folder
2019-07-07 03:16:33 +00:00
artistFolder = artist . ArtistFileFolder ( Configuration ) ;
2019-05-29 22:25:40 +00:00
if ( ! Directory . Exists ( artistFolder ) )
{
2019-08-03 22:59:20 +00:00
Logger . LogTrace ( $"Artist Folder [{artistFolder}], Not Found For Artist `{artist}`" ) ;
2019-05-29 22:25:40 +00:00
}
else
{
2019-06-30 22:14:36 +00:00
var artistSecondaryImages = ImageHelper
. FindImageTypeInDirectory ( new DirectoryInfo ( artistFolder ) , ImageType . ArtistSecondary )
. ToArray ( ) ;
2019-05-29 22:25:40 +00:00
if ( artistSecondaryImages . Length > = imageId & & artistSecondaryImages [ imageId ] ! = null )
imageBytes = File . ReadAllBytes ( artistSecondaryImages [ imageId ] . FullName ) ;
}
}
catch ( Exception ex )
{
2019-06-30 22:14:36 +00:00
Logger . LogError ( ex , $"Error Reading Artist Folder [{artistFolder}] For Artist `{artist}`" ) ;
2019-05-29 22:25:40 +00:00
}
2019-06-30 22:14:36 +00:00
2019-11-04 03:19:04 +00:00
var image = new Library . Imaging . Image ( id )
2019-05-29 22:25:40 +00:00
{
Bytes = imageBytes ,
2019-11-04 03:19:04 +00:00
CreatedDate = artist . CreatedDate
2019-05-29 22:25:40 +00:00
} ;
return Task . FromResult ( GenerateFileOperationResult ( id , image , etag ) ) ;
}
catch ( Exception ex )
{
2019-06-30 22:14:36 +00:00
Logger . LogError ( $"Error fetching Release Thumbnail [{id}]" , ex ) ;
2019-05-29 22:25:40 +00:00
}
2019-06-30 22:14:36 +00:00
2019-11-04 03:19:04 +00:00
return Task . FromResult ( new FileOperationResult < IImage > ( OperationMessages . ErrorOccured ) ) ;
2019-05-29 22:25:40 +00:00
}
2019-11-04 03:19:04 +00:00
private Task < FileOperationResult < IImage > > CollectionImageAction ( Guid id , EntityTagHeaderValue etag = null )
2018-11-11 16:20:33 +00:00
{
try
{
2019-06-30 22:14:36 +00:00
var collection = GetCollection ( id ) ;
2018-11-11 20:10:10 +00:00
if ( collection = = null )
2019-07-31 16:44:25 +00:00
{
2019-11-04 03:19:04 +00:00
return Task . FromResult ( new FileOperationResult < IImage > ( true , string . Format ( "Collection Not Found [{0}]" , id ) ) ) ;
2019-07-31 16:44:25 +00:00
}
2019-11-04 03:19:04 +00:00
IImage image = new Library . Imaging . Image ( id )
2018-11-11 17:27:13 +00:00
{
2019-11-04 03:19:04 +00:00
CreatedDate = collection . CreatedDate
2018-11-11 17:27:13 +00:00
} ;
2019-07-31 16:44:25 +00:00
var collectionImageFilename = collection . PathToImage ( Configuration ) ;
try
{
if ( File . Exists ( collectionImageFilename ) )
{
image . Bytes = File . ReadAllBytes ( collectionImageFilename ) ;
}
}
catch ( Exception ex )
{
Logger . LogError ( ex , $"Error Reading Image File [{collectionImageFilename}]" ) ;
}
2019-11-04 03:19:04 +00:00
if ( image . Bytes = = null | | ! image . Bytes . Any ( ) )
2019-07-31 16:44:25 +00:00
{
2019-06-30 22:14:36 +00:00
image = DefaultNotFoundImages . Collection ;
2019-07-31 16:44:25 +00:00
}
2018-12-24 19:40:49 +00:00
return Task . FromResult ( GenerateFileOperationResult ( id , image , etag ) ) ;
2018-11-11 01:11:58 +00:00
}
catch ( Exception ex )
{
2019-06-30 22:14:36 +00:00
Logger . LogError ( $"Error fetching Collection Thumbnail [{id}]" , ex ) ;
2018-11-11 01:11:58 +00:00
}
2019-06-30 22:14:36 +00:00
2019-11-04 03:19:04 +00:00
return Task . FromResult ( new FileOperationResult < IImage > ( OperationMessages . ErrorOccured ) ) ;
2018-11-11 01:11:58 +00:00
}
2018-11-11 16:20:33 +00:00
2019-11-04 03:19:04 +00:00
private FileOperationResult < IImage > GenerateFileOperationResult ( Guid id , IImage image , EntityTagHeaderValue etag = null , string contentType = "image/jpeg" )
2018-11-11 16:20:33 +00:00
{
2019-06-30 22:14:36 +00:00
var imageEtag = EtagHelper . GenerateETag ( HttpEncoder , image . Bytes ) ;
if ( EtagHelper . CompareETag ( HttpEncoder , etag , imageEtag ) )
2019-09-05 02:04:20 +00:00
{
2019-11-04 03:19:04 +00:00
return new FileOperationResult < IImage > ( OperationMessages . NotModified ) ;
2019-09-05 02:04:20 +00:00
}
if ( image ? . Bytes ? . Any ( ) ! = true )
{
2019-11-04 03:19:04 +00:00
return new FileOperationResult < IImage > ( string . Format ( "ImageById Not Set [{0}]" , id ) ) ;
2019-09-05 02:04:20 +00:00
}
2019-11-04 03:19:04 +00:00
return new FileOperationResult < IImage > ( image ? . Bytes ? . Any ( ) ? ? false
2019-06-30 22:14:36 +00:00
? OperationMessages . OkMessage
: OperationMessages . NoImageDataFound )
2018-11-11 16:20:33 +00:00
{
IsSuccess = true ,
2019-11-04 03:19:04 +00:00
Data = image ,
2018-11-21 18:19:38 +00:00
ContentType = contentType ,
2019-11-04 03:19:04 +00:00
LastModified = image . CreatedDate ,
2018-11-11 16:20:33 +00:00
ETag = imageEtag
} ;
}
2019-11-04 03:19:04 +00:00
private Task < FileOperationResult < IImage > > GenreImageAction ( Guid id , EntityTagHeaderValue etag = null )
2019-08-22 20:33:07 +00:00
{
try
{
var genre = GetGenre ( id ) ;
if ( genre = = null )
{
2019-11-04 03:19:04 +00:00
return Task . FromResult ( new FileOperationResult < IImage > ( true , string . Format ( "Genre Not Found [{0}]" , id ) ) ) ;
2019-08-22 20:33:07 +00:00
}
2019-11-04 03:19:04 +00:00
IImage image = new Library . Imaging . Image ( id )
2019-08-22 20:33:07 +00:00
{
2019-11-04 03:19:04 +00:00
CreatedDate = genre . CreatedDate
2019-08-22 20:33:07 +00:00
} ;
var genreImageFilename = genre . PathToImage ( Configuration ) ;
try
{
if ( File . Exists ( genreImageFilename ) )
{
image . Bytes = File . ReadAllBytes ( genreImageFilename ) ;
}
}
catch ( Exception ex )
{
Logger . LogError ( ex , $"Error Reading Image File [{genreImageFilename}]" ) ;
}
2019-11-04 03:19:04 +00:00
if ( image . Bytes = = null | | ! image . Bytes . Any ( ) )
2019-08-22 20:33:07 +00:00
{
image = DefaultNotFoundImages . Genre ;
}
return Task . FromResult ( GenerateFileOperationResult ( id , image , etag ) ) ;
}
catch ( Exception ex )
{
Logger . LogError ( $"Error fetching Label Thumbnail [{id}]" , ex ) ;
}
2019-11-04 03:19:04 +00:00
return Task . FromResult ( new FileOperationResult < IImage > ( OperationMessages . ErrorOccured ) ) ;
2019-08-22 20:33:07 +00:00
}
2019-11-04 03:19:04 +00:00
private async Task < FileOperationResult < IImage > > GetImageFileOperation ( string type , string regionUrn , Guid id , int? width , int? height , Func < Task < FileOperationResult < IImage > > > action , EntityTagHeaderValue etag = null )
2018-11-11 17:27:13 +00:00
{
2019-05-11 03:07:45 +00:00
try
2018-11-11 17:27:13 +00:00
{
2019-05-11 03:07:45 +00:00
var sw = Stopwatch . StartNew ( ) ;
2019-11-04 03:19:04 +00:00
var sizeHash = ( width ? ? 0 ) + ( height ? ? 0 ) ;
var result = await CacheManager . GetAsync ( $"urn:{type}_by_id_operation:{id}:{sizeHash}" , action , regionUrn ) ;
2019-09-05 02:04:20 +00:00
if ( ! result . IsSuccess )
{
2019-11-04 03:19:04 +00:00
return new FileOperationResult < IImage > ( result . IsNotFoundResult , result . Messages ) ;
2019-09-05 02:04:20 +00:00
}
if ( result . ETag = = etag & & etag ! = null )
{
2019-11-04 03:19:04 +00:00
return new FileOperationResult < IImage > ( OperationMessages . NotModified ) ;
2019-09-05 02:04:20 +00:00
}
2019-07-13 12:28:27 +00:00
var force = width . HasValue | | height . HasValue ;
var newWidth = width ? ? Configuration . MaximumImageSize . Width ;
var newHeight = height ? ? Configuration . MaximumImageSize . Height ;
if ( result ? . Data ? . Bytes ! = null )
2018-11-11 17:27:13 +00:00
{
2019-07-13 12:28:27 +00:00
var resized = ImageHelper . ResizeImage ( result ? . Data ? . Bytes , newWidth , newHeight , force ) ;
2019-08-11 19:11:29 +00:00
if ( resized ! = null )
2019-07-13 12:28:27 +00:00
{
2019-08-11 19:11:29 +00:00
result . Data . Bytes = resized . Item2 ;
result . ETag = EtagHelper . GenerateETag ( HttpEncoder , result . Data . Bytes ) ;
result . LastModified = DateTime . UtcNow ;
if ( resized . Item1 )
{
Logger . LogTrace ( $"{type}: Resized [{id}], Width [{ newWidth}], Height [{ newHeight}], Forced [{ force }]" ) ;
}
}
else
{
Logger . LogTrace ( $"{type}: Image [{id}] Request returned Null Image, Forced [{ force }]" ) ;
2019-07-13 12:28:27 +00:00
}
2018-11-11 17:27:13 +00:00
}
2019-06-30 22:14:36 +00:00
2019-05-11 03:07:45 +00:00
sw . Stop ( ) ;
2019-11-04 03:19:04 +00:00
return new FileOperationResult < IImage > ( result . Messages )
2019-05-11 03:07:45 +00:00
{
Data = result . Data ,
ETag = result . ETag ,
LastModified = result . LastModified ,
ContentType = result . ContentType ,
Errors = result ? . Errors ,
IsSuccess = result ? . IsSuccess ? ? false ,
OperationTime = sw . ElapsedMilliseconds
} ;
2018-11-11 17:27:13 +00:00
}
2019-05-11 03:07:45 +00:00
catch ( Exception ex )
{
2019-06-30 22:14:36 +00:00
Logger . LogError ( ex , $"GetImageFileOperation Error, Type [{type}], id [{id}]" ) ;
2019-05-11 03:07:45 +00:00
}
2019-06-30 22:14:36 +00:00
2019-11-04 03:19:04 +00:00
return new FileOperationResult < IImage > ( "System Error" ) ;
2018-11-11 17:27:13 +00:00
}
2019-11-04 03:19:04 +00:00
private Task < FileOperationResult < IImage > > ImageByIdAction ( Guid id , EntityTagHeaderValue etag = null )
2018-11-11 17:27:13 +00:00
{
try
{
2019-11-04 03:19:04 +00:00
// TODO #29; fetch image by ID
throw new NotImplementedException ( ) ;
//var image = DbContext.Images
// .Include("Release")
// .Include("Artist")
// .FirstOrDefault(x => x.RoadieId == id);
//if (image == null)
//{
// return Task.FromResult(new FileOperationResult<IImage>(true, string.Format("ImageById Not Found [{0}]", id)));
//}
//return Task.FromResult(GenerateFileOperationResult(id, image, etag));
2018-11-11 17:27:13 +00:00
}
catch ( Exception ex )
{
2019-06-30 22:14:36 +00:00
Logger . LogError ( $"Error fetching Image [{id}]" , ex ) ;
2018-11-11 17:27:13 +00:00
}
2019-06-30 22:14:36 +00:00
2019-11-04 03:19:04 +00:00
return Task . FromResult ( new FileOperationResult < IImage > ( OperationMessages . ErrorOccured ) ) ;
2018-11-11 17:27:13 +00:00
}
2018-11-11 20:10:10 +00:00
2019-11-04 03:19:04 +00:00
private Task < FileOperationResult < IImage > > LabelImageAction ( Guid id , EntityTagHeaderValue etag = null )
2018-11-11 20:10:10 +00:00
{
try
{
2019-06-30 22:14:36 +00:00
var label = GetLabel ( id ) ;
2018-11-11 20:10:10 +00:00
if ( label = = null )
2019-11-04 03:19:04 +00:00
return Task . FromResult ( new FileOperationResult < IImage > ( true ,
2019-06-30 22:14:36 +00:00
string . Format ( "Label Not Found [{0}]" , id ) ) ) ;
2019-11-04 03:19:04 +00:00
IImage image = new Library . Imaging . Image ( id )
2018-11-11 20:10:10 +00:00
{
2019-11-04 03:19:04 +00:00
CreatedDate = label . CreatedDate
2018-11-11 20:10:10 +00:00
} ;
2019-07-31 16:44:25 +00:00
var labelImageFilename = label . PathToImage ( Configuration ) ;
try
{
2019-08-22 20:33:07 +00:00
if ( File . Exists ( labelImageFilename ) )
2019-07-31 16:44:25 +00:00
{
image . Bytes = File . ReadAllBytes ( labelImageFilename ) ;
}
}
catch ( Exception ex )
{
Logger . LogError ( ex , $"Error Reading Image File [{labelImageFilename}]" ) ;
}
2019-11-04 03:19:04 +00:00
if ( image . Bytes = = null | | ! image . Bytes . Any ( ) )
2019-07-31 16:44:25 +00:00
{
image = DefaultNotFoundImages . Label ;
}
2018-12-24 19:40:49 +00:00
return Task . FromResult ( GenerateFileOperationResult ( id , image , etag ) ) ;
2018-11-11 20:10:10 +00:00
}
catch ( Exception ex )
{
2019-06-30 22:14:36 +00:00
Logger . LogError ( $"Error fetching Label Thumbnail [{id}]" , ex ) ;
2018-11-11 20:10:10 +00:00
}
2019-06-30 22:14:36 +00:00
2019-11-04 03:19:04 +00:00
return Task . FromResult ( new FileOperationResult < IImage > ( OperationMessages . ErrorOccured ) ) ;
2018-11-11 20:10:10 +00:00
}
2019-11-04 03:19:04 +00:00
private Task < FileOperationResult < IImage > > PlaylistImageAction ( Guid id , EntityTagHeaderValue etag = null )
2018-11-11 20:10:10 +00:00
{
try
{
2019-06-30 22:14:36 +00:00
var playlist = GetPlaylist ( id ) ;
2018-11-11 20:10:10 +00:00
if ( playlist = = null )
2019-11-04 03:19:04 +00:00
return Task . FromResult ( new FileOperationResult < IImage > ( true ,
2019-06-30 22:14:36 +00:00
string . Format ( "Playlist Not Found [{0}]" , id ) ) ) ;
2019-11-04 03:19:04 +00:00
IImage image = new Library . Imaging . Image ( id )
2018-11-11 20:10:10 +00:00
{
2019-11-04 03:19:04 +00:00
CreatedDate = playlist . CreatedDate
2018-11-11 20:10:10 +00:00
} ;
2019-07-31 16:44:25 +00:00
var playlistImageFilename = playlist . PathToImage ( Configuration ) ;
try
{
if ( File . Exists ( playlistImageFilename ) )
{
image . Bytes = File . ReadAllBytes ( playlistImageFilename ) ;
}
}
catch ( Exception ex )
{
Logger . LogError ( ex , $"Error Reading Image File [{playlistImageFilename}]" ) ;
}
2019-11-04 03:19:04 +00:00
if ( image . Bytes = = null | | ! image . Bytes . Any ( ) )
2019-07-31 16:44:25 +00:00
{
image = DefaultNotFoundImages . Playlist ;
}
2018-12-24 19:40:49 +00:00
return Task . FromResult ( GenerateFileOperationResult ( id , image , etag ) ) ;
2018-11-11 20:10:10 +00:00
}
catch ( Exception ex )
{
2019-06-30 22:14:36 +00:00
Logger . LogError ( $"Error fetching Playlist Thumbnail [{id}]" , ex ) ;
2018-11-11 20:10:10 +00:00
}
2019-06-30 22:14:36 +00:00
2019-11-04 03:19:04 +00:00
return Task . FromResult ( new FileOperationResult < IImage > ( OperationMessages . ErrorOccured ) ) ;
2018-11-11 20:10:10 +00:00
}
2019-07-25 15:43:11 +00:00
/// <summary>
/// Get image for Release from Release folder, if not exists then use Release.Thumbnail if that is not set then use Release DefaultNotFound image.
/// </summary>
2019-11-04 03:19:04 +00:00
private Task < FileOperationResult < IImage > > ReleaseImageAction ( Guid id , EntityTagHeaderValue etag = null )
2018-11-11 20:10:10 +00:00
{
try
{
2019-06-30 22:14:36 +00:00
var release = GetRelease ( id ) ;
2018-11-11 20:10:10 +00:00
if ( release = = null )
2019-07-07 03:16:33 +00:00
{
2019-11-04 03:19:04 +00:00
return Task . FromResult ( new FileOperationResult < IImage > ( true , string . Format ( "Release Not Found [{0}]" , id ) ) ) ;
2019-07-07 03:16:33 +00:00
}
2018-11-22 17:31:59 +00:00
byte [ ] imageBytes = null ;
string artistFolder = null ;
2018-11-24 01:46:12 +00:00
string releaseFolder = null ;
2018-11-22 17:31:59 +00:00
try
{
// See if cover art file exists in release folder
2019-07-07 03:16:33 +00:00
artistFolder = release . Artist . ArtistFileFolder ( Configuration ) ;
2018-11-24 01:46:12 +00:00
if ( ! Directory . Exists ( artistFolder ) )
{
2019-08-03 22:59:20 +00:00
Logger . LogTrace ( $"Artist Folder [{artistFolder}], Not Found For Artist `{release.Artist}`" ) ;
2018-11-24 01:46:12 +00:00
}
else
2018-11-22 17:31:59 +00:00
{
2018-11-24 01:46:12 +00:00
releaseFolder = release . ReleaseFileFolder ( artistFolder ) ;
if ( ! Directory . Exists ( releaseFolder ) )
{
2019-06-30 22:14:36 +00:00
Logger . LogWarning ( $"Release Folder [{releaseFolder}], Not Found For Release `{release}`" ) ;
2018-11-24 01:46:12 +00:00
}
else
{
2019-07-07 03:16:33 +00:00
var releaseCoverFiles = ImageHelper . FindImageTypeInDirectory ( new DirectoryInfo ( releaseFolder ) , ImageType . Release ) ;
2019-05-29 22:25:40 +00:00
if ( releaseCoverFiles . Any ( ) )
2019-07-07 03:16:33 +00:00
{
2019-02-10 00:19:26 +00:00
imageBytes = File . ReadAllBytes ( releaseCoverFiles . First ( ) . FullName ) ;
2019-07-07 03:16:33 +00:00
}
2018-11-24 01:46:12 +00:00
}
2018-11-22 17:31:59 +00:00
}
}
catch ( Exception ex )
{
2019-07-25 15:43:11 +00:00
Logger . LogError ( ex , $"Error Reading Release Folder [{releaseFolder}] Artist Folder [{artistFolder}] For Artist `{release.Artist.Id}`" ) ;
2018-11-22 17:31:59 +00:00
}
2019-11-04 03:19:04 +00:00
IImage image = new Library . Imaging . Image ( id )
2018-11-11 20:10:10 +00:00
{
2018-11-22 17:31:59 +00:00
Bytes = imageBytes ,
2019-11-04 03:19:04 +00:00
CreatedDate = release . CreatedDate
2018-11-11 20:10:10 +00:00
} ;
2019-11-04 03:19:04 +00:00
if ( imageBytes ? . Any ( ) ! = true )
2019-07-25 15:43:11 +00:00
{
image = DefaultNotFoundImages . Release ;
}
2018-12-24 19:40:49 +00:00
return Task . FromResult ( GenerateFileOperationResult ( id , image , etag ) ) ;
2018-11-11 20:10:10 +00:00
}
catch ( Exception ex )
{
2019-06-30 22:14:36 +00:00
Logger . LogError ( $"Error fetching Release Thumbnail [{id}]" , ex ) ;
2018-11-11 20:10:10 +00:00
}
2019-06-30 22:14:36 +00:00
2019-11-04 03:19:04 +00:00
return Task . FromResult ( new FileOperationResult < IImage > ( OperationMessages . ErrorOccured ) ) ;
2018-11-11 20:10:10 +00:00
}
2019-11-04 03:19:04 +00:00
private Task < FileOperationResult < IImage > > ReleaseSecondaryImageAction ( Guid id , int imageId ,
2019-06-30 22:14:36 +00:00
EntityTagHeaderValue etag = null )
2019-02-10 00:19:26 +00:00
{
try
{
2019-06-30 22:14:36 +00:00
var release = GetRelease ( id ) ;
2019-02-10 00:19:26 +00:00
if ( release = = null )
2019-11-04 03:19:04 +00:00
{
return Task . FromResult ( new FileOperationResult < IImage > ( true , string . Format ( "Release Not Found [{0}]" , id ) ) ) ;
}
2019-02-10 00:19:26 +00:00
byte [ ] imageBytes = null ;
string artistFolder = null ;
string releaseFolder = null ;
try
{
// See if cover art file exists in release folder
2019-07-07 03:16:33 +00:00
artistFolder = release . Artist . ArtistFileFolder ( Configuration ) ;
2019-02-10 00:19:26 +00:00
if ( ! Directory . Exists ( artistFolder ) )
{
2019-08-03 22:59:20 +00:00
Logger . LogTrace ( $"Artist Folder [{artistFolder}], Not Found For Artist `{release.Artist}`" ) ;
2019-02-10 00:19:26 +00:00
}
else
{
releaseFolder = release . ReleaseFileFolder ( artistFolder ) ;
if ( ! Directory . Exists ( releaseFolder ) )
{
2019-06-30 22:14:36 +00:00
Logger . LogWarning ( $"Release Folder [{releaseFolder}], Not Found For Release `{release}`" ) ;
2019-02-10 00:19:26 +00:00
}
else
{
2019-06-30 22:14:36 +00:00
var releaseSecondaryImages = ImageHelper
. FindImageTypeInDirectory ( new DirectoryInfo ( releaseFolder ) , ImageType . ReleaseSecondary )
. ToArray ( ) ;
2019-05-29 22:25:40 +00:00
if ( releaseSecondaryImages . Length > = imageId & & releaseSecondaryImages [ imageId ] ! = null )
2019-02-10 00:19:26 +00:00
imageBytes = File . ReadAllBytes ( releaseSecondaryImages [ imageId ] . FullName ) ;
}
}
}
catch ( Exception ex )
{
2019-06-30 22:14:36 +00:00
Logger . LogError ( ex ,
$"Error Reading Release Folder [{releaseFolder}] Artist Folder [{artistFolder}] For Artist `{release.Artist.Id}`" ) ;
2019-02-10 00:19:26 +00:00
}
2019-06-30 22:14:36 +00:00
2019-11-04 03:19:04 +00:00
IImage image = new Library . Imaging . Image ( id )
2019-02-10 00:19:26 +00:00
{
Bytes = imageBytes ,
2019-11-04 03:19:04 +00:00
CreatedDate = release . CreatedDate
2019-02-10 00:19:26 +00:00
} ;
return Task . FromResult ( GenerateFileOperationResult ( id , image , etag ) ) ;
}
catch ( Exception ex )
{
2019-06-30 22:14:36 +00:00
Logger . LogError ( $"Error fetching Release Thumbnail [{id}]" , ex ) ;
2019-02-10 00:19:26 +00:00
}
2019-06-30 22:14:36 +00:00
2019-11-04 03:19:04 +00:00
return Task . FromResult ( new FileOperationResult < IImage > ( OperationMessages . ErrorOccured ) ) ;
2019-02-10 00:19:26 +00:00
}
2019-11-04 03:19:04 +00:00
private async Task < FileOperationResult < IImage > > TrackImageAction ( Guid id , int? width , int? height , EntityTagHeaderValue etag = null )
2018-11-11 20:10:10 +00:00
{
try
{
2019-06-30 22:14:36 +00:00
var track = GetTrack ( id ) ;
2018-11-11 20:10:10 +00:00
if ( track = = null )
2019-11-04 03:19:04 +00:00
{
return new FileOperationResult < IImage > ( true , string . Format ( "Track Not Found [{0}]" , id ) ) ;
}
IImage image = new Library . Imaging . Image ( id )
{
CreatedDate = track . CreatedDate
2018-11-11 20:10:10 +00:00
} ;
2019-11-10 23:54:15 +00:00
var trackImageFileName = track . PathToTrackThumbnail ( Configuration ) ;
if ( File . Exists ( trackImageFileName ) )
2019-11-04 03:19:04 +00:00
{
2019-11-10 23:54:15 +00:00
image . Bytes = File . ReadAllBytes ( trackImageFileName ) ;
2019-11-04 03:19:04 +00:00
}
if ( image . Bytes = = null | | ! image . Bytes . Any ( ) )
{
2018-11-21 06:34:53 +00:00
// If no track image is found then return image for release
2019-06-30 22:14:36 +00:00
return await ReleaseImage ( track . ReleaseMedia . Release . RoadieId , width , height , etag ) ;
2019-11-04 03:19:04 +00:00
}
2018-11-11 20:10:10 +00:00
return GenerateFileOperationResult ( id , image , etag ) ;
}
catch ( Exception ex )
{
2019-06-30 22:14:36 +00:00
Logger . LogError ( $"Error fetching Track Thumbnail [{id}]" , ex ) ;
2018-11-11 20:10:10 +00:00
}
2019-06-30 22:14:36 +00:00
2019-11-04 03:19:04 +00:00
return new FileOperationResult < IImage > ( OperationMessages . ErrorOccured ) ;
2018-11-11 20:10:10 +00:00
}
2019-11-04 03:19:04 +00:00
private Task < FileOperationResult < IImage > > UserImageAction ( Guid id , EntityTagHeaderValue etag = null )
2018-11-11 20:10:10 +00:00
{
try
{
2019-06-30 22:14:36 +00:00
var user = GetUser ( id ) ;
2018-11-11 20:10:10 +00:00
if ( user = = null )
2019-07-31 16:44:25 +00:00
{
2019-11-04 03:19:04 +00:00
return Task . FromResult ( new FileOperationResult < IImage > ( true , string . Format ( "User Not Found [{0}]" , id ) ) ) ;
2019-07-31 16:44:25 +00:00
}
2019-11-04 03:19:04 +00:00
IImage image = new Library . Imaging . Image ( id )
2018-11-11 20:10:10 +00:00
{
2019-11-04 03:19:04 +00:00
CreatedDate = user . CreatedDate . Value
2018-11-11 20:10:10 +00:00
} ;
2019-07-31 16:44:25 +00:00
var userImageFilename = user . PathToImage ( Configuration ) ;
try
{
if ( File . Exists ( userImageFilename ) )
{
image . Bytes = File . ReadAllBytes ( userImageFilename ) ;
}
}
catch ( Exception ex )
{
Logger . LogError ( ex , $"Error Reading Image File [{userImageFilename}]" ) ;
}
2019-11-04 03:19:04 +00:00
if ( image . Bytes = = null | | ! image . Bytes . Any ( ) )
2019-07-31 16:44:25 +00:00
{
image = DefaultNotFoundImages . User ;
}
2018-12-24 19:40:49 +00:00
return Task . FromResult ( GenerateFileOperationResult ( id , image , etag , "image/png" ) ) ;
2018-11-11 20:10:10 +00:00
}
catch ( Exception ex )
{
2019-06-30 22:14:36 +00:00
Logger . LogError ( $"Error fetching User Thumbnail [{id}]" , ex ) ;
2018-11-11 20:10:10 +00:00
}
2019-06-30 22:14:36 +00:00
2019-11-04 03:19:04 +00:00
return Task . FromResult ( new FileOperationResult < IImage > ( OperationMessages . ErrorOccured ) ) ;
2018-11-11 20:10:10 +00:00
}
2018-11-11 01:11:58 +00:00
}
}