[backend/drive] Add option to reject images that exceed the maximum media processing resolution

This commit is contained in:
Laura Hausmann 2024-08-08 18:21:25 +02:00
parent f903a1d8a4
commit 1d31553199
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 19 additions and 6 deletions

View file

@ -203,9 +203,10 @@ public sealed class Config
public ImagePipelineSection ImagePipeline { get; init; } = new();
public readonly int MaxFileSizeBytes = 10 * 1024 * 1024;
public Enums.ImageProcessor ImageProcessor { get; init; } = Enums.ImageProcessor.ImageSharp;
public int MaxResolutionMpx { get; init; } = 30;
public bool LocalOnly { get; init; } = false;
public Enums.ImageProcessor ImageProcessor { get; init; } = Enums.ImageProcessor.ImageSharp;
public int MaxResolutionMpx { get; init; } = 30;
public bool LocalOnly { get; init; } = false;
public bool FailIfImageExceedsMaxRes { get; init; } = false;
[Range(0, 128)] public int ImageProcessorConcurrency { get; init; } = 8;

View file

@ -263,9 +263,16 @@ public class DriveService(
}
else if (ident.Width * ident.Height > storageConfig.Value.MediaProcessing.MaxResolutionPx)
{
var config = storageConfig.Value.MediaProcessing;
if (config.FailIfImageExceedsMaxRes)
{
// @formatter:off
throw GracefulException.UnprocessableEntity($"Image is larger than {config.MaxResolutionMpx}mpx. Please resize your image to fit within the allowed dimensions.");
// @formatter:on
}
logger.LogDebug("Image is larger than {mpx}mpx ({width}x{height}), bypassing image processing...",
storageConfig.Value.MediaProcessing.MaxResolutionMpx, ident.Width,
ident.Height);
config.MaxResolutionMpx, ident.Width, ident.Height);
skipImageProcessing = true;
}

View file

@ -158,9 +158,14 @@ MaxFileSize = 10M
;; Maximum resolution for files to be considered for image processing, in megapixels
;; Note that processing an image requires up to 4MB of system memory per megapixel, in some edge case scenarios.
;; Caution: metadata (e.g. location data) for locally originating images will *not* be stripped for files larger than this
;; Caution: metadata (e.g. location data) for locally originating images will *not* be stripped for files larger than this.
;; If this is unwanted behavior, enable FailIfImageExceedsMaxRes.
MaxResolutionMpx = 30
;; Should you prefer to reject locally originating images that exceed MaxResolutionMpx, set this option to true.
;; Note that this does not apply to remote images, or to local images in a format not supported by the configured image processor.
FailIfImageExceedsMaxRes = false
;; Maxmimum concurrent image encode tasks to run. (0 = no limit)
ImageProcessorConcurrency = 8