mirror of https://github.com/ErsatzTV/ErsatzTV.git
17 changed files with 218 additions and 64 deletions
@ -1,9 +1,8 @@ |
|||||||
namespace ErsatzTV.Application.MediaItems |
namespace ErsatzTV.Application.MediaItems |
||||||
{ |
{ |
||||||
public record AggregateMediaItemViewModel( |
public record AggregateMediaItemViewModel( |
||||||
int MediaItemId, |
|
||||||
string Title, |
string Title, |
||||||
string Subtitle, |
string Subtitle, |
||||||
string SortTitle, |
string SortTitle, |
||||||
bool HasPoster); |
string Poster); |
||||||
} |
} |
||||||
|
|||||||
@ -1,4 +1,4 @@ |
|||||||
namespace ErsatzTV.Core.AggregateModels |
namespace ErsatzTV.Core.AggregateModels |
||||||
{ |
{ |
||||||
public record MediaItemSummary(string Title, string SortTitle, string Subtitle, string PosterPath, int MediaItemId); |
public record MediaItemSummary(string Title, string SortTitle, string Subtitle, string Poster); |
||||||
} |
} |
||||||
|
|||||||
@ -0,0 +1,11 @@ |
|||||||
|
using System.Threading.Tasks; |
||||||
|
using LanguageExt; |
||||||
|
|
||||||
|
namespace ErsatzTV.Core.Interfaces.Images |
||||||
|
{ |
||||||
|
public interface IImageCache |
||||||
|
{ |
||||||
|
Task<Either<BaseError, string>> ResizeAndSaveImage(byte[] imageBuffer, int? height, int? width); |
||||||
|
Task<Either<BaseError, string>> SaveImage(byte[] imageBuffer); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,64 @@ |
|||||||
|
using System; |
||||||
|
using System.IO; |
||||||
|
using System.Security.Cryptography; |
||||||
|
using System.Threading.Tasks; |
||||||
|
using ErsatzTV.Core; |
||||||
|
using ErsatzTV.Core.Interfaces.Images; |
||||||
|
using LanguageExt; |
||||||
|
using SixLabors.ImageSharp; |
||||||
|
using SixLabors.ImageSharp.Formats.Jpeg; |
||||||
|
using SixLabors.ImageSharp.Processing; |
||||||
|
|
||||||
|
namespace ErsatzTV.Infrastructure.Images |
||||||
|
{ |
||||||
|
public class ImageCache : IImageCache |
||||||
|
{ |
||||||
|
private static readonly SHA1CryptoServiceProvider Crypto; |
||||||
|
|
||||||
|
static ImageCache() => Crypto = new SHA1CryptoServiceProvider(); |
||||||
|
|
||||||
|
public async Task<Either<BaseError, string>> ResizeAndSaveImage(byte[] imageBuffer, int? height, int? width) |
||||||
|
{ |
||||||
|
await using var inStream = new MemoryStream(imageBuffer); |
||||||
|
using var image = await Image.LoadAsync(inStream); |
||||||
|
|
||||||
|
Size size = height.HasValue ? new Size { Height = height.Value } : new Size { Width = width.Value }; |
||||||
|
|
||||||
|
image.Mutate( |
||||||
|
i => i.Resize( |
||||||
|
new ResizeOptions |
||||||
|
{ |
||||||
|
Mode = ResizeMode.Max, |
||||||
|
Size = size |
||||||
|
})); |
||||||
|
|
||||||
|
await using var outStream = new MemoryStream(); |
||||||
|
await image.SaveAsync(outStream, new JpegEncoder { Quality = 90 }); |
||||||
|
|
||||||
|
return await SaveImage(outStream.ToArray()); |
||||||
|
} |
||||||
|
|
||||||
|
public async Task<Either<BaseError, string>> SaveImage(byte[] imageBuffer) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
byte[] hash = Crypto.ComputeHash(imageBuffer); |
||||||
|
string hex = BitConverter.ToString(hash).Replace("-", string.Empty); |
||||||
|
|
||||||
|
string fileName = Path.Combine(FileSystemLayout.ImageCacheFolder, hex); |
||||||
|
|
||||||
|
if (!Directory.Exists(FileSystemLayout.ImageCacheFolder)) |
||||||
|
{ |
||||||
|
Directory.CreateDirectory(FileSystemLayout.ImageCacheFolder); |
||||||
|
} |
||||||
|
|
||||||
|
await File.WriteAllBytesAsync(fileName, imageBuffer); |
||||||
|
return hex; |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
return BaseError.New(ex.Message); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue