示例#1
0
 public BlobFetchCache(string virtualPath, BlobProvider provider)
 {
     this.virtualPath = virtualPath;
     this.provider    = provider;
     resultFetched    = false;
     blobFetched      = false;
 }
示例#2
0
        public ImageflowMiddleware(
            RequestDelegate next,
            IWebHostEnvironment env,
            IEnumerable <ILogger <ImageflowMiddleware> > logger,
            IEnumerable <IMemoryCache> memoryCache,
            IEnumerable <IDistributedCache> distributedCache,
            IEnumerable <ISqliteCache> sqliteCaches,
            IEnumerable <IClassicDiskCache> diskCache,
            IEnumerable <IBlobProvider> blobProviders,
            ImageflowMiddlewareOptions options)
        {
            this.next             = next;
            this.options          = options;
            this.env              = env;
            this.logger           = logger.FirstOrDefault();
            this.memoryCache      = memoryCache.FirstOrDefault();
            this.diskCache        = diskCache.FirstOrDefault();
            this.distributedCache = distributedCache.FirstOrDefault();
            this.sqliteCache      = sqliteCaches.FirstOrDefault();
            var providers   = blobProviders.ToList();
            var mappedPaths = options.MappedPaths.ToList();

            if (options.MapWebRoot)
            {
                if (this.env?.WebRootPath == null)
                {
                    throw new InvalidOperationException("Cannot call MapWebRoot if env.WebRootPath is null");
                }
                mappedPaths.Add(new PathMapping("/", this.env.WebRootPath));
            }

            blobProvider    = new BlobProvider(providers, mappedPaths);
            diagnosticsPage = new DiagnosticsPage(env, this.logger, this.memoryCache, this.distributedCache, this.diskCache, providers);
        }
