146 lines
5.2 KiB
C#
146 lines
5.2 KiB
C#
using AutoMapper;
|
|
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Web.Helper;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Options;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using System;
|
|
using SixLabors.ImageSharp;
|
|
using SixLabors.ImageSharp.Advanced;
|
|
using SixLabors.ImageSharp.Processing;
|
|
|
|
namespace gehGassi.Web.Controllers.WebsiteApi
|
|
{
|
|
/// <summary>
|
|
/// Basis-Controller für die Website-API
|
|
/// </summary>
|
|
public class WebSiteApiBaseController : Controller
|
|
{
|
|
public IMapper Mapper { get; }
|
|
public IOptions<LocalizationOptions> LocalizationOptions { get; }
|
|
public IUserService UserService { get; }
|
|
|
|
private IFileService _fileService;
|
|
protected IFileService FileService => _fileService ??= HttpContext?.RequestServices.GetService<IFileService>();
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="mapper">Instanz eines IMapper</param>
|
|
/// <param name="localizationOptions">Instanz von LocalizationOptions</param>
|
|
/// <param name="userService">Instanz eines IUserService</param>
|
|
public WebSiteApiBaseController(IMapper mapper, IOptions<LocalizationOptions> localizationOptions, IUserService userService)
|
|
{
|
|
Mapper = mapper;
|
|
LocalizationOptions = localizationOptions;
|
|
UserService = userService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt die Basis-Adresse für URLS zurück
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
internal string GetBaseAddress()
|
|
{
|
|
var baseAddress = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}";
|
|
#if DEBUG
|
|
baseAddress = "http://10.0.0.31:54763";
|
|
#endif
|
|
return baseAddress;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellt ein Thumbnail basierend auf der Breite
|
|
/// </summary>
|
|
/// <param name="container">Container</param>
|
|
/// <param name="filename">Dateiname</param>
|
|
/// <param name="size">Größe für Breite und Höhe</param>
|
|
/// <returns>Task</returns>
|
|
internal async Task GenerateThumbnailByWidth(string container, string filename, int size)
|
|
{
|
|
try
|
|
{
|
|
using (var imgStream = await FileService.GetAsStreamAsync(container, filename))
|
|
{
|
|
using (var img = await Image.LoadAsync(imgStream))
|
|
{
|
|
img.Mutate(x => x.Resize(new ResizeOptions() { Mode = ResizeMode.Max, Size = new Size(size) }));
|
|
|
|
var format = img.DetectEncoder(filename);
|
|
|
|
await using (var memStream = new MemoryStream())
|
|
{
|
|
await img.SaveAsync(memStream, format);
|
|
memStream.Position = 0;
|
|
await FileService.StoreAsync(container, $"thumbnails/{size}/{filename}", memStream);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen eines Thumbnails für ein Bild
|
|
/// </summary>
|
|
/// <param name="container">Container</param>
|
|
/// <param name="fileName">Dateiname</param>
|
|
/// <param name="size">Größe für Breite und Höhe</param>
|
|
/// <returns>Task</returns>
|
|
internal async Task RemoveThumbnail(string container, string fileName, int size)
|
|
{
|
|
try
|
|
{
|
|
var file = await FileService.GetAsync(container, $"thumbnails/{size}/{fileName}");
|
|
if (file != null)
|
|
{
|
|
await FileService.DeleteAsync(container, $"thumbnails/{size}/{fileName}");
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt den Offset zwischen der Server- und der Client-Zeit zurück
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected int GetClientDateOffset()
|
|
{
|
|
var compareDate = DateTimeOffset.UtcNow;
|
|
var dateOffset = 0;
|
|
|
|
try
|
|
{
|
|
if (Request.Headers.ContainsKey("clientTime"))
|
|
{
|
|
var requestHeader = Request.Headers["clientTime"];
|
|
|
|
DateTimeOffset clientDate;
|
|
if (DateTimeOffset.TryParse(requestHeader, null, System.Globalization.DateTimeStyles.RoundtripKind, out clientDate))
|
|
{
|
|
//var clientDate = DateTimeOffset.Parse(Request.Headers["clientTime"], null, System.Globalization.DateTimeStyles.RoundtripKind);
|
|
dateOffset = (int)(compareDate - clientDate).TotalSeconds;
|
|
}
|
|
else
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"ClientOffset header Problem: {Request.Headers["clientTime"]}");
|
|
}
|
|
}
|
|
|
|
//System.Diagnostics.Debug.WriteLine($"ClientOffset in seconds: {dateOffset}");
|
|
}
|
|
catch { }
|
|
|
|
return dateOffset;
|
|
}
|
|
}
|
|
}
|