From 713867bbda8322a0938d6b8f0c850752622fdd42 Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Sun, 21 Jun 2026 21:02:44 +1000 Subject: v0.20a --- Services/MediaService.cs | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to 'Services/MediaService.cs') diff --git a/Services/MediaService.cs b/Services/MediaService.cs index a0521db..9974802 100644 --- a/Services/MediaService.cs +++ b/Services/MediaService.cs @@ -4,7 +4,6 @@ using ImageMagick; using Microsoft.EntityFrameworkCore; using MimeDetective; using MimeDetective.Definitions; -using System.Diagnostics.CodeAnalysis; using System.Security.Cryptography; using System.Text.RegularExpressions; @@ -78,13 +77,13 @@ public class MediaService : IMediaService { .ThenInclude(t => t.TagDefinition) .First(m => m.Guid == media.Guid); var ingestTag = db.TagDefinitions - .First(td => td.Guid == HBContext.IngestTag); + .First(td => td.Guid == InternalTag.IngestTag); if(ingest) { - if(!media.Tags.Select(t => t.TagDefinition.Guid).Contains(HBContext.IngestTag)) + if(!media.Tags.Select(t => t.TagDefinition.Guid).Contains(InternalTag.IngestTag)) media.Tags.Add(new(ingestTag)); } else { - media.Tags.RemoveAll(t => t.TagDefinition.Guid == HBContext.IngestTag); + media.Tags.RemoveAll(t => t.TagDefinition.Guid == InternalTag.IngestTag); } db.SaveChanges(); @@ -190,7 +189,7 @@ public class MediaService : IMediaService { if(media is null) { var ingestTagDef = db.TagDefinitions - .First(td => td.Guid == HBContext.IngestTag); + .First(td => td.Guid == InternalTag.IngestTag); media = new() { UploadedFiles = new() { @@ -299,23 +298,37 @@ public class MediaService : IMediaService { public Stream GetThumbnail(Guid mediaId, int? width, int? height) { if(width is null && height is null) - throw new ThumbnailException( - "Both width and height cannot be null!", - mediaId); + throw new ApiModels.ArgumentException("Both width and height cannot be null!"); var thumbPath = GetThumbnailPath(mediaId, width, height); if(File.Exists(thumbPath)) - return System.IO.File.OpenRead(thumbPath); + return File.OpenRead(thumbPath); if(!File.Exists(GetPath(mediaId))) throw new ObjectNotFoundException([ mediaId ]); using var image = new MagickImage(GetPath(mediaId)); - if(width > image.Width || height > image.Height) { + if(width > image.Width) width = (int) image.Width; + if(height > image.Height) height = (int) image.Height; + + // Crop the image if a different aspect ratio is specified + if(width is not null && height is not null) { + double requestedRatio = (double) width / (double) height; + double currentRatio = (double) image.Width / (double) image.Height; + + double w = image.Width; + double h = image.Height; + + if(requestedRatio < currentRatio) + w = w / currentRatio * requestedRatio; + if(requestedRatio > currentRatio) + h = h / requestedRatio * currentRatio; + + image.Extent((uint) w, (uint) h, Gravity.Center); } image.Thumbnail((uint) (width ?? -1), (uint) (height ?? -1)); -- cgit v1.3