2018-11-11 01:11:58 +00:00
using Mapster ;
using Microsoft.EntityFrameworkCore ;
using Microsoft.Extensions.Logging ;
using Microsoft.Net.Http.Headers ;
using Roadie.Library ;
using Roadie.Library.Caching ;
using Roadie.Library.Configuration ;
using Roadie.Library.Encoding ;
2019-06-30 22:14:36 +00:00
using Roadie.Library.Enums ;
2019-07-07 03:16:33 +00:00
using Roadie.Library.Extensions ;
2018-11-11 20:10:10 +00:00
using Roadie.Library.Identity ;
2018-11-11 14:46:09 +00:00
using Roadie.Library.Imaging ;
2019-07-07 03:16:33 +00:00
using Roadie.Library.MetaData.Audio ;
2018-11-11 01:11:58 +00:00
using Roadie.Library.Models ;
2018-11-11 20:45:44 +00:00
using Roadie.Library.Models.Users ;
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-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
2019-07-07 03:16:33 +00:00
public string Referrer { get ; set ; }
2019-06-30 22:14:36 +00:00
2019-07-07 03:16:33 +00:00
public string RequestIp { get ; set ; }
2018-11-11 16:20: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 ,
data . IRoadieDbContext context ,
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-07-07 03:16:33 +00:00
public async Task < FileOperationResult < Image > > 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-07-07 03:16:33 +00:00
public async Task < FileOperationResult < Image > > ArtistSecondaryImage ( 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 ( $"ArtistSecondaryThumbnail-{imageId}" ,
data . Release . CacheRegionUrn ( id ) ,
id ,
width ,
height ,
async ( ) = > { return await ArtistSecondaryImageAction ( id , imageId , etag ) ; } ,
etag ) ;
2019-02-10 00:19:26 +00:00
}
2019-07-07 03:16:33 +00:00
public async Task < FileOperationResult < Image > > 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" ,
data . Image . CacheRegionUrn ( id ) ,
id ,
width ,
height ,
async ( ) = > { return await ImageByIdAction ( id , etag ) ; } ,
etag ) ;
2018-11-16 03:37:00 +00:00
}
2019-07-07 03:16:33 +00:00
public async Task < FileOperationResult < Image > > 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-07-07 03:16:33 +00:00
public async Task < OperationResult < bool > > Delete ( ApplicationUser user , Guid id )
2018-11-14 23:18:02 +00:00
{
var sw = Stopwatch . StartNew ( ) ;
2019-06-30 22:14:36 +00:00
var image = DbContext . Images
. Include ( "Release" )
. Include ( "Artist" )
. FirstOrDefault ( x = > x . RoadieId = = id ) ;
if ( image = = null ) return new OperationResult < bool > ( true , string . Format ( "Image Not Found [{0}]" , id ) ) ;
DbContext . Images . Remove ( image ) ;
await DbContext . SaveChangesAsync ( ) ;
2019-07-07 03:16:33 +00:00
if ( image . ArtistId . HasValue ) CacheManager . ClearRegion ( data . Artist . CacheRegionUrn ( image . Artist . RoadieId ) ) ;
if ( image . ReleaseId . HasValue ) CacheManager . ClearRegion ( data . Release . CacheRegionUrn ( image . Release . RoadieId ) ) ;
2019-06-30 22:14:36 +00:00
CacheManager . ClearRegion ( data . Image . CacheRegionUrn ( id ) ) ;
2019-08-03 22:59:20 +00:00
Logger . LogWarning ( "User `{0}` deleted Image `{1}]`" , user , image ) ;
2018-11-14 23:18:02 +00:00
sw . Stop ( ) ;
return new OperationResult < bool >
{
Data = true ,
IsSuccess = true ,
OperationTime = sw . ElapsedMilliseconds
} ;
}
2019-07-07 03:16:33 +00:00
public async Task < FileOperationResult < Image > > LabelImage ( 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 ( "LabelThumbnail" ,
data . Label . CacheRegionUrn ( id ) ,
id ,
width ,
height ,
async ( ) = > { return await LabelImageAction ( id , etag ) ; } ,
etag ) ;
2018-11-11 20:10:10 +00:00
}
2019-08-02 20:59:24 +00:00
public async Task < FileOperationResult < Image > > GenreImage ( Guid id , int? width , int? height , EntityTagHeaderValue etag = null )
{
return await GetImageFileOperation ( "GenreThumbnail" ,
data . Label . CacheRegionUrn ( id ) ,
id ,
width ,
height ,
async ( ) = > { return await GenreImageAction ( id , etag ) ; } ,
etag ) ;
}
2019-07-07 03:16:33 +00:00
public async Task < FileOperationResult < Image > > 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 ,
async ( ) = > { return await PlaylistImageAction ( id , etag ) ; } ,
etag ) ;
2018-11-11 20:10:10 +00:00
}
2019-07-07 03:16:33 +00:00
public async Task < FileOperationResult < Image > > 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 ,
async ( ) = > { return await ReleaseImageAction ( id , etag ) ; } ,
etag ) ;
2018-11-11 20:10:10 +00:00
}
2019-07-07 03:16:33 +00:00
public async Task < FileOperationResult < Image > > 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-07-07 03:16:33 +00:00
2018-12-21 22:59:33 +00:00
}
2019-07-07 03:16:33 +00:00
public async Task < FileOperationResult < Image > > 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 ,
async ( ) = > { return await TrackImageAction ( id , width , height , etag ) ; } ,
etag ) ;
2019-01-08 22:40:26 +00:00
}
2019-07-07 03:16:33 +00:00
public async Task < FileOperationResult < Image > > 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 ,
async ( ) = > { return await UserImageAction ( id , etag ) ; } ,
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>
2018-12-24 19:40:49 +00:00
private Task < FileOperationResult < Image > > 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
{
return Task . FromResult ( new FileOperationResult < Image > ( true , string . Format ( "Artist Not Found [{0}]" , id ) ) ) ;
}
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
2018-11-22 17:31:59 +00:00
imageBytes = imageBytes ? ? artist . Thumbnail ;
2018-11-11 16:20:33 +00:00
var image = new data . Image
{
2018-11-22 17:31:59 +00:00
Bytes = imageBytes ,
2018-11-11 16:20:33 +00:00
CreatedDate = artist . CreatedDate ,
LastUpdated = artist . LastUpdated
} ;
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
2018-12-24 19:40:49 +00:00
return Task . FromResult ( new FileOperationResult < Image > ( OperationMessages . ErrorOccured ) ) ;
2018-11-11 16:20:33 +00:00
}
2018-11-11 01:11:58 +00:00
2019-07-07 03:16:33 +00:00
private Task < FileOperationResult < Image > > 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-06-30 22:14:36 +00:00
return Task . FromResult ( new FileOperationResult < Image > ( 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-05-29 22:25:40 +00:00
var image = new data . Image
{
Bytes = imageBytes ,
CreatedDate = artist . CreatedDate ,
LastUpdated = artist . LastUpdated
} ;
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-05-29 22:25:40 +00:00
return Task . FromResult ( new FileOperationResult < Image > ( OperationMessages . ErrorOccured ) ) ;
}
2018-12-24 19:40:49 +00:00
private Task < FileOperationResult < Image > > 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
{
return Task . FromResult ( new FileOperationResult < Image > ( true , string . Format ( "Collection Not Found [{0}]" , id ) ) ) ;
}
2018-11-11 17:27:13 +00:00
var image = new data . Image
{
2018-11-11 20:10:10 +00:00
Bytes = collection . Thumbnail ,
CreatedDate = collection . CreatedDate ,
LastUpdated = collection . LastUpdated
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}]" ) ;
}
2018-11-11 20:10:10 +00:00
if ( collection . Thumbnail = = null | | ! collection . Thumbnail . 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
2018-12-24 19:40:49 +00:00
return Task . FromResult ( new FileOperationResult < Image > ( OperationMessages . ErrorOccured ) ) ;
2018-11-11 01:11:58 +00:00
}
2018-11-11 16:20:33 +00:00
2019-06-30 22:14:36 +00:00
private FileOperationResult < Image > GenerateFileOperationResult ( Guid id , data . Image 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 ) )
2018-11-11 16:20:33 +00:00
return new FileOperationResult < Image > ( OperationMessages . NotModified ) ;
if ( ! image ? . Bytes ? . Any ( ) ? ? false )
return new FileOperationResult < Image > ( string . Format ( "ImageById Not Set [{0}]" , id ) ) ;
2019-06-30 22:14:36 +00:00
return new FileOperationResult < Image > ( image ? . Bytes ? . Any ( ) ? ? false
? OperationMessages . OkMessage
: OperationMessages . NoImageDataFound )
2018-11-11 16:20:33 +00:00
{
IsSuccess = true ,
Data = image . Adapt < Image > ( ) ,
2018-11-21 18:19:38 +00:00
ContentType = contentType ,
2019-06-30 22:14:36 +00:00
LastModified = image . LastUpdated ? ? image . CreatedDate ,
2018-11-11 16:20:33 +00:00
ETag = imageEtag
} ;
}
2019-07-13 12:28:27 +00:00
private async Task < FileOperationResult < Image > > GetImageFileOperation ( string type , string regionUrn , Guid id , int? width , int? height , Func < Task < FileOperationResult < Image > > > 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-07-13 12:28:27 +00:00
var result = ( await CacheManager . GetAsync ( $"urn:{type}_by_id_operation:{id}" , action , regionUrn ) ) . Adapt < FileOperationResult < Image > > ( ) ;
2019-06-30 22:14:36 +00:00
if ( ! result . IsSuccess ) return new FileOperationResult < Image > ( result . IsNotFoundResult , result . Messages ) ;
if ( result . ETag = = etag ) return new FileOperationResult < Image > ( OperationMessages . NotModified ) ;
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 ) ;
result . Data . Bytes = resized . Item2 ;
2019-06-30 22:14:36 +00:00
result . ETag = EtagHelper . GenerateETag ( HttpEncoder , result . Data . Bytes ) ;
2019-05-11 03:07:45 +00:00
result . LastModified = DateTime . UtcNow ;
2019-07-13 12:28:27 +00:00
if ( resized . Item1 )
{
Logger . LogTrace ( $"{type}: Resized [{id}], Width [{ newWidth}], Height [{ newHeight}], Forced [{ force }]" ) ;
}
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 ( ) ;
return new FileOperationResult < Image > ( result . Messages )
{
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-05-11 03:07:45 +00:00
return new FileOperationResult < Image > ( "System Error" ) ;
2018-11-11 17:27:13 +00:00
}
2018-12-24 19:40:49 +00:00
private Task < FileOperationResult < Image > > ImageByIdAction ( Guid id , EntityTagHeaderValue etag = null )
2018-11-11 17:27:13 +00:00
{
try
{
2019-06-30 22:14:36 +00:00
var image = DbContext . Images
. Include ( "Release" )
. Include ( "Artist" )
. FirstOrDefault ( x = > x . RoadieId = = id ) ;
2018-11-11 17:27:13 +00:00
if ( image = = null )
2019-06-30 22:14:36 +00:00
return Task . FromResult ( new FileOperationResult < Image > ( true ,
string . Format ( "ImageById Not Found [{0}]" , id ) ) ) ;
2018-12-24 19:40:49 +00:00
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
2018-12-24 19:40:49 +00:00
return Task . FromResult ( new FileOperationResult < Image > ( OperationMessages . ErrorOccured ) ) ;
2018-11-11 17:27:13 +00:00
}
2018-11-11 20:10:10 +00:00
2018-12-24 19:40:49 +00:00
private Task < FileOperationResult < Image > > 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-06-30 22:14:36 +00:00
return Task . FromResult ( new FileOperationResult < Image > ( true ,
string . Format ( "Label Not Found [{0}]" , id ) ) ) ;
2018-11-11 20:10:10 +00:00
var image = new data . Image
{
Bytes = label . Thumbnail ,
CreatedDate = label . CreatedDate ,
LastUpdated = label . LastUpdated
} ;
2019-07-31 16:44:25 +00:00
var labelImageFilename = label . PathToImage ( Configuration ) ;
try
{
if ( File . Exists ( labelImageFilename ) )
{
image . Bytes = File . ReadAllBytes ( labelImageFilename ) ;
}
}
catch ( Exception ex )
{
Logger . LogError ( ex , $"Error Reading Image File [{labelImageFilename}]" ) ;
}
if ( label . Thumbnail = = null | | ! label . Thumbnail . Any ( ) )
{
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
2018-12-24 19:40:49 +00:00
return Task . FromResult ( new FileOperationResult < Image > ( OperationMessages . ErrorOccured ) ) ;
2018-11-11 20:10:10 +00:00
}
2019-08-02 20:59:24 +00:00
private Task < FileOperationResult < Image > > GenreImageAction ( Guid id , EntityTagHeaderValue etag = null )
{
try
{
var genre = GetGenre ( id ) ;
if ( genre = = null )
{
return Task . FromResult ( new FileOperationResult < Image > ( true , string . Format ( "Genre Not Found [{0}]" , id ) ) ) ;
}
var image = new data . Image
{
Bytes = genre . Thumbnail ,
CreatedDate = genre . CreatedDate ,
LastUpdated = genre . LastUpdated
} ;
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}]" ) ;
}
if ( genre . Thumbnail = = null | | ! genre . Thumbnail . Any ( ) )
{
image = DefaultNotFoundImages . Genre ;
}
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 ) ) ;
}
2018-12-24 19:40:49 +00:00
private Task < FileOperationResult < Image > > 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-06-30 22:14:36 +00:00
return Task . FromResult ( new FileOperationResult < Image > ( true ,
string . Format ( "Playlist Not Found [{0}]" , id ) ) ) ;
2018-11-11 20:10:10 +00:00
var image = new data . Image
{
Bytes = playlist . Thumbnail ,
CreatedDate = playlist . CreatedDate ,
LastUpdated = playlist . LastUpdated
} ;
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}]" ) ;
}
if ( playlist . Thumbnail = = null | | ! playlist . Thumbnail . Any ( ) )
{
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
2018-12-24 19:40:49 +00:00
return Task . FromResult ( new FileOperationResult < Image > ( 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>
2018-12-24 19:40:49 +00:00
private Task < FileOperationResult < Image > > 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
{
return Task . FromResult ( new FileOperationResult < Image > ( true , string . Format ( "Release Not Found [{0}]" , id ) ) ) ;
}
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-06-30 22:14:36 +00:00
2018-11-22 17:31:59 +00:00
imageBytes = imageBytes ? ? release . Thumbnail ;
2018-11-11 20:10:10 +00:00
var image = new data . Image
{
2018-11-22 17:31:59 +00:00
Bytes = imageBytes ,
2018-11-11 20:10:10 +00:00
CreatedDate = release . CreatedDate ,
LastUpdated = release . LastUpdated
} ;
2019-07-25 15:43:11 +00:00
if ( release . Thumbnail = = null | | ! release . Thumbnail . Any ( ) )
{
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
2018-12-24 19:40:49 +00:00
return Task . FromResult ( new FileOperationResult < Image > ( OperationMessages . ErrorOccured ) ) ;
2018-11-11 20:10:10 +00:00
}
2019-06-30 22:14:36 +00:00
private Task < FileOperationResult < Image > > ReleaseSecondaryImageAction ( Guid id , int imageId ,
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-06-30 22:14:36 +00:00
return Task . FromResult ( new FileOperationResult < Image > ( 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-02-10 00:19:26 +00:00
var image = new data . Image
{
Bytes = imageBytes ,
CreatedDate = release . CreatedDate ,
LastUpdated = release . LastUpdated
} ;
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-02-10 00:19:26 +00:00
return Task . FromResult ( new FileOperationResult < Image > ( OperationMessages . ErrorOccured ) ) ;
}
2019-06-30 22:14:36 +00:00
private async Task < FileOperationResult < Image > > 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 )
return new FileOperationResult < Image > ( true , string . Format ( "Track Not Found [{0}]" , id ) ) ;
2019-05-11 03:07:45 +00:00
var imageBytes = track . Thumbnail ;
2019-06-30 22:14:36 +00:00
var trackThumbnailImages = ImageHelper . FindImageTypeInDirectory (
2019-07-07 03:16:33 +00:00
new DirectoryInfo ( track . PathToTrackThumbnail ( Configuration ) ) ,
2019-06-30 22:14:36 +00:00
ImageType . Track , SearchOption . TopDirectoryOnly ) ;
if ( trackThumbnailImages . Any ( ) ) imageBytes = File . ReadAllBytes ( trackThumbnailImages . First ( ) . FullName ) ;
2018-11-11 20:10:10 +00:00
var image = new data . Image
{
Bytes = track . Thumbnail ,
CreatedDate = track . CreatedDate ,
LastUpdated = track . LastUpdated
} ;
if ( track . Thumbnail = = null | | ! track . Thumbnail . 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 ) ;
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
2018-11-11 20:10:10 +00:00
return new FileOperationResult < Image > ( OperationMessages . ErrorOccured ) ;
}
2018-12-24 19:40:49 +00:00
private Task < FileOperationResult < Image > > 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
{
return Task . FromResult ( new FileOperationResult < Image > ( true , string . Format ( "User Not Found [{0}]" , id ) ) ) ;
}
2018-11-11 20:10:10 +00:00
var image = new data . Image
{
Bytes = user . Avatar ,
CreatedDate = user . CreatedDate . Value ,
LastUpdated = user . LastUpdated
} ;
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}]" ) ;
}
if ( ! ( image ? . Bytes ? . Any ( ) ? ? false ) )
{
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
2018-12-24 19:40:49 +00:00
return Task . FromResult ( new FileOperationResult < Image > ( OperationMessages . ErrorOccured ) ) ;
2018-11-11 20:10:10 +00:00
}
2018-11-11 01:11:58 +00:00
}
}