Ejemplo n.º 1
0
 public void GifDecoder_DegenerateMemoryRequest_ShouldTranslateTo_ImageFormatException<TPixel>(TestImageProvider<TPixel> provider)
     where TPixel : unmanaged, IPixel<TPixel>
 {
     provider.LimitAllocatorBufferCapacity().InPixelsSqrt(10);
     InvalidImageContentException ex = Assert.Throws<InvalidImageContentException>(() => provider.GetImage(GifDecoder));
     Assert.IsType<InvalidMemoryOperationException>(ex.InnerException);
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Decodes the image from the specified stream.
 /// </summary>
 /// <typeparam name="TPixel">The pixel format.</typeparam>
 /// <param name="decoder">The decoder.</param>
 /// <param name="configuration">The configuration for the image.</param>
 /// <param name="stream">The <see cref="Stream"/> containing image data.</param>
 /// <param name="largeImageExceptionFactory">Factory method to handle <see cref="InvalidMemoryOperationException"/> as <see cref="InvalidImageContentException"/>.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
 public static Task <Image <TPixel> > DecodeAsync <TPixel>(
     this IImageDecoderInternals decoder,
     Configuration configuration,
     Stream stream,
     Func <InvalidMemoryOperationException, Size, InvalidImageContentException> largeImageExceptionFactory,
     CancellationToken cancellationToken)
     where TPixel : unmanaged, IPixel <TPixel>
 {
     try
     {
         using var bufferedReadStream = new BufferedReadStream(configuration, stream);
         Image <TPixel> image = decoder.Decode <TPixel>(bufferedReadStream, cancellationToken);
         return(Task.FromResult(image));
     }
     catch (InvalidMemoryOperationException ex)
     {
         InvalidImageContentException invalidImageContentException = largeImageExceptionFactory(ex, decoder.Dimensions);
         return(Task.FromException <Image <TPixel> >(invalidImageContentException));
     }
     catch (OperationCanceledException)
     {
         return(Task.FromCanceled <Image <TPixel> >(cancellationToken));
     }
     catch (Exception ex)
     {
         return(Task.FromException <Image <TPixel> >(ex));
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Reads the raw image information from the specified stream.
 /// </summary>
 /// <param name="decoder">The decoder.</param>
 /// <param name="configuration">The configuration for the image.</param>
 /// <param name="stream">The <see cref="Stream"/> containing image data.</param>
 /// <param name="tooLargeImageExceptionFactory">Factory method to handle <see cref="InvalidMemoryOperationException"/> as <see cref="InvalidImageContentException"/>.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
 /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
 public static Task <IImageInfo> IdentifyAsync(
     this IImageDecoderInternals decoder,
     Configuration configuration,
     Stream stream,
     Func <InvalidMemoryOperationException, Size, InvalidImageContentException> tooLargeImageExceptionFactory,
     CancellationToken cancellationToken)
 {
     try
     {
         using var bufferedReadStream = new BufferedReadStream(configuration, stream);
         IImageInfo imageInfo = decoder.Identify(bufferedReadStream, cancellationToken);
         return(Task.FromResult(imageInfo));
     }
     catch (InvalidMemoryOperationException ex)
     {
         InvalidImageContentException invalidImageContentException = tooLargeImageExceptionFactory(ex, decoder.Dimensions);
         return(Task.FromException <IImageInfo>(invalidImageContentException));
     }
     catch (OperationCanceledException)
     {
         return(Task.FromCanceled <IImageInfo>(cancellationToken));
     }
     catch (Exception ex)
     {
         return(Task.FromException <IImageInfo>(ex));
     }
 }
Ejemplo n.º 4
0
        public async Task DecodeAsnc_DegenerateMemoryRequest_ShouldTranslateTo_ImageFormatException <TPixel>(TestImageProvider <TPixel> provider)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            provider.LimitAllocatorBufferCapacity().InBytesSqrt(10);
            InvalidImageContentException ex = await Assert.ThrowsAsync <InvalidImageContentException>(() => provider.GetImageAsync(JpegDecoder));

            this.Output.WriteLine(ex.Message);
            Assert.IsType <InvalidMemoryOperationException>(ex.InnerException);
        }
Ejemplo n.º 5
0
        public ActionResult Error()
        {
            var context   = HttpContext.Features.Get <IExceptionHandlerFeature>();
            var exception = context?.Error;

            Response.StatusCode = exception switch
            {
                FormatException _ => 400,
                InvalidImageContentException _ => 400,
                                _ => 500
            };

            _logger.LogError(exception?.Message);

            return(Content(exception?.Message)); // Your error model
        }
    }