public static bool ShouldHandleRequest(HttpContext context, ImageflowMiddlewareOptions options) { // If the path is empty or null we don't handle it var pathValue = context.Request.Path; if (pathValue == null || !pathValue.HasValue) { return(false); } var path = pathValue.Value; if (path == null) { return(false); } // We handle image request extensions if (PathHelpers.IsImagePath(path)) { return(true); } // Don't do string parsing unless there are actually prefixes configured if (options.ExtensionlessPaths.Count == 0) { return(false); } // If there's no extension, then we can see if it's one of the prefixes we should handle var extension = Path.GetExtension(path); // If there's a non-image extension, we shouldn't handle the request if (!string.IsNullOrEmpty(extension)) { return(false); } // Return true if any of the prefixes match return(options.ExtensionlessPaths .Any(extensionlessPath => path.StartsWith(extensionlessPath.Prefix, extensionlessPath.PrefixComparison))); }
public async Task Invoke(HttpContext context) { var path = context.Request.Path; // Delegate to the diagnostics page if it is requested if (diagnosticsPage.MatchesPath(path.Value)) { await diagnosticsPage.Invoke(context); return; } // We only handle requests with an image extension, period. if (!PathHelpers.IsImagePath(path)) { await next.Invoke(context); return; } var imageJobInfo = new ImageJobInfo(context, options, blobProvider); if (!imageJobInfo.Authorized) { await NotAuthorized(context); return; } // If the file is definitely missing hand to the next middleware // Remote providers will fail late rather than make 2 requests if (!imageJobInfo.PrimaryBlobMayExist()) { await next.Invoke(context); return; } var memoryCacheEnabled = memoryCache != null && options.AllowMemoryCaching && imageJobInfo.NeedsCaching(); var diskCacheEnabled = diskCache != null && options.AllowDiskCaching && imageJobInfo.NeedsCaching(); var distributedCacheEnabled = distributedCache != null && options.AllowDistributedCaching && imageJobInfo.NeedsCaching(); var sqliteCacheEnabled = sqliteCache != null && options.AllowSqliteCaching && imageJobInfo.NeedsCaching(); string cacheKey = null; if (memoryCacheEnabled || diskCacheEnabled || distributedCacheEnabled | sqliteCacheEnabled) { cacheKey = await imageJobInfo.GetFastCacheKey(); if (context.Request.Headers.TryGetValue(HeaderNames.IfNoneMatch, out var etag) && cacheKey == etag) { context.Response.StatusCode = StatusCodes.Status304NotModified; context.Response.ContentLength = 0; context.Response.ContentType = null; return; } } try { if (sqliteCacheEnabled) { await ProcessWithSqliteCache(context, cacheKey, imageJobInfo); } else if (diskCacheEnabled) { await ProcessWithDiskCache(context, cacheKey, imageJobInfo); } else if (memoryCacheEnabled) { await ProcessWithMemoryCache(context, cacheKey, imageJobInfo); // ReSharper disable once ConditionIsAlwaysTrueOrFalse } else if (distributedCacheEnabled) { await ProcessWithDistributedCache(context, cacheKey, imageJobInfo); } else { await ProcessWithNoCache(context, imageJobInfo); } } catch (BlobMissingException e) { await NotFound(context, e); } }