diff options
| author | Jake Mannens <jake@asger.xyz> | 2026-06-21 21:02:44 +1000 |
|---|---|---|
| committer | Jake Mannens <jake@asger.xyz> | 2026-06-25 12:26:07 +1000 |
| commit | 713867bbda8322a0938d6b8f0c850752622fdd42 (patch) | |
| tree | 1f8c4e58480471aecdee37e3f734c4c1211e16b1 /Services | |
| parent | 79d383f6f48df0078e6ba13239f9c4c94fbc9ea6 (diff) | |
Diffstat (limited to 'Services')
| -rw-r--r-- | Services/FeedService.cs | 2 | ||||
| -rw-r--r-- | Services/MediaService.cs | 33 |
2 files changed, 24 insertions, 11 deletions
diff --git a/Services/FeedService.cs b/Services/FeedService.cs index 3744e73..112b3c0 100644 --- a/Services/FeedService.cs +++ b/Services/FeedService.cs @@ -116,7 +116,7 @@ public class FeedService : IFeedService { if(!includeNsfw) media = media - .Where(m => !TagsThatImply(db, HBContext.NsfwTag) + .Where(m => !TagsThatImply(db, InternalTag.NsfwTag) .Intersect(m.Tags.Select(t => t.TagDefinitionId)) .Any()); 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)); |
