using System; using System.Globalization; using System.IO; using System.Reflection; using System.Threading.Tasks; using gehGassi.Core.Interfaces; using gehGassi.Domain.Users; using gehGassi.Web.Helper; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Localization; using Microsoft.Extensions.Options; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats.Jpeg; using SixLabors.ImageSharp.Processing; namespace gehGassi.Web.Controllers { /// /// Basis-Controller /// //[Localize] public class BaseController : Controller { private IUserService _userService; protected IUserService UserService => _userService ??= HttpContext?.RequestServices.GetService(); private IFileService _fileService; protected IFileService FileService => _fileService ??= HttpContext?.RequestServices.GetService(); private IOptions _licenseOptions; protected IOptions LicenseOptions => _licenseOptions ??= HttpContext?.RequestServices.GetService>(); private IOptions _sessionSettings; protected IOptions SessionSettings => _sessionSettings ??= HttpContext?.RequestServices.GetService>(); private IStringLocalizerFactory _stringLocalizerFactory; protected IStringLocalizerFactory LocalizerFacory => _stringLocalizerFactory ?? HttpContext?.RequestServices.GetService(); private IOptions _localizationOptions; protected IOptions LocalizationOptions => _localizationOptions ??= HttpContext?.RequestServices.GetService>(); /// /// Aktueller Benutzer /// internal ApplicationUser ApplicationUser; /// /// Gewählte Sprache /// internal string SelectedLanguage; /// /// Fallback Sprache /// internal string FallbackLanguage; /// /// Stringlocalizer der Zugriff auf Annotations hast /// internal IStringLocalizer AnnotationsLocalizer; /// /// Erstellt eine Instanz /// public BaseController() { } /// /// /// /// public override void OnActionExecuting(ActionExecutingContext context) { base.OnActionExecuting(context); var version = Assembly.GetEntryAssembly().GetName().Version.ToString(); ViewBag.Version = version; if (context.HttpContext.User.Identity.IsAuthenticated) { ApplicationUser = UserService.GetByUsernameAsync(context.HttpContext.User.Identity.Name).Result; ViewBag.ApplicationUser = ApplicationUser; ViewBag.SessionSettings = SessionSettings; } SelectedLanguage = CultureInfo.CurrentCulture.TwoLetterISOLanguageName; FallbackLanguage = LocalizationOptions.Value.DefaultCulture; AnnotationsLocalizer = LocalizerFacory.Create("Annotations", "Localization"); ViewBag.SelectedLanguage = SelectedLanguage; ViewBag.FallbackLanguage = FallbackLanguage; ViewBag.AnnotationsLocalizer = AnnotationsLocalizer; } #region Images /// /// Erstellt ein Thumbnail /// /// Container /// Dateiname /// Größe für Breite und Höhe /// Task internal async Task GenerateThumbnail(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(size, size)); var format = img.DetectEncoder(filename); if (format is JpegEncoder) { format = new JpegEncoder() { Quality = 100 }; } await using (var memStream = new MemoryStream()) { await img.SaveAsync(memStream, format); memStream.Position = 0; await FileService.StoreAsync(container, $"thumbnails/{size}/{filename}", memStream); } } } } catch { } } /// /// Erstellt ein Thumbnail basierend auf der Breite /// /// Container /// Dateiname /// Größe für Breite und Höhe /// Task 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); if (format is JpegEncoder) { format = new JpegEncoder() { Quality = 100 }; } await using (var memStream = new MemoryStream()) { await img.SaveAsync(memStream, format); memStream.Position = 0; await FileService.StoreAsync(container, $"thumbnails/{size}/{filename}", memStream); } } } } catch { } } /// /// Löschen eines Thumbnails für ein Bild /// /// Container /// Dateiname /// Größe für Breite und Höhe /// Task 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 { } } #endregion } }