summaryrefslogtreecommitdiff
path: root/Controllers/MediaController.cs
diff options
context:
space:
mode:
authorJake Mannens <jake@asger.xyz>2023-08-29 15:47:37 +1000
committerJake Mannens <jake@asger.xyz>2023-08-29 15:47:37 +1000
commitcb8c17917d0363f09eeaa3c379617dc213b2dacf (patch)
tree9c3c1c15cb6925b31fb9ef3eb93b66fb9e02a5ac /Controllers/MediaController.cs
parentd3b8abea30cda6127e9318f7762df175d5f6d459 (diff)
Moved thumbnail generation from media controller to media service
Diffstat (limited to 'Controllers/MediaController.cs')
-rw-r--r--Controllers/MediaController.cs40
1 files changed, 9 insertions, 31 deletions
diff --git a/Controllers/MediaController.cs b/Controllers/MediaController.cs
index 674b406..85dfc65 100644
--- a/Controllers/MediaController.cs
+++ b/Controllers/MediaController.cs
@@ -38,39 +38,17 @@ public class MediaController : Controller {
[HttpGet("thumb/{mediaId}")]
public IActionResult Thumbnail(
[FromRoute] Guid mediaId,
- [FromQuery] int? w,
- [FromQuery] int? h) {
+ [FromQuery(Name = "w")] int? width,
+ [FromQuery(Name = "h")] int? height) {
- var media = db.Media.First(m => m.Guid == mediaId);
- if(media is null)
- return NotFound();
-
- if(media.MimeType.Split("/")[0] != "image")
- return BadRequest("Media object not an image");
-
- using var image = new MagickImage(mediaService.GetPath(media));
-
- if(w is null && h is null)
- return BadRequest("Both width and height cannot be null!");
-
- if(w > image.Width || h > image.Height)
- return BadRequest("Requested thumbnail size is larger than original media");
-
- #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(width, height);
- image.Write(thumbPath);
+ try {
+ var thumb = mediaService.GetThumbnail(mediaId, width, height);
+ return new FileStreamResult(thumb, "image/jpeg");
+ } catch(ThumbnailException e) {
+ return BadRequest(e.Message);
+ } catch(ObjectNotFoundException e) {
+ return NotFound(e.Message);
}
-
- var fs = System.IO.File.OpenRead(thumbPath);
-
- return new FileStreamResult(fs, "image/jpeg");
}
[HttpDelete("{mediaId}")]