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;
|
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 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
|
|
|
|
|
{
|
2018-12-21 22:59:33 +00:00
|
|
|
|
private IImageSearchEngine BingSearchEngine { get; }
|
2019-01-08 22:40:26 +00:00
|
|
|
|
private IDefaultNotFoundImages DefaultNotFoundImages { get; }
|
2018-12-21 22:59:33 +00:00
|
|
|
|
private IImageSearchEngine ITunesSearchEngine { get; }
|
|
|
|
|
|
|
|
|
|
private string Referrer { get; }
|
|
|
|
|
private string RequestIp { get; }
|
2018-11-11 16:20:33 +00:00
|
|
|
|
|
2018-11-11 01:11:58 +00:00
|
|
|
|
public ImageService(IRoadieSettings configuration,
|
|
|
|
|
IHttpEncoder httpEncoder,
|
|
|
|
|
IHttpContext httpContext,
|
|
|
|
|
data.IRoadieDbContext context,
|
|
|
|
|
ICacheManager cacheManager,
|
2018-11-12 00:28:37 +00:00
|
|
|
|
ILogger<ImageService> logger,
|
2018-11-11 16:20:33 +00:00
|
|
|
|
IDefaultNotFoundImages defaultNotFoundImages)
|
2018-11-11 01:11:58 +00:00
|
|
|
|
: base(configuration, httpEncoder, context, cacheManager, logger, httpContext)
|
|
|
|
|
{
|
2018-11-11 16:20:33 +00:00
|
|
|
|
this.DefaultNotFoundImages = defaultNotFoundImages;
|
2018-12-21 22:59:33 +00:00
|
|
|
|
this.BingSearchEngine = new BingImageSearchEngine(configuration, logger, this.RequestIp, this.Referrer);
|
|
|
|
|
this.ITunesSearchEngine = new ITunesSearchEngine(configuration, cacheManager, logger, this.RequestIp, this.Referrer);
|
2018-11-11 01:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 15:22:55 +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
|
|
|
|
{
|
2018-11-21 15:22:55 +00:00
|
|
|
|
return await this.GetImageFileOperation(type: "ArtistImage",
|
2018-11-11 17:27:13 +00:00
|
|
|
|
regionUrn: data.Artist.CacheRegionUrn(id),
|
|
|
|
|
id: id,
|
|
|
|
|
width: width,
|
|
|
|
|
height: height,
|
|
|
|
|
action: async () =>
|
|
|
|
|
{
|
2018-11-21 15:22:55 +00:00
|
|
|
|
return await this.ArtistImageAction(id, etag);
|
2018-11-11 17:27:13 +00:00
|
|
|
|
},
|
|
|
|
|
etag: etag);
|
2018-11-11 01:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-16 03:37:00 +00:00
|
|
|
|
public async Task<FileOperationResult<Image>> ById(Guid id, int? width, int? height, EntityTagHeaderValue etag = null)
|
|
|
|
|
{
|
|
|
|
|
return await this.GetImageFileOperation(type: "ImageById",
|
|
|
|
|
regionUrn: data.Image.CacheRegionUrn(id),
|
|
|
|
|
id: id,
|
|
|
|
|
width: width,
|
|
|
|
|
height: height,
|
|
|
|
|
action: async () =>
|
|
|
|
|
{
|
|
|
|
|
return await this.ImageByIdAction(id, etag);
|
|
|
|
|
},
|
|
|
|
|
etag: etag);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 15:22:55 +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
|
|
|
|
{
|
2018-11-11 20:10:10 +00:00
|
|
|
|
return await this.GetImageFileOperation(type: "CollectionThumbnail",
|
|
|
|
|
regionUrn: data.Collection.CacheRegionUrn(id),
|
2018-11-11 17:27:13 +00:00
|
|
|
|
id: id,
|
|
|
|
|
width: width,
|
|
|
|
|
height: height,
|
|
|
|
|
action: async () =>
|
|
|
|
|
{
|
2018-11-21 15:22:55 +00:00
|
|
|
|
return await this.CollectionImageAction(id, etag);
|
2018-11-11 17:27:13 +00:00
|
|
|
|
},
|
|
|
|
|
etag: etag);
|
2018-11-11 16:20:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-14 23:18:02 +00:00
|
|
|
|
public async Task<OperationResult<bool>> Delete(User user, Guid id)
|
|
|
|
|
{
|
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
var image = this.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));
|
|
|
|
|
}
|
|
|
|
|
if (image.ArtistId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
this.CacheManager.ClearRegion(data.Artist.CacheRegionUrn(image.Artist.RoadieId));
|
|
|
|
|
}
|
|
|
|
|
if (image.ReleaseId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
this.CacheManager.ClearRegion(data.Release.CacheRegionUrn(image.Release.RoadieId));
|
|
|
|
|
}
|
|
|
|
|
this.DbContext.Images.Remove(image);
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
|
|
|
|
this.CacheManager.ClearRegion(data.Image.CacheRegionUrn(id));
|
|
|
|
|
this.Logger.LogInformation($"Deleted Image [{ id }], By User [{ user.ToString() }]");
|
|
|
|
|
sw.Stop();
|
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
Data = true,
|
|
|
|
|
IsSuccess = true,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<OperationResult<IEnumerable<ImageSearchResult>>> ImageProvidersSearch(string query)
|
|
|
|
|
{
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
var errors = new List<Exception>();
|
|
|
|
|
IEnumerable<ImageSearchResult> searchResults = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var manager = new ImageSearchManager(this.Configuration, this.CacheManager, this.Logger);
|
|
|
|
|
searchResults = await manager.ImageSearch(query);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
this.Logger.LogError(ex);
|
|
|
|
|
errors.Add(ex);
|
|
|
|
|
}
|
|
|
|
|
sw.Stop();
|
|
|
|
|
return new OperationResult<IEnumerable<ImageSearchResult>>
|
|
|
|
|
{
|
|
|
|
|
Data = searchResults,
|
|
|
|
|
IsSuccess = !errors.Any(),
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds,
|
|
|
|
|
Errors = errors
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 15:22:55 +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
|
|
|
|
{
|
|
|
|
|
return await this.GetImageFileOperation(type: "LabelThumbnail",
|
|
|
|
|
regionUrn: data.Label.CacheRegionUrn(id),
|
|
|
|
|
id: id,
|
|
|
|
|
width: width,
|
|
|
|
|
height: height,
|
|
|
|
|
action: async () =>
|
|
|
|
|
{
|
2018-11-21 15:22:55 +00:00
|
|
|
|
return await this.LabelImageAction(id, etag);
|
2018-11-11 20:10:10 +00:00
|
|
|
|
},
|
|
|
|
|
etag: etag);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 15:22:55 +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
|
|
|
|
{
|
|
|
|
|
return await this.GetImageFileOperation(type: "PlaylistThumbnail",
|
|
|
|
|
regionUrn: data.Playlist.CacheRegionUrn(id),
|
|
|
|
|
id: id,
|
|
|
|
|
width: width,
|
|
|
|
|
height: height,
|
|
|
|
|
action: async () =>
|
|
|
|
|
{
|
2018-11-21 15:22:55 +00:00
|
|
|
|
return await this.PlaylistImageAction(id, etag);
|
2018-11-11 20:10:10 +00:00
|
|
|
|
},
|
|
|
|
|
etag: etag);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 15:22:55 +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
|
|
|
|
{
|
|
|
|
|
return await this.GetImageFileOperation(type: "ReleaseThumbnail",
|
|
|
|
|
regionUrn: data.Release.CacheRegionUrn(id),
|
|
|
|
|
id: id,
|
|
|
|
|
width: width,
|
|
|
|
|
height: height,
|
|
|
|
|
action: async () =>
|
|
|
|
|
{
|
2018-11-21 15:22:55 +00:00
|
|
|
|
return await this.ReleaseImageAction(id, etag);
|
2018-11-11 20:10:10 +00:00
|
|
|
|
},
|
|
|
|
|
etag: etag);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 22:59:33 +00:00
|
|
|
|
public async Task<OperationResult<IEnumerable<ImageSearchResult>>> Search(string query, int resultsCount = 10)
|
|
|
|
|
{
|
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
|
|
|
|
|
var result = new List<ImageSearchResult>();
|
|
|
|
|
|
|
|
|
|
if (WebHelper.IsStringUrl(query))
|
|
|
|
|
{
|
|
|
|
|
var s = ImageHelper.ImageSearchResultForImageUrl(query);
|
|
|
|
|
if (s != null)
|
|
|
|
|
{
|
|
|
|
|
result.Add(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var bingResults = await this.BingSearchEngine.PerformImageSearch(query, resultsCount);
|
|
|
|
|
if (bingResults != null)
|
|
|
|
|
{
|
|
|
|
|
result.AddRange(bingResults);
|
|
|
|
|
}
|
|
|
|
|
var iTunesResults = await this.ITunesSearchEngine.PerformImageSearch(query, resultsCount);
|
|
|
|
|
if (iTunesResults != null)
|
|
|
|
|
{
|
|
|
|
|
result.AddRange(iTunesResults);
|
|
|
|
|
}
|
|
|
|
|
sw.Stop();
|
|
|
|
|
return new OperationResult<IEnumerable<ImageSearchResult>>
|
|
|
|
|
{
|
|
|
|
|
IsSuccess = true,
|
|
|
|
|
Data = result,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 22:40:26 +00:00
|
|
|
|
public async Task<FileOperationResult<Image>> TrackImage(Guid id, int? width, int? height, EntityTagHeaderValue etag = null)
|
|
|
|
|
{
|
|
|
|
|
return await this.GetImageFileOperation(type: "TrackThumbnail",
|
|
|
|
|
regionUrn: data.Track.CacheRegionUrn(id),
|
|
|
|
|
id: id,
|
|
|
|
|
width: width,
|
|
|
|
|
height: height,
|
|
|
|
|
action: async () =>
|
|
|
|
|
{
|
|
|
|
|
return await this.TrackImageAction(id, width, height, etag);
|
|
|
|
|
},
|
|
|
|
|
etag: etag);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<FileOperationResult<Image>> UserImage(Guid id, int? width, int? height, EntityTagHeaderValue etag = null)
|
|
|
|
|
{
|
|
|
|
|
return await this.GetImageFileOperation(type: "UserById",
|
|
|
|
|
regionUrn: ApplicationUser.CacheRegionUrn(id),
|
|
|
|
|
id: id,
|
|
|
|
|
width: width,
|
|
|
|
|
height: height,
|
|
|
|
|
action: async () =>
|
|
|
|
|
{
|
|
|
|
|
return await this.UserImageAction(id, etag);
|
|
|
|
|
},
|
|
|
|
|
etag: etag);
|
|
|
|
|
}
|
2018-12-21 22:59:33 +00:00
|
|
|
|
|
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
|
|
|
|
|
{
|
2018-11-11 16:20:33 +00:00
|
|
|
|
var artist = this.GetArtist(id);
|
2018-11-11 17:27:13 +00:00
|
|
|
|
if (artist == null)
|
2018-11-11 16:20:33 +00:00
|
|
|
|
{
|
2018-12-24 19:40:49 +00:00
|
|
|
|
return Task.FromResult(new FileOperationResult<Image>(true, string.Format("Artist Not Found [{0}]", id)));
|
2018-11-11 16:20:33 +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
|
|
|
|
|
artistFolder = artist.ArtistFileFolder(this.Configuration, this.Configuration.LibraryFolder);
|
|
|
|
|
if (!Directory.Exists(artistFolder))
|
2018-11-22 17:31:59 +00:00
|
|
|
|
{
|
2018-12-26 21:18:51 +00:00
|
|
|
|
this.Logger.LogWarning($"Artist Folder [{ artistFolder }], Not Found For Artist `{ artist.ToString() }`");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var artistImages = Directory.GetFiles(artistFolder, "artist*.*");
|
|
|
|
|
if (artistImages.Any())
|
2018-11-24 01:46:12 +00:00
|
|
|
|
{
|
2018-12-26 21:18:51 +00:00
|
|
|
|
imageBytes = File.ReadAllBytes(artistImages.First());
|
2018-11-24 01:46:12 +00:00
|
|
|
|
}
|
2018-11-22 17:31:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
this.Logger.LogError(ex, $"Error Reading Folder [{ artistFolder }] For Artist [{ artist.Id }]");
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
};
|
2018-11-22 17:31:59 +00:00
|
|
|
|
if (imageBytes == null || !imageBytes.Any())
|
2018-11-11 16:20:33 +00:00
|
|
|
|
{
|
|
|
|
|
image = this.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)
|
|
|
|
|
{
|
2018-11-11 17:27:13 +00:00
|
|
|
|
this.Logger.LogError($"Error fetching Artist Thumbnail [{ id }]", ex);
|
2018-11-11 16:20:33 +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
|
|
|
|
|
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
|
|
|
|
|
{
|
2018-11-11 20:10:10 +00:00
|
|
|
|
var collection = this.GetCollection(id);
|
|
|
|
|
if (collection == null)
|
2018-11-11 01:11:58 +00:00
|
|
|
|
{
|
2018-12-24 19:40:49 +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
|
|
|
|
};
|
2018-11-11 20:10:10 +00:00
|
|
|
|
if (collection.Thumbnail == null || !collection.Thumbnail.Any())
|
2018-11-11 17:27:13 +00:00
|
|
|
|
{
|
2018-11-22 17:31:59 +00:00
|
|
|
|
image = this.DefaultNotFoundImages.Collection;
|
2018-11-11 01:11:58 +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)
|
|
|
|
|
{
|
2018-11-11 20:10:10 +00:00
|
|
|
|
this.Logger.LogError($"Error fetching Collection Thumbnail [{ id }]", ex);
|
2018-11-11 01:11:58 +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
|
|
|
|
|
2018-11-21 18:19:38 +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
|
|
|
|
{
|
|
|
|
|
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>(),
|
2018-11-21 18:19:38 +00:00
|
|
|
|
ContentType = contentType,
|
2018-11-11 16:20:33 +00:00
|
|
|
|
LastModified = (image.LastUpdated ?? image.CreatedDate),
|
|
|
|
|
ETag = imageEtag
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 17:27:13 +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)
|
|
|
|
|
{
|
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
var result = (await this.CacheManager.GetAsync($"urn:{ type }_by_id_operation:{id}", action, regionUrn)).Adapt<FileOperationResult<Image>>();
|
|
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return new FileOperationResult<Image>(result.IsNotFoundResult, result.Messages);
|
|
|
|
|
}
|
|
|
|
|
if (result.ETag == etag)
|
|
|
|
|
{
|
|
|
|
|
return new FileOperationResult<Image>(OperationMessages.NotModified);
|
|
|
|
|
}
|
|
|
|
|
if ((width.HasValue || height.HasValue) && result?.Data?.Bytes != null)
|
|
|
|
|
{
|
|
|
|
|
result.Data.Bytes = ImageHelper.ResizeImage(result?.Data?.Bytes, width.Value, height.Value);
|
|
|
|
|
result.ETag = EtagHelper.GenerateETag(this.HttpEncoder, result.Data.Bytes);
|
|
|
|
|
result.LastModified = DateTime.UtcNow;
|
2018-11-21 15:22:55 +00:00
|
|
|
|
if (width.Value != this.Configuration.ThumbnailImageSize.Width || height.Value != this.Configuration.ThumbnailImageSize.Height)
|
2018-11-11 17:27:13 +00:00
|
|
|
|
{
|
2018-11-25 20:43:52 +00:00
|
|
|
|
this.Logger.LogTrace($"{ type }: Resized [{ id }], Width [{ width.Value }], Height [{ height.Value }]");
|
2018-11-11 17:27:13 +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-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
|
|
|
|
|
{
|
|
|
|
|
var image = this.DbContext.Images
|
|
|
|
|
.Include("Release")
|
|
|
|
|
.Include("Artist")
|
|
|
|
|
.FirstOrDefault(x => x.RoadieId == id);
|
|
|
|
|
if (image == null)
|
|
|
|
|
{
|
2018-12-24 19:40:49 +00:00
|
|
|
|
return Task.FromResult(new FileOperationResult<Image>(true, string.Format("ImageById Not Found [{0}]", id)));
|
2018-11-11 17:27:13 +00:00
|
|
|
|
}
|
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)
|
|
|
|
|
{
|
|
|
|
|
this.Logger.LogError($"Error fetching Image [{ id }]", ex);
|
|
|
|
|
}
|
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
|
|
|
|
|
{
|
|
|
|
|
var label = this.GetLabel(id);
|
|
|
|
|
if (label == null)
|
|
|
|
|
{
|
2018-12-24 19:40:49 +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
|
|
|
|
|
};
|
|
|
|
|
if (label.Thumbnail == null || !label.Thumbnail.Any())
|
|
|
|
|
{
|
|
|
|
|
image = this.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)
|
|
|
|
|
{
|
|
|
|
|
this.Logger.LogError($"Error fetching Label Thumbnail [{ id }]", ex);
|
|
|
|
|
}
|
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-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
|
|
|
|
|
{
|
|
|
|
|
var playlist = this.GetPlaylist(id);
|
|
|
|
|
if (playlist == null)
|
|
|
|
|
{
|
2018-12-24 19:40:49 +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
|
|
|
|
|
};
|
|
|
|
|
if (playlist.Thumbnail == null || !playlist.Thumbnail.Any())
|
|
|
|
|
{
|
|
|
|
|
image = this.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)
|
|
|
|
|
{
|
|
|
|
|
this.Logger.LogError($"Error fetching Playlist Thumbnail [{ id }]", ex);
|
|
|
|
|
}
|
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-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
|
|
|
|
|
{
|
|
|
|
|
var release = this.GetRelease(id);
|
|
|
|
|
if (release == null)
|
|
|
|
|
{
|
2018-12-24 19:40:49 +00:00
|
|
|
|
return Task.FromResult(new FileOperationResult<Image>(true, string.Format("Release Not Found [{0}]", id)));
|
2018-11-11 20:10:10 +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
|
|
|
|
|
artistFolder = release.Artist.ArtistFileFolder(this.Configuration, this.Configuration.LibraryFolder);
|
2018-11-24 01:46:12 +00:00
|
|
|
|
if (!Directory.Exists(artistFolder))
|
|
|
|
|
{
|
2018-11-24 16:18:03 +00:00
|
|
|
|
this.Logger.LogWarning($"Artist Folder [{ artistFolder }], Not Found For Artist `{ release.Artist.ToString() }`");
|
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))
|
|
|
|
|
{
|
2018-11-24 16:18:03 +00:00
|
|
|
|
this.Logger.LogWarning($"Release Folder [{ releaseFolder }], Not Found For Release `{ release.ToString() }`");
|
2018-11-24 01:46:12 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var coverArtFiles = Directory.GetFiles(releaseFolder, "cover*.*");
|
|
|
|
|
if (coverArtFiles.Any())
|
|
|
|
|
{
|
|
|
|
|
imageBytes = File.ReadAllBytes(coverArtFiles.First());
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-22 17:31:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2018-11-24 16:18:03 +00:00
|
|
|
|
this.Logger.LogError(ex, $"Error Reading Release Folder [{ releaseFolder }] Artist Folder [{ artistFolder }] For Artist `{ release.Artist.Id }`");
|
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
|
|
|
|
|
};
|
|
|
|
|
if (release.Thumbnail == null || !release.Thumbnail.Any())
|
|
|
|
|
{
|
|
|
|
|
image = this.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)
|
|
|
|
|
{
|
|
|
|
|
this.Logger.LogError($"Error fetching Release Thumbnail [{ id }]", ex);
|
|
|
|
|
}
|
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-23 04:18:48 +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
|
|
|
|
|
{
|
|
|
|
|
var track = this.GetTrack(id);
|
|
|
|
|
if (track == null)
|
|
|
|
|
{
|
|
|
|
|
return new FileOperationResult<Image>(true, string.Format("Track Not Found [{0}]", id));
|
|
|
|
|
}
|
|
|
|
|
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
|
2018-11-23 04:18:48 +00:00
|
|
|
|
return await this.ReleaseImage(track.ReleaseMedia.Release.RoadieId, width, height, etag);
|
2018-11-11 20:10:10 +00:00
|
|
|
|
}
|
|
|
|
|
return GenerateFileOperationResult(id, image, etag);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
this.Logger.LogError($"Error fetching Track Thumbnail [{ id }]", ex);
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
var user = this.GetUser(id);
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
2018-12-24 19:40:49 +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
|
|
|
|
|
};
|
|
|
|
|
if (user.Avatar == null || !user.Avatar.Any())
|
|
|
|
|
{
|
|
|
|
|
image = this.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)
|
|
|
|
|
{
|
|
|
|
|
this.Logger.LogError($"Error fetching User Thumbnail [{ id }]", ex);
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|