204 lines
7.3 KiB
C#
204 lines
7.3 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// Basis-Controller
|
|
/// </summary>
|
|
//[Localize]
|
|
public class BaseController : Controller
|
|
{
|
|
private IUserService _userService;
|
|
protected IUserService UserService => _userService ??= HttpContext?.RequestServices.GetService<IUserService>();
|
|
|
|
private IFileService _fileService;
|
|
protected IFileService FileService => _fileService ??= HttpContext?.RequestServices.GetService<IFileService>();
|
|
|
|
private IOptions<LicenseOptions> _licenseOptions;
|
|
protected IOptions<LicenseOptions> LicenseOptions => _licenseOptions ??= HttpContext?.RequestServices.GetService<IOptions<LicenseOptions>>();
|
|
|
|
private IOptions<SessionSettings> _sessionSettings;
|
|
protected IOptions<SessionSettings> SessionSettings => _sessionSettings ??= HttpContext?.RequestServices.GetService<IOptions<SessionSettings>>();
|
|
|
|
private IStringLocalizerFactory _stringLocalizerFactory;
|
|
protected IStringLocalizerFactory LocalizerFacory => _stringLocalizerFactory ?? HttpContext?.RequestServices.GetService<IStringLocalizerFactory>();
|
|
|
|
private IOptions<Helper.LocalizationOptions> _localizationOptions;
|
|
protected IOptions<Helper.LocalizationOptions> LocalizationOptions => _localizationOptions ??= HttpContext?.RequestServices.GetService<IOptions<Helper.LocalizationOptions>>();
|
|
|
|
/// <summary>
|
|
/// Aktueller Benutzer
|
|
/// </summary>
|
|
internal ApplicationUser ApplicationUser;
|
|
|
|
/// <summary>
|
|
/// Gewählte Sprache
|
|
/// </summary>
|
|
internal string SelectedLanguage;
|
|
|
|
/// <summary>
|
|
/// Fallback Sprache
|
|
/// </summary>
|
|
internal string FallbackLanguage;
|
|
|
|
/// <summary>
|
|
/// Stringlocalizer der Zugriff auf Annotations hast
|
|
/// </summary>
|
|
internal IStringLocalizer AnnotationsLocalizer;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
public BaseController()
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
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
|
|
|
|
/// <summary>
|
|
/// Erstellt ein Thumbnail
|
|
/// </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 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
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
/// <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);
|
|
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
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
/// <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
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|