Exemple #1
0
        private async Task ProcessWithNoCache(HttpContext context, ImageJobInfo info)
        {
            // If we're not caching, we should always use the modified date from source blobs as part of the etag

            var betterCacheKey = await info.GetExactCacheKey();

            if (context.Request.Headers.TryGetValue(HeaderNames.IfNoneMatch, out var etag) && betterCacheKey == etag)
            {
                context.Response.StatusCode    = StatusCodes.Status304NotModified;
                context.Response.ContentLength = 0;
                context.Response.ContentType   = null;
                return;
            }

            if (info.HasParams)
            {
                logger?.LogInformation($"Processing image {info.FinalVirtualPath} with params {info.CommandString}");

                var imageData = await info.ProcessUncached();

                var imageBytes  = imageData.ResultBytes;
                var contentType = imageData.ContentType;

                // write to stream
                context.Response.ContentType   = contentType;
                context.Response.ContentLength = imageBytes.Count;
                SetCachingHeaders(context, betterCacheKey);

                await context.Response.Body.WriteAsync(imageBytes.Array, imageBytes.Offset, imageBytes.Count);
            }
            else
            {
                logger?.LogInformation($"Proxying image {info.FinalVirtualPath} with params {info.CommandString}");

                var contentType = PathHelpers.ContentTypeForImageExtension(info.EstimatedFileExtension);
                await using var sourceStream = (await info.GetPrimaryBlob()).OpenRead();
                if (sourceStream.CanSeek)
                {
                    if (sourceStream.Length == 0)
                    {
                        throw new InvalidOperationException("Source blob has zero bytes.");
                    }
                    context.Response.ContentType   = contentType;
                    context.Response.ContentLength = sourceStream.Length;
                    SetCachingHeaders(context, betterCacheKey);
                    await sourceStream.CopyToAsync(context.Response.Body);
                }
                else
                {
                    context.Response.ContentType = contentType;
                    SetCachingHeaders(context, betterCacheKey);
                    await sourceStream.CopyToAsync(context.Response.Body);
                }
            }
        }
Exemple #2
0
        private async Task ProcessWithNoCache(HttpContext context, ImageJobInfo info)
        {
            // If we're not caching, we should always use the modified date from source blobs as part of the etag

            var betterCacheKey = await info.GetExactCacheKey();

            if (context.Request.Headers.TryGetValue(HeaderNames.IfNoneMatch, out var etag) && betterCacheKey == etag)
            {
                GlobalPerf.Singleton.IncrementCounter("etag_hit");
                context.Response.StatusCode    = StatusCodes.Status304NotModified;
                context.Response.ContentLength = 0;
                context.Response.ContentType   = null;
                return;
            }
            GlobalPerf.Singleton.IncrementCounter("etag_miss");
            if (info.HasParams)
            {
                logger?.LogInformation("Processing image {VirtualPath} with params {CommandString}", info.FinalVirtualPath, info.CommandString);
                GlobalPerf.Singleton.IncrementCounter("nocache_processed");
                var imageData = await info.ProcessUncached();

                var imageBytes  = imageData.ResultBytes;
                var contentType = imageData.ContentType;

                // write to stream
                context.Response.ContentType   = contentType;
                context.Response.ContentLength = imageBytes.Count;
                SetCachingHeaders(context, betterCacheKey);

                if (imageBytes.Array == null)
                {
                    throw new InvalidOperationException("Image job returned zero bytes.");
                }
                await context.Response.Body.WriteAsync(imageBytes.Array, imageBytes.Offset, imageBytes.Count);
            }
            else
            {
                logger?.LogInformation("Proxying image {VirtualPath} with params {CommandString}", info.FinalVirtualPath, info.CommandString);
                GlobalPerf.Singleton.IncrementCounter("nocache_proxied");
                await using var sourceStream = (await info.GetPrimaryBlob()).OpenRead();
                SetCachingHeaders(context, betterCacheKey);
                await MagicBytes.ProxyToStream(sourceStream, context.Response);
            }
        }