Ejemplo n.º 1
0
        public SKData Resize(IResizableImage image, ResizeParameters resizeParameters)
        {
            if (resizeParameters is null)
            {
                return(null);
            }
            if (image is null)
            {
                return(null);
            }

            _image            = image;
            _resizeParameters = resizeParameters;

            SKData imageData = null;

            try
            {
                imageData = GetImageData();
            }
            catch (ArgumentException ex)
            {
                _logger.LogError(ex.Message);
            }

            return(imageData);
        }
Ejemplo n.º 2
0
        public async Task Invoke(HttpContext httpContext)
        {
            var path             = httpContext.Request.Path.Value;
            var resizeParameters = new ResizeParameters(path, httpContext.Request.Query);

            if (httpContext.Request.Query.Count == 0 || !IsImagePath(path) || !resizeParameters.HasParams)
            {
                await _next.Invoke(httpContext);

                return;
            }

            var cacheKey           = (path.GetHashCode() + resizeParameters.ToString().GetHashCode()).ToString("X");
            var cacheDirectoryName = Path.GetFileName(path).Replace(".", "");

            //сведения об общей директории кэша
            var cacheInfo = new PhysicalImageCacheInfo(_appConfiguration.CacheOptions);

            cacheInfo.SpecCacheRoot = Path.Join(cacheInfo.CacheRoot, cacheDirectoryName);

            ICustomImageCacheManager cacheManager = new PhysicalImageCacheManager(cacheInfo);

            if (cacheManager.Contains(cacheKey))
            {
                var cachedImageBytes = await cacheManager.ReadCachedFileAsync(cacheKey);

                await httpContext.Response.Body.WriteAsync(cachedImageBytes, 0, cachedImageBytes.Length);

                return;
            }

            await using var imageStream = File.OpenRead(path);
            using IResizableImage image = new ResizableImage(imageStream);

            var result = _imageResizer.Resize(image, resizeParameters);

            if (!(result is null))
            {
                _cacheController.CheckCacheState(cacheInfo);

                var cachedFile = await cacheManager.CacheFileAsync(result.ToArray(), path, cacheKey);

                await httpContext.Response.Body.WriteAsync(cachedFile, 0, cachedFile.Length);
            }