示例#3
0
        public ImageJobInfo(HttpContext context, ImageflowMiddlewareOptions options, BlobProvider blobProvider)
        {
            this.options = options;
            Authorized   = ProcessRewritesAndAuthorization(context, options);

            if (!Authorized)
            {
                return;
            }

            HasParams = PathHelpers.SupportedQuerystringKeys.Any(FinalQuery.ContainsKey);


            var extension = Path.GetExtension(FinalVirtualPath);

            if (FinalQuery.TryGetValue("format", out var newExtension))
            {
                extension = newExtension;
            }

            EstimatedFileExtension = PathHelpers.SanitizeImageExtension(extension);

            primaryBlob = new BlobFetchCache(FinalVirtualPath, blobProvider);
            allBlobs    = new List <BlobFetchCache>(1)
            {
                primaryBlob
            };

            if (HasParams)
            {
                CommandString = PathHelpers.SerializeCommandString(FinalQuery);

                // Look up watermark names
                if (FinalQuery.TryGetValue("watermark", out var watermarkValues))
                {
                    var watermarkNames = watermarkValues.Split(",").Select(s => s.Trim(' '));

                    appliedWatermarks = new List <NamedWatermark>();
                    foreach (var name in watermarkNames)
                    {
                        var watermark = options.NamedWatermarks.FirstOrDefault(w =>
                                                                               w.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
                        if (watermark == null)
                        {
                            throw new InvalidOperationException($"watermark {name} was referenced from the querystring but no watermark by that name is registered with the middleware");
                        }

                        appliedWatermarks.Add(watermark);
                        allBlobs.Add(new BlobFetchCache(watermark.VirtualPath, blobProvider));
                    }
                }
            }

            provider = blobProvider;
        }
        public ImageJobInfo(HttpContext context, ImageflowMiddlewareOptions options, BlobProvider blobProvider)
        {
            this.options = options;
            Authorized   = ProcessRewritesAndAuthorization(context, options);

            if (!Authorized)
            {
                return;
            }

            HasParams = PathHelpers.SupportedQuerystringKeys.Any(FinalQuery.ContainsKey);


            var extension = Path.GetExtension(FinalVirtualPath);

            if (FinalQuery.TryGetValue("format", out var newExtension))
            {
                extension = newExtension;
            }

            EstimatedFileExtension = PathHelpers.SanitizeImageExtension(extension) ?? "jpg";

            primaryBlob = new BlobFetchCache(FinalVirtualPath, blobProvider);
            allBlobs    = new List <BlobFetchCache>(1)
            {
                primaryBlob
            };

            appliedWatermarks = new List <NamedWatermark>();

            if (HasParams)
            {
                CommandString = PathHelpers.SerializeCommandString(FinalQuery);

                // Look up watermark names
                if (FinalQuery.TryGetValue("watermark", out var watermarkValues))
                {
                    var watermarkNames = watermarkValues.Split(",").Select(s => s.Trim(' '));
                    foreach (var name in watermarkNames)
                    {
                        var watermark = options.NamedWatermarks.FirstOrDefault(w =>
                                                                               w.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
                        if (watermark == null)
                        {
                            throw new InvalidOperationException(
                                      $"watermark {name} was referenced from the querystring but no watermark by that name is registered with the middleware");
                        }

                        appliedWatermarks.Add(watermark);
                    }
                }
            }

            // After we've populated the defaults, run the event handlers for custom watermarking logic
            var args = new WatermarkingEventArgs(context, FinalVirtualPath, FinalQuery, appliedWatermarks);

            foreach (var handler in options.Watermarking)
            {
                var matches = string.IsNullOrEmpty(handler.PathPrefix) ||
                              FinalVirtualPath.StartsWith(handler.PathPrefix, StringComparison.OrdinalIgnoreCase);
                if (matches)
                {
                    handler.Handler(args);
                }
            }
            appliedWatermarks = args.AppliedWatermarks;

            // Add the watermark source files
            foreach (var w in appliedWatermarks)
            {
                allBlobs.Add(new BlobFetchCache(w.VirtualPath, blobProvider));
            }

            provider = blobProvider;
        }
示例#5
0
        public ImageflowMiddleware(
            RequestDelegate next,
            IWebHostEnvironment env,
            IEnumerable <ILogger <ImageflowMiddleware> > logger,
            IEnumerable <IClassicDiskCache> diskCaches,
            IEnumerable <IStreamCache> streamCaches,
            IEnumerable <IBlobProvider> blobProviders,
            ImageflowMiddlewareOptions options)
        {
            this.next = next;
            options.Licensing ??= new Licensing(LicenseManagerSingleton.GetOrCreateSingleton(
                                                    "imageflow_", new[] { env.ContentRootPath, Path.GetTempPath() }));
            this.options = options;
            this.env     = env;
            this.logger  = logger.FirstOrDefault();
            diskCache    = diskCaches.FirstOrDefault();

            var streamCacheArray = streamCaches.ToArray();

            if (streamCacheArray.Count() > 1)
            {
                throw new InvalidOperationException("Only 1 IStreamCache instance can be registered at a time");
            }

            streamCache = streamCacheArray.FirstOrDefault();


            var providers   = blobProviders.ToList();
            var mappedPaths = options.MappedPaths.ToList();

            if (options.MapWebRoot)
            {
                if (this.env?.WebRootPath == null)
                {
                    throw new InvalidOperationException("Cannot call MapWebRoot if env.WebRootPath is null");
                }
                mappedPaths.Add(new PathMapping("/", this.env.WebRootPath));
            }

            //Determine the active cache backend
            var streamCacheEnabled = streamCache != null && options.AllowCaching;
            var diskCacheEnabled   = this.diskCache != null && options.AllowDiskCaching;

            if (streamCacheEnabled)
            {
                options.ActiveCacheBackend = CacheBackend.StreamCache;
            }
            else if (diskCacheEnabled)
            {
                options.ActiveCacheBackend = CacheBackend.ClassicDiskCache;
            }
            else
            {
                options.ActiveCacheBackend = CacheBackend.NoCache;
            }


            options.Licensing.Initialize(this.options);

            blobProvider       = new BlobProvider(providers, mappedPaths);
            diagnosticsPage    = new DiagnosticsPage(options, env, this.logger, streamCache, this.diskCache, providers);
            licensePage        = new LicensePage(options);
            globalInfoProvider = new GlobalInfoProvider(options, env, this.logger, streamCache, this.diskCache, providers);

            options.Licensing.FireHeartbeat();
            GlobalPerf.Singleton.SetInfoProviders(new List <IInfoProvider>()
            {
                globalInfoProvider
            });
        }
        public ImageJobInfo(HttpContext context, ImageflowMiddlewareOptions options, BlobProvider blobProvider)
        {
            this.options = options;
            Authorized   = ProcessRewritesAndAuthorization(context, options);

            if (!Authorized)
            {
                return;
            }

            HasParams = PathHelpers.SupportedQuerystringKeys.Any(FinalQuery.ContainsKey);

            // Get the image and page domains
            ImageDomain = context.Request.Host.Host;
            var referer = context.Request.Headers["Referer"].ToString();

            if (!string.IsNullOrEmpty(referer) && Uri.TryCreate(referer, UriKind.Absolute, out var result))
            {
                PageDomain = result.DnsSafeHost;
            }

            var extension = Path.GetExtension(FinalVirtualPath);

            if (FinalQuery.TryGetValue("format", out var newExtension))
            {
                extension = newExtension;
            }

            EstimatedFileExtension = PathHelpers.SanitizeImageExtension(extension) ?? "jpg";

            primaryBlob = new BlobFetchCache(FinalVirtualPath, blobProvider);
            allBlobs    = new List <BlobFetchCache>(1)
            {
                primaryBlob
            };

            appliedWatermarks = new List <NamedWatermark>();

            if (HasParams)
            {
                if (options.Licensing.RequestNeedsEnforcementAction(context.Request))
                {
                    if (options.EnforcementMethod == EnforceLicenseWith.RedDotWatermark)
                    {
                        FinalQuery["watermark_red_dot"] = "true";
                    }
                    LicenseError = true;
                }

                CommandString = PathHelpers.SerializeCommandString(FinalQuery);

                // Look up watermark names
                if (FinalQuery.TryGetValue("watermark", out var watermarkValues))
                {
                    var watermarkNames = watermarkValues.Split(",").Select(s => s.Trim(' '));
                    foreach (var name in watermarkNames)
                    {
                        var watermark = options.NamedWatermarks.FirstOrDefault(w =>
                                                                               w.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
                        if (watermark == null)
                        {
                            throw new InvalidOperationException(
                                      $"watermark {name} was referenced from the querystring but no watermark by that name is registered with the middleware");
                        }

                        appliedWatermarks.Add(watermark);
                    }
                }
            }

            // After we've populated the defaults, run the event handlers for custom watermarking logic
            var args = new WatermarkingEventArgs(context, FinalVirtualPath, FinalQuery, appliedWatermarks);

            foreach (var handler in options.Watermarking)
            {
                var matches = string.IsNullOrEmpty(handler.PathPrefix) ||
                              (FinalVirtualPath != null &&
                               FinalVirtualPath.StartsWith(handler.PathPrefix, StringComparison.OrdinalIgnoreCase));
                if (matches)
                {
                    handler.Handler(args);
                }
            }
            appliedWatermarks = args.AppliedWatermarks;
            if (appliedWatermarks.Count > 0)
            {
                HasParams = true;
            }

            // Add the watermark source files
            foreach (var w in appliedWatermarks)
            {
                allBlobs.Add(new BlobFetchCache(w.VirtualPath, blobProvider));
            }
        }
示例#7
0
 public BlobFetchCache(string virtualPath, BlobProvider provider)
 {
     this.virtualPath = virtualPath;
     this.provider    = provider;
 }