2025-11-16 12:31:25 -06:00
|
|
|
|
using System.Configuration;
|
|
|
|
|
|
using LANCommander.Server.Data;
|
2024-08-04 18:44:33 -05:00
|
|
|
|
using LANCommander.Server.Data.Models;
|
2023-11-02 01:24:42 -05:00
|
|
|
|
using LANCommander.Helpers;
|
2024-06-20 00:06:58 -05:00
|
|
|
|
using Syncfusion.PdfToImageConverter;
|
|
|
|
|
|
using System.Net.Mime;
|
2025-02-01 02:21:34 -06:00
|
|
|
|
using AutoMapper;
|
2024-10-07 18:04:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-10-30 17:44:10 -05:00
|
|
|
|
using ZiggyCreatures.Caching.Fusion;
|
2024-11-05 19:22:59 -06:00
|
|
|
|
using SixLabors.ImageSharp;
|
|
|
|
|
|
using SixLabors.ImageSharp.Processing;
|
|
|
|
|
|
using SixLabors.ImageSharp.Formats.Jpeg;
|
|
|
|
|
|
using LANCommander.SDK.Enums;
|
|
|
|
|
|
using LANCommander.SDK.Extensions;
|
2025-02-04 02:31:23 -06:00
|
|
|
|
using LANCommander.Server.Services.Extensions;
|
2025-02-09 18:53:40 -06:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2025-02-01 02:21:34 -06:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-11-07 18:55:10 -06:00
|
|
|
|
using SixLabors.ImageSharp.PixelFormats;
|
2023-11-02 01:24:42 -05:00
|
|
|
|
|
2024-08-04 18:44:33 -05:00
|
|
|
|
namespace LANCommander.Server.Services
|
2023-11-02 01:24:42 -05:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
public sealed class MediaService(
|
2025-09-29 19:49:09 -05:00
|
|
|
|
ILogger<MediaService> logger,
|
2025-11-16 12:31:25 -06:00
|
|
|
|
SettingsProvider<Settings.Settings> settingsProvider,
|
2025-02-01 02:21:34 -06:00
|
|
|
|
IFusionCache cache,
|
|
|
|
|
|
IMapper mapper,
|
2025-02-09 18:53:40 -06:00
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
2025-02-01 02:21:34 -06:00
|
|
|
|
IDbContextFactory<DatabaseContext> contextFactory,
|
2025-11-16 12:31:25 -06:00
|
|
|
|
StorageLocationService storageLocationService) : BaseDatabaseService<Media>(logger, settingsProvider, cache, mapper, httpContextAccessor, contextFactory)
|
2023-11-02 01:24:42 -05:00
|
|
|
|
{
|
2025-02-23 08:49:43 -06:00
|
|
|
|
public override async Task<Media> AddAsync(Media entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
await cache.ExpireGameCacheAsync(entity.GameId);
|
|
|
|
|
|
|
|
|
|
|
|
return await base.AddAsync(entity, async context =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await context.UpdateRelationshipAsync(m => m.Game);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(m => m.Parent);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(m => m.StorageLocation);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-02 14:58:07 -06:00
|
|
|
|
public override async Task<Media> UpdateAsync(Media entity)
|
2025-02-01 02:21:34 -06:00
|
|
|
|
{
|
2025-02-04 02:31:23 -06:00
|
|
|
|
await cache.ExpireGameCacheAsync(entity.GameId);
|
|
|
|
|
|
|
2025-02-23 08:49:43 -06:00
|
|
|
|
return await base.UpdateAsync(entity, async context =>
|
2025-02-02 14:58:07 -06:00
|
|
|
|
{
|
2025-02-23 08:49:43 -06:00
|
|
|
|
await context.UpdateRelationshipAsync(m => m.Game);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(m => m.Parent);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(m => m.StorageLocation);
|
2025-02-02 14:58:07 -06:00
|
|
|
|
});
|
2025-02-01 02:21:34 -06:00
|
|
|
|
}
|
2025-09-10 02:35:22 -05:00
|
|
|
|
|
2024-11-05 19:22:59 -06:00
|
|
|
|
|
2025-02-04 02:31:23 -06:00
|
|
|
|
public override async Task DeleteAsync(Media entity)
|
2023-11-02 01:24:42 -05:00
|
|
|
|
{
|
2024-11-05 19:22:59 -06:00
|
|
|
|
DeleteLocalMediaFile(entity);
|
2025-02-04 02:31:23 -06:00
|
|
|
|
|
|
|
|
|
|
await cache.ExpireGameCacheAsync(entity.GameId);
|
2023-11-02 01:24:42 -05:00
|
|
|
|
|
2025-02-04 02:31:23 -06:00
|
|
|
|
await base.DeleteAsync(entity);
|
2023-11-02 01:24:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-02 02:24:07 +02:00
|
|
|
|
public override async Task DeleteRangeAsync(IEnumerable<Media> entities)
|
|
|
|
|
|
{
|
|
|
|
|
|
DeleteLocalMediaFiles(entities);
|
|
|
|
|
|
|
|
|
|
|
|
var gameIds = entities.Select(x => x.GameId).Distinct();
|
|
|
|
|
|
var expirationTasks = gameIds.Select(gameId => cache.ExpireGameCacheAsync(gameId));
|
|
|
|
|
|
await Task.WhenAll(expirationTasks);
|
|
|
|
|
|
|
|
|
|
|
|
await base.DeleteRangeAsync(entities);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-25 22:29:56 -06:00
|
|
|
|
public static bool FileExists(Media entity)
|
2023-11-02 01:24:42 -05:00
|
|
|
|
{
|
2024-11-05 19:38:59 -06:00
|
|
|
|
var path = GetMediaPath(entity);
|
2023-11-02 01:24:42 -05:00
|
|
|
|
|
|
|
|
|
|
return File.Exists(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public async Task<bool> FileExistsAsync(Guid id)
|
2023-11-02 01:24:42 -05:00
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
var path = await GetMediaPathAsync(id);
|
2023-11-02 01:24:42 -05:00
|
|
|
|
|
|
|
|
|
|
return File.Exists(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-05 19:39:32 -06:00
|
|
|
|
public bool ThumbnailExists(Media entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
var path = GetThumbnailPath(entity);
|
|
|
|
|
|
|
|
|
|
|
|
return File.Exists(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public async Task<string> GetMediaPathAsync(Guid id)
|
2023-11-02 01:24:42 -05:00
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
var entity = await GetAsync(id);
|
2023-11-02 01:24:42 -05:00
|
|
|
|
|
2024-11-05 19:38:59 -06:00
|
|
|
|
return GetMediaPath(entity);
|
2023-11-02 01:24:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-05 19:38:59 -06:00
|
|
|
|
public static string GetMediaPath(Media entity)
|
2023-11-02 01:24:42 -05:00
|
|
|
|
{
|
2024-10-11 01:09:12 -05:00
|
|
|
|
return Path.Combine(entity.StorageLocation.Path, entity.FileId.ToString());
|
2023-11-02 01:24:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-23 00:25:23 -06:00
|
|
|
|
public static string GetMediaPath(Media entity, StorageLocation storageLocation)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Path.Combine(storageLocation.Path, entity.FileId.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public async Task<string> GetThumbnailPathAsync(Guid id)
|
2024-11-07 18:54:46 -06:00
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
var entity = await GetAsync(id);
|
2024-11-07 18:54:46 -06:00
|
|
|
|
|
|
|
|
|
|
return GetThumbnailPath(entity);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetThumbnailPath(Media media)
|
|
|
|
|
|
{
|
2025-11-16 12:31:25 -06:00
|
|
|
|
var config = _settingsProvider.CurrentValue.Server.Media.GetMediaTypeConfig(media.Type);
|
2025-09-10 02:35:22 -05:00
|
|
|
|
|
2025-09-15 19:19:52 -05:00
|
|
|
|
if (config != null && config.Thumbnails.Enabled)
|
2024-11-07 18:54:46 -06:00
|
|
|
|
return $"{GetMediaPath(media)}.Thumb";
|
|
|
|
|
|
else
|
|
|
|
|
|
return GetMediaPath(media);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-07 18:04:26 -05:00
|
|
|
|
/*public async Task<Media> UploadMediaAsync(Stream stream, Media media)
|
2023-11-02 23:18:19 -05:00
|
|
|
|
{
|
2024-10-07 18:04:26 -05:00
|
|
|
|
return await UploadMediaAsync(stream, media);
|
|
|
|
|
|
// return await UploadMediaAsync(file.OpenReadStream(maxAllowedSize: Settings.Media.MaxSize * 1024 * 1024), media);
|
|
|
|
|
|
}*/
|
2024-08-23 01:05:31 -05:00
|
|
|
|
|
2025-03-29 17:58:47 -05:00
|
|
|
|
public async Task<Media> WriteToFileAsync(Media media, Stream stream, bool overwrite = false)
|
2024-08-23 01:05:31 -05:00
|
|
|
|
{
|
2025-03-29 17:58:47 -05:00
|
|
|
|
if (media.StorageLocation == null)
|
|
|
|
|
|
media.StorageLocation = await storageLocationService.GetAsync(media.StorageLocationId);
|
|
|
|
|
|
|
|
|
|
|
|
if (media.StorageLocation == null)
|
2025-11-21 13:15:57 -06:00
|
|
|
|
media.StorageLocation = await storageLocationService.DefaultAsync(StorageLocationType.Media);
|
2025-03-29 17:58:47 -05:00
|
|
|
|
|
|
|
|
|
|
if (media.FileId == Guid.Empty)
|
|
|
|
|
|
media.FileId = Guid.NewGuid();
|
|
|
|
|
|
|
2024-11-05 19:38:59 -06:00
|
|
|
|
var path = GetMediaPath(media);
|
2025-03-29 17:58:47 -05:00
|
|
|
|
|
2025-12-03 18:30:46 -06:00
|
|
|
|
if (!String.IsNullOrWhiteSpace(path))
|
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
|
|
|
|
|
|
2025-03-29 17:58:47 -05:00
|
|
|
|
if (overwrite && File.Exists(path))
|
|
|
|
|
|
File.Delete(path);
|
2023-11-02 23:18:19 -05:00
|
|
|
|
|
2025-03-29 17:58:47 -05:00
|
|
|
|
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))
|
2025-01-21 21:37:43 -06:00
|
|
|
|
{
|
|
|
|
|
|
await stream.CopyToAsync(fs);
|
|
|
|
|
|
}
|
2025-03-29 17:58:47 -05:00
|
|
|
|
|
2025-09-27 16:39:54 -05:00
|
|
|
|
media.Crc32 = await SDK.Services.MediaClient.CalculateChecksumAsync(path);
|
2024-02-20 17:58:49 -06:00
|
|
|
|
|
2024-11-05 19:22:59 -06:00
|
|
|
|
await GenerateThumbnailAsync(media);
|
2025-02-13 02:10:46 -06:00
|
|
|
|
|
|
|
|
|
|
if (media.Id != Guid.Empty)
|
|
|
|
|
|
media = await UpdateAsync(media);
|
|
|
|
|
|
else
|
|
|
|
|
|
media = await AddAsync(media);
|
2025-02-04 02:31:23 -06:00
|
|
|
|
|
2025-03-29 17:58:47 -05:00
|
|
|
|
await cache.ExpireGameCacheAsync(media.Game?.Id ?? media.GameId);
|
2024-11-05 19:22:59 -06:00
|
|
|
|
|
2024-02-20 17:58:49 -06:00
|
|
|
|
return media;
|
2023-11-02 23:18:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-05 19:22:59 -06:00
|
|
|
|
public async Task<string> GenerateThumbnailAsync(Media media, int quality = 75)
|
2024-06-20 00:06:58 -05:00
|
|
|
|
{
|
2024-11-07 02:16:57 -06:00
|
|
|
|
var source = GetMediaPath(media);
|
2024-11-05 19:22:59 -06:00
|
|
|
|
var destination = GetThumbnailPath(media);
|
2024-06-20 00:06:58 -05:00
|
|
|
|
|
2024-11-07 02:16:57 -06:00
|
|
|
|
if (!File.Exists(source))
|
|
|
|
|
|
return String.Empty;
|
2025-09-15 19:19:52 -05:00
|
|
|
|
|
2025-11-16 12:31:25 -06:00
|
|
|
|
var config = _settingsProvider.CurrentValue.Server.Media.GetMediaTypeConfig(media.Type);
|
2024-11-07 02:16:57 -06:00
|
|
|
|
|
2024-11-05 19:22:59 -06:00
|
|
|
|
Stream stream = null;
|
2024-06-20 00:06:58 -05:00
|
|
|
|
|
2024-11-05 19:22:59 -06:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (media.MimeType == MediaTypeNames.Application.Pdf)
|
|
|
|
|
|
{
|
2024-11-07 02:16:57 -06:00
|
|
|
|
using (var pdfStream = new FileStream(source, FileMode.Open, FileAccess.Read))
|
2024-11-05 19:22:59 -06:00
|
|
|
|
{
|
|
|
|
|
|
var converter = new PdfToImageConverter();
|
2024-06-20 00:06:58 -05:00
|
|
|
|
|
2024-11-07 02:16:42 -06:00
|
|
|
|
converter.Load(pdfStream);
|
2024-06-20 00:06:58 -05:00
|
|
|
|
|
2024-11-05 19:22:59 -06:00
|
|
|
|
stream = converter.Convert(0, false, true);
|
2024-06-20 00:06:58 -05:00
|
|
|
|
|
2024-11-05 19:22:59 -06:00
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
}
|
2024-06-20 00:06:58 -05:00
|
|
|
|
}
|
2024-11-05 19:22:59 -06:00
|
|
|
|
else
|
2024-06-20 00:06:58 -05:00
|
|
|
|
{
|
2024-11-07 02:16:57 -06:00
|
|
|
|
stream = new FileStream(source, FileMode.Open, FileAccess.Read);
|
2024-11-05 19:22:59 -06:00
|
|
|
|
}
|
2024-06-20 00:06:58 -05:00
|
|
|
|
|
2025-09-15 19:19:52 -05:00
|
|
|
|
if (config != null && config.Thumbnails.Enabled)
|
2024-11-05 19:22:59 -06:00
|
|
|
|
{
|
2024-11-07 18:55:10 -06:00
|
|
|
|
using (var image = await Image.LoadAsync<Rgba32>(stream))
|
2024-11-05 19:22:59 -06:00
|
|
|
|
{
|
2025-09-15 19:19:52 -05:00
|
|
|
|
int thumbsizeX = (int)Math.Clamp(image.Width * (config.Thumbnails.Scale / 100f), config.Thumbnails.MinSize.Width, config.Thumbnails.MaxSize.Width);
|
|
|
|
|
|
int thumbsizeY = (int)Math.Clamp(image.Height * (config.Thumbnails.Scale / 100f), config.Thumbnails.MinSize.Height, config.Thumbnails.MaxSize.Height);
|
2024-11-05 19:22:59 -06:00
|
|
|
|
var resizeOptions = new ResizeOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
Mode = ResizeMode.Max,
|
2025-04-20 23:44:28 +02:00
|
|
|
|
Size = new Size(thumbsizeX, thumbsizeY),
|
2024-11-05 19:22:59 -06:00
|
|
|
|
Sampler = KnownResamplers.Bicubic,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
image.Mutate(context => context.Resize(resizeOptions));
|
|
|
|
|
|
|
2025-01-12 02:54:41 -06:00
|
|
|
|
if (media.Type.ValueIsIn(MediaType.Icon, MediaType.Logo, MediaType.PageImage) && media.MimeType == MediaTypeNames.Image.Png && HasTransparentPixels(image))
|
2024-11-05 19:22:59 -06:00
|
|
|
|
{
|
|
|
|
|
|
await image.SaveAsPngAsync(destination);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
await image.SaveAsJpegAsync(destination, new JpegEncoder
|
|
|
|
|
|
{
|
|
|
|
|
|
Quality = quality
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-06-20 00:06:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-11-07 02:17:08 -06:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
_logger?.LogError(ex, "Could not generate thumbnail for media with ID {MediaId}", media.Id);
|
2024-11-07 02:17:08 -06:00
|
|
|
|
}
|
2024-11-05 19:22:59 -06:00
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
if (stream is not null)
|
|
|
|
|
|
await stream.DisposeAsync();
|
|
|
|
|
|
}
|
2024-06-20 00:06:58 -05:00
|
|
|
|
|
2024-11-05 19:22:59 -06:00
|
|
|
|
return destination;
|
|
|
|
|
|
}
|
2024-06-20 00:06:58 -05:00
|
|
|
|
|
2024-11-07 18:55:10 -06:00
|
|
|
|
private bool HasTransparentPixels(Image<Rgba32> image)
|
2024-11-05 19:22:59 -06:00
|
|
|
|
{
|
2024-11-07 18:55:10 -06:00
|
|
|
|
var hasTransparentPixels = false;
|
|
|
|
|
|
|
|
|
|
|
|
image.ProcessPixelRows(accessor =>
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int y = 0; y < accessor.Height; y++)
|
|
|
|
|
|
{
|
|
|
|
|
|
Span<Rgba32> pixelRow = accessor.GetRowSpan(y);
|
|
|
|
|
|
|
|
|
|
|
|
for (int x = 0; x < pixelRow.Length; x++)
|
|
|
|
|
|
{
|
|
|
|
|
|
ref Rgba32 pixel = ref pixelRow[x];
|
|
|
|
|
|
|
|
|
|
|
|
if (pixel.A < 255)
|
|
|
|
|
|
{
|
|
|
|
|
|
hasTransparentPixels = true;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return hasTransparentPixels;
|
2024-06-20 00:06:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-11 01:09:12 -05:00
|
|
|
|
public void DeleteLocalMediaFile(Media media)
|
2023-11-02 19:27:50 -05:00
|
|
|
|
{
|
2024-11-05 19:38:59 -06:00
|
|
|
|
FileHelpers.DeleteIfExists(GetMediaPath(media));
|
2024-11-05 19:22:59 -06:00
|
|
|
|
FileHelpers.DeleteIfExists(GetThumbnailPath(media));
|
2023-11-02 19:27:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-02 02:24:07 +02:00
|
|
|
|
public void DeleteLocalMediaFiles(IEnumerable<Media> medias)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var media in medias)
|
|
|
|
|
|
{
|
|
|
|
|
|
DeleteLocalMediaFile(media);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-21 23:42:16 -06:00
|
|
|
|
public async Task<Media> DownloadMediaAsync(string sourceUrl, Media media)
|
2023-11-02 01:24:42 -05:00
|
|
|
|
{
|
|
|
|
|
|
using (var http = new HttpClient())
|
|
|
|
|
|
{
|
2024-11-05 19:22:59 -06:00
|
|
|
|
var response = await http.GetStreamAsync(sourceUrl);
|
2024-06-27 17:32:36 -05:00
|
|
|
|
|
2025-03-29 20:10:35 -05:00
|
|
|
|
return await WriteToFileAsync(media, response);
|
2023-11-02 01:24:42 -05:00
|
|
|
|
}
|
2024-02-21 23:42:16 -06:00
|
|
|
|
}
|
2025-01-21 20:43:40 -06:00
|
|
|
|
|
|
|
|
|
|
public async Task<StorageLocation> GetDefaultStorageLocationAsync()
|
|
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
var defaultStorageLocation = await storageLocationService.FirstOrDefaultAsync(l => l.Type == StorageLocationType.Media && l.Default);
|
2025-01-21 20:43:40 -06:00
|
|
|
|
|
|
|
|
|
|
return defaultStorageLocation;
|
|
|
|
|
|
}
|
2023-11-02 01:24:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|