diff options
| author | Jake Mannens <jake@asger.xyz> | 2023-08-29 15:47:37 +1000 |
|---|---|---|
| committer | Jake Mannens <jake@asger.xyz> | 2025-08-20 00:48:44 +1000 |
| commit | 98053c0fc6e27998ccb8e0ad75900d2b48d6f70e (patch) | |
| tree | b552085007432de023e8f24a0d7417aa17a5bd48 /Controllers | |
| parent | d4f0eae4dc54f356f296ff26aba006e69d21ec0b (diff) | |
Moved thumbnail generation from media controller to media service
Diffstat (limited to 'Controllers')
| -rw-r--r-- | Controllers/MediaController.cs | 40 |
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}")] |
