Ejemplo n.º 1
0
        public ImageProcessing ToImageProcessing(ResizeRequest resizeRequest, string filePath)
        {
            var result = new ImageProcessing()
            {
                Url         = resizeRequest.Path,
                ContentType = resizeRequest.ContentType,
                Format      = resizeRequest.Format ?? options.Value.Default.Format,
                Mode        = resizeRequest.Mode ?? options.Value.Default.FitMode,
                Quality     = resizeRequest.Quality ?? options.Value.Default.Quality,
                Anchor      = resizeRequest.Anchor ?? options.Value.Default.Anchor,
                Height      = resizeRequest.Height,
                Width       = resizeRequest.Width,
                Path        = filePath,
            };

            var resizeFileName = result.GetResizeName();

            var requestedDirectory = Path.GetDirectoryName(result.Url);

            result.ResizeUrl = ToUrlPath(
                Path.Combine(options.Value.CachePath, RemoveRootSlash(requestedDirectory), resizeFileName)
                );

            result.ResizePath = Path.Combine(_cachePath, RemoveRootSlash(requestedDirectory), resizeFileName);

            return(result);
        }
Ejemplo n.º 2
0
        private bool Limited(ResizeRequest resizeRequested)
        {
            if (resizeRequested.Width > options.Value.MaxWidth)
            {
                return(true);
            }

            if (resizeRequested.Height > options.Value.MaxHeight)
            {
                return(true);
            }

            return(false);
        }
        public static ResizeRequest CreateFormHttpContext(this HttpContext httpContext)
        {
            var resizeRequest = new ResizeRequest(httpContext.Request.Path);

            var queryCollection = httpContext.Request.Query;

            resizeRequest.Width = queryCollection.ExtractValue <int?>("width", "w");

            resizeRequest.Height = queryCollection.ExtractValue <int?>("height", "h");

            resizeRequest.Format = queryCollection.ExtractValue <OutputFormat?>("format", "f");

            resizeRequest.Quality = queryCollection.ExtractValue <int?>("quality", "q");

            resizeRequest.Anchor = queryCollection.ExtractValue <AnchorLocation?>("anchor", "a");

            resizeRequest.Mode = queryCollection.ExtractValue <FitMode?>("mode", "m");

            return(resizeRequest);
        }
Ejemplo n.º 4
0
 public UrlImageResizerException(ResizeRequest resizeRequest, string message, Exception ex = null) : base(message, ex)
 {
     ResizeRequest = resizeRequest;
 }
Ejemplo n.º 5
0
        public string GetResizedUrl(ResizeRequest resizeRequested)
        {
            var returnPath = resizeRequested.Path;

            if (Limited(resizeRequested))
            {
                if (options.Value.IgnoreLimitation)
                {
                    return(returnPath);
                }
                else
                {
                    throw new UrlImageResizerException(resizeRequested, "bad request");
                }
            }

            var fileInfo = _fileProvider.GetFileInfo(resizeRequested.Path);

            if (!fileInfo.Exists)
            {
                _logger.FileNotFound(fileInfo.PhysicalPath);

                if (options.Value.IgnoreFileNotFound)
                {
                    return(returnPath);
                }
                else
                {
                    throw new UrlImageResizerException(resizeRequested, "File not found.");
                }
            }

            var imageProcessing = ToImageProcessing(resizeRequested, fileInfo.PhysicalPath);

            var resizeFileInfo = _fileProvider.GetFileInfo(imageProcessing.ResizeUrl);

            if (resizeFileInfo.Exists)
            {
                returnPath = imageProcessing.ResizeUrl;
            }
            else
            {
                var pathLock = pathLocks.GetOrAdd(resizeRequested.Path.ToLower(), new object());

                lock (pathLock)
                {
                    try
                    {
                        if (!resizeFileInfo.Exists)
                        {
                            EnsureDirectoryExist(imageProcessing);

                            _imageResizer.Processing(imageProcessing);
                        }

                        returnPath = imageProcessing.ResizeUrl;

                        resizeFileInfo = _fileProvider.GetFileInfo(imageProcessing.ResizeUrl);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError("image url resize failed.", ex);

                        if (!options.Value.IgnoreResizeFailed)
                        {
                            throw new UrlImageResizerException(resizeRequested, "image url resize failed.", ex);
                        }
                    }
                }
            }

            if (returnPath == imageProcessing.ResizeUrl && resizeFileInfo.Exists)
            {
                File.SetLastAccessTimeUtc(imageProcessing.ResizePath, DateTime.UtcNow);
            }

            return(returnPath);
        }