Exemplo n.º 1
0
        private dynamic ResizeImage(dynamic arg)
        {
            var file = Path.Combine(this.RootPath, (string)arg.path);

            if (File.Exists(file) == false)
            {
                return(404);
            }

            if (this.Request.Url.Query == string.Empty)
            {
                return(400);
            }

            var    parameter    = new ImageResizeModule.ImageFiltrationParameters(this.Context, file);
            var    parameterKey = parameter.GetHashCode().ToString();
            var    key          = file + parameterKey;
            string resizeRelativePath;

            if (_FileList.TryGetValue(key, out resizeRelativePath) == true)
            {
                return(this.Response.AsRedirect(resizeRelativePath, Nancy.Responses.RedirectResponse.RedirectType.Permanent));
            }

            resizeRelativePath = "site/imageresize/" + (string)arg.path + "/" + parameterKey + Path.GetExtension(file);

            var rootPath        = this.RootPath;
            var resizeDirectory = Path.Combine(rootPath, "Site", "imageresize", (string)arg.path);

            Directory.CreateDirectory(resizeDirectory);

            var resizeFile = Path.Combine(rootPath, resizeRelativePath);

            lock (resizeFile)
            {
                if (File.Exists(resizeFile) == true)
                {
                    _FileList[key] = "/" + resizeRelativePath;
                    return(this.Response.AsRedirect("/" + resizeRelativePath, Nancy.Responses.RedirectResponse.RedirectType.Permanent));
                }

                if (Directory.GetFiles(resizeDirectory).Length + 1 > MAX_PER_FILE)
                {
                    return(400);
                }

                var result = ImageResizeModule.ResizeAndFilterImage(file, parameter);

                using (var resized = File.OpenWrite(resizeFile))
                {
                    result.Output.CopyTo(resized);
                }

                _FileList[key] = "/" + resizeRelativePath;
                return(this.Response.AsRedirect("/" + resizeRelativePath, Nancy.Responses.RedirectResponse.RedirectType.Permanent));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Resize the image without using disk cache
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        private dynamic ResizeImage_NoCache(dynamic arg)
        {
            var file = Path.Combine(this.RootPath, (string)arg.path);

            if (File.Exists(file) == false)
            {
                return(404);
            }

            var parameter    = new ImageResizeModule.ImageFiltrationParameters(arg, file);
            var parameterKey = parameter.GetHashCode().ToString();

            HashSet <string> set;

            if (_AttackPrevention.TryGetValue(file, out set) == false)
            {
                set = new HashSet <string>();
                _AttackPrevention.Add(file, set);
            }

            // this file contains too much unique parameter request
            if (set.Contains(parameterKey) == false && set.Count > MAX_PER_FILE)
            {
                return(404);
            }

            set.Add(parameterKey);

            var cacheKey = file + "&&" + parameter.GetHashCode();
            var result   = MemoryCache.Default.Get(cacheKey) as ResizeResult;

            if (result == null)
            {
                var resizeResult = ImageResizeModule.ResizeAndFilterImage(file, parameter);
                result = resizeResult;

                MemoryCache.Default.Add(cacheKey, result, DateTimeOffset.Now.AddMinutes(10));
            }

            var response = new Response();

            response.ContentType = result.ContentType;
            response.Contents    = (s) =>
            {
                result.Output.Position = 0;
                result.Output.CopyTo(s);
            };

            return(response);
        }