summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJake Mannens <jake@asger.xyz>2023-08-29 13:02:41 +1000
committerJake Mannens <jake@asger.xyz>2025-08-20 00:48:44 +1000
commit49dc2dda8b6188de0a99ff5070d6280122143e70 (patch)
tree2bda1f99c18e04f4019ca8fa57bcfd3c2064c100
parent277642e460e2c1cb68952de86249f1525a9c1ab0 (diff)
Added width and height properties to media objects
-rw-r--r--Controllers/MediaController.cs6
-rw-r--r--Media.cs2
-rw-r--r--Pages/ViewMedia.razor3
-rw-r--r--Services/MediaService.cs13
4 files changed, 19 insertions, 5 deletions
diff --git a/Controllers/MediaController.cs b/Controllers/MediaController.cs
index 6e04670..674b406 100644
--- a/Controllers/MediaController.cs
+++ b/Controllers/MediaController.cs
@@ -56,13 +56,15 @@ public class MediaController : Controller {
if(w > image.Width || h > image.Height)
return BadRequest("Requested thumbnail size is larger than original media");
- int width = (int)(w is not null ? w : image.Width * h / image.Height);
+ #pragma warning disable CS8629
+ int width = (int)(w is not null ? w : image.Width * h / image.Height);
int height = (int)(h is not null ? h : image.Height * w / image.Width);
+ #pragma warning restore CS8629
var thumbPath = mediaService.GetPath(media, width, height);
if(!System.IO.File.Exists(thumbPath)) {
- image.Resize(new MagickGeometry(width, height));
+ image.Resize(width, height);
image.Write(thumbPath);
}
diff --git a/Media.cs b/Media.cs
index 47aa73b..d4f9b5b 100644
--- a/Media.cs
+++ b/Media.cs
@@ -11,6 +11,8 @@ public class Media : HBObject {
public string MimeType { get; set; }
public string? ShortDescription { get; set; }
public string? LongDescription { get; set; }
+ public int Width { get; set; }
+ public int Height { get; set; }
public virtual List<UploadedFile> UploadedFiles { get; set; } = new();
public bool IsIngest => Tags
diff --git a/Pages/ViewMedia.razor b/Pages/ViewMedia.razor
index 60ae43f..935b4ca 100644
--- a/Pages/ViewMedia.razor
+++ b/Pages/ViewMedia.razor
@@ -10,7 +10,7 @@
<link rel="stylesheet" type="text/css" href="@(nameof(HyperBooru)).styles.css"/>
<div id="content">
- <img src="/media/@(media.Guid)"/>
+ <img src="/media/@(media.Guid)" width=@media.Width height=@media.Height/>
<div id="metadata">
<div id="metadata-fileinfo">
@if(infoEditMode) {
@@ -30,6 +30,7 @@
<p>Title: <i>@(media.ShortDescription ?? "None")</i></p>
<p class="newlines">Description:<br/><i>@(media.LongDescription ?? "None")</i></p>
}
+ <p>Resolution: @(media.Width)x@(media.Height)</p>
<p class="heading">Upload history</p>
<hr/>
<table id="uploaded-files" class="data-table">
diff --git a/Services/MediaService.cs b/Services/MediaService.cs
index 460e0c7..a9bd678 100644
--- a/Services/MediaService.cs
+++ b/Services/MediaService.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using ImageMagick;
+using Microsoft.EntityFrameworkCore;
using MimeDetective;
using MimeDetective.Definitions;
using System.Security.Cryptography;
@@ -123,13 +124,17 @@ public class MediaService : IMediaService {
CreateTime = createTime
};
+ // Determine the MIME type
fileData.Seek(0, SeekOrigin.Begin);
var defs = inspector.Inspect(fileData);
-
var mime = defs.ByMimeType().FirstOrDefault()?.MimeType;
if(mime is null)
throw new MediaCreateException("Unsupported file type");
+ // Read the image with ImageMagick to determine the width and height
+ fileData.Seek(0, SeekOrigin.Begin);
+ using var magickImage = new MagickImage(fileData);
+
var media = db.Media
.FirstOrDefault(m => m.Checksum == hash);
@@ -138,6 +143,10 @@ public class MediaService : IMediaService {
.First(td => td.Guid == HBContext.IngestTag);
media = new() {
+ Checksum = hash,
+ MimeType = mime,
+ Width = magickImage.Width,
+ Height = magickImage.Height,
UploadedFiles = new() {
fileRecord
},