Exemplo n.º 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);
                }
            }
        }
Exemplo n.º 2
0
        private async Task ProcessWithMemoryCache(HttpContext context, string cacheKey, ImageJobInfo info)
        {
            var isCached            = memoryCache.TryGetValue(cacheKey, out ArraySegment <byte> imageBytes);
            var isContentTypeCached = memoryCache.TryGetValue(cacheKey + ".contentType", out string contentType);

            if (isCached && isContentTypeCached)
            {
                logger?.LogInformation("Serving {0}?{1} from memory cache", info.FinalVirtualPath, info.CommandString);
            }
            else
            {
                if (info.HasParams)
                {
                    logger?.LogInformation($"Memory Cache Miss: Processing image {info.FinalVirtualPath}?{info.CommandString}");

                    var imageData = await info.ProcessUncached();

                    imageBytes  = imageData.ResultBytes;
                    contentType = imageData.ContentType;
                }
                else
                {
                    logger?.LogInformation($"Memory Cache Miss: Proxying image {info.FinalVirtualPath}?{info.CommandString}");

                    contentType = PathHelpers.ContentTypeForImageExtension(info.EstimatedFileExtension);
                    imageBytes  = new ArraySegment <byte>(await info.GetPrimaryBlobBytesAsync());
                }

                // Set cache options.
                var cacheEntryOptions = new MemoryCacheEntryOptions()
                                        .SetSize(imageBytes.Count)
                                        .SetSlidingExpiration(options.MemoryCacheSlidingExpiration);

                var cacheEntryMetaOptions = new MemoryCacheEntryOptions()
                                            .SetSize(contentType.Length * 2)
                                            .SetSlidingExpiration(options.MemoryCacheSlidingExpiration);

                memoryCache.Set(cacheKey, imageBytes, cacheEntryOptions);
                memoryCache.Set(cacheKey + ".contentType", contentType, cacheEntryMetaOptions);
            }

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


            await context.Response.Body.WriteAsync(imageBytes.Array, imageBytes.Offset, imageBytes.Count);
        }
Exemplo n.º 3
0
        private async Task ProcessWithDistributedCache(HttpContext context, string cacheKey, ImageJobInfo info)
        {
            var imageBytes = await distributedCache.GetAsync(cacheKey);

            var contentType = await distributedCache.GetStringAsync(cacheKey + ".contentType");

            if (imageBytes != null && contentType != null)
            {
                logger?.LogInformation("Serving {0}?{1} from distributed cache", info.FinalVirtualPath, info.CommandString);
            }
            else
            {
                if (info.HasParams)
                {
                    logger?.LogInformation($"Distributed Cache Miss: Processing image {info.FinalVirtualPath}?{info.CommandString}");

                    var imageData = await info.ProcessUncached();

                    imageBytes = imageData.ResultBytes.Count != imageData.ResultBytes.Array?.Length
                        ? imageData.ResultBytes.ToArray()
                        : imageData.ResultBytes.Array;

                    contentType = imageData.ContentType;
                }
                else
                {
                    logger?.LogInformation($"Distributed Cache Miss: Proxying image {info.FinalVirtualPath}?{info.CommandString}");

                    contentType = PathHelpers.ContentTypeForImageExtension(info.EstimatedFileExtension);
                    imageBytes  = await info.GetPrimaryBlobBytesAsync();
                }

                // Set cache options.
                var cacheEntryOptions = new DistributedCacheEntryOptions()
                                        .SetSlidingExpiration(options.DistributedCacheSlidingExpiration);

                await distributedCache.SetAsync(cacheKey, imageBytes, cacheEntryOptions);

                await distributedCache.SetStringAsync(cacheKey + ".contentType", contentType, cacheEntryOptions);
            }

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

            await context.Response.Body.WriteAsync(imageBytes, 0, imageBytes.Length);
        }
Exemplo n.º 4
0
        private async Task ProcessWithDiskCache(HttpContext context, string cacheKey, ImageJobInfo info)
        {
            var cacheResult = await diskCache.GetOrCreate(cacheKey, info.EstimatedFileExtension, async (stream) =>
            {
                if (info.HasParams)
                {
                    logger?.LogInformation($"DiskCache Miss: Processing image {info.FinalVirtualPath}?{info}");


                    var result = await info.ProcessUncached();
                    await stream.WriteAsync(result.ResultBytes.Array, result.ResultBytes.Offset,
                                            result.ResultBytes.Count,
                                            CancellationToken.None);
                    await stream.FlushAsync();
                }
                else
                {
                    logger?.LogInformation($"DiskCache Miss: Proxying image {info.FinalVirtualPath}");
                    await info.CopyPrimaryBlobToAsync(stream);
                }
            });

            // Note that using estimated file extension instead of parsing magic bytes will lead to incorrect content-type
            // values when the source file has a mismatched extension.

            if (cacheResult.Data != null)
            {
                if (cacheResult.Data.Length < 1)
                {
                    throw new InvalidOperationException("DiskCache returned cache entry with zero bytes");
                }
                context.Response.ContentType   = PathHelpers.ContentTypeForImageExtension(info.EstimatedFileExtension);
                context.Response.ContentLength = cacheResult.Data.Length; //ReadOnlyMemoryStream, so it supports seeking
                SetCachingHeaders(context, cacheKey);
                await cacheResult.Data.CopyToAsync(context.Response.Body);
            }
            else
            {
                logger?.LogInformation("Serving {0}?{1} from disk cache {2}", info.FinalVirtualPath, info.CommandString, cacheResult.RelativePath);
                await ServeFileFromDisk(context, cacheResult.PhysicalPath, cacheKey,
                                        PathHelpers.ContentTypeForImageExtension(info.EstimatedFileExtension));
            }
        }
Exemplo n.º 5
0
        private async Task ProcessWithSqliteCache(HttpContext context, string cacheKey, ImageJobInfo info)
        {
            var cacheResult = await sqliteCache.GetOrCreate(cacheKey, async() =>
            {
                if (info.HasParams)
                {
                    logger?.LogInformation($"Sqlite Cache Miss: Processing image {info.FinalVirtualPath}?{info.CommandString}");

                    var imageData  = await info.ProcessUncached();
                    var imageBytes = imageData.ResultBytes.Count != imageData.ResultBytes.Array?.Length
                        ? imageData.ResultBytes.ToArray()
                        : imageData.ResultBytes.Array;

                    var contentType = imageData.ContentType;
                    return(new SqliteCacheEntry()
                    {
                        ContentType = contentType,
                        Data = imageBytes
                    });
                }
                else
                {
                    logger?.LogInformation($"Sqlite Cache Miss: Proxying image {info.FinalVirtualPath}?{info.CommandString}");

                    var contentType = PathHelpers.ContentTypeForImageExtension(info.EstimatedFileExtension);
                    return(new SqliteCacheEntry()
                    {
                        ContentType = contentType,
                        Data = await info.GetPrimaryBlobBytesAsync()
                    });
                }
            });


            // write to stream
            context.Response.ContentType   = cacheResult.ContentType;
            context.Response.ContentLength = cacheResult.Data.Length;
            SetCachingHeaders(context, cacheKey);

            await context.Response.Body.WriteAsync(cacheResult.Data, 0, cacheResult.Data.Length);
        }