88 lines
2.7 KiB
C#
88 lines
2.7 KiB
C#
using DomainResults.Common;
|
|
|
|
using DataProviders;
|
|
using DataProviders.Buckets;
|
|
using DataProviders.Collections;
|
|
|
|
using ImageProvider;
|
|
using ImageProvider.Fonts;
|
|
using Core.Abstractions;
|
|
|
|
namespace WeatherForecast.Services {
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public interface IImageService {
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="siteId"></param>
|
|
/// <param name="width"></param>
|
|
/// <param name="height"></param>
|
|
/// <param name="imageId"></param>
|
|
/// <returns></returns>
|
|
(BucketFile?, IDomainResult) Get(Guid siteId, int width, int height, Guid imageId);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class ImageService : ServiceBase<ImageService>, IImageService {
|
|
|
|
private readonly IImageBucketDataProvider _imageBucketDataProvider;
|
|
private readonly IImageProvider _imageProvider;
|
|
private readonly IContentDataProvider _contentDataProvider;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="logger"></param>
|
|
/// <param name="imageBucketDataProvider"></param>
|
|
/// <param name="imageProvider"></param>
|
|
public ImageService(
|
|
ILogger<ImageService> logger,
|
|
IImageBucketDataProvider imageBucketDataProvider,
|
|
IImageProvider imageProvider,
|
|
IContentDataProvider contentDataProvider
|
|
) : base(logger) {
|
|
_imageBucketDataProvider = imageBucketDataProvider;
|
|
_imageProvider = imageProvider;
|
|
_contentDataProvider = contentDataProvider;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="siteId"></param>
|
|
/// <param name="width"></param>
|
|
/// <param name="height"></param>
|
|
/// <param name="imageId"></param>
|
|
/// <returns></returns>
|
|
public (BucketFile?, IDomainResult) Get(Guid siteId, int width, int height, Guid imageId) {
|
|
try {
|
|
var (file, downloadResult) = _imageBucketDataProvider.Download(siteId, imageId);
|
|
if (!downloadResult.IsSuccess || file == null)
|
|
return IDomainResult.Failed<BucketFile?> ();
|
|
|
|
var (content, getContentResult) = _contentDataProvider.Get(siteId);
|
|
if (!getContentResult.IsSuccess || content == null)
|
|
return IDomainResult.Failed<BucketFile?>();
|
|
|
|
var (image, transformResult) = _imageProvider.ResizeAndWatermark(file.Bytes, width, height, FontsEnum.Montserrat, FontStylesEnum.Regular, content.First().SiteName);
|
|
if (!transformResult.IsSuccess || image == null)
|
|
return IDomainResult.Failed<BucketFile?>();
|
|
|
|
return IDomainResult.Success(new BucketFile(file.Name, image, "image/jpeg"));
|
|
}
|
|
catch (Exception ex) {
|
|
_logger.LogError(ex, "Unhandled exception");
|
|
return IDomainResult.Failed<BucketFile?> (ex.Message);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
}
|