Ejemplo n.º 1
0
        public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
        {
            _context = context;

            ImageRequestState state = new ImageRequestState();

            string quality = "";
            string format  = "png";
            int    dotPos  = context.Request["quality_format"].IndexOf(".");

            if (dotPos != -1)
            {
                quality = context.Request["quality_format"].Substring(0, dotPos);
                format  = context.Request["quality_format"].Substring(dotPos + 1);
            }
            else
            {
                quality = context.Request["quality_format"];
            }

            if (string.IsNullOrEmpty(context.Request["identifier"]) || string.IsNullOrEmpty(context.Request["size"]) ||
                string.IsNullOrEmpty(context.Request["region"]) || string.IsNullOrEmpty(quality) ||
                string.IsNullOrEmpty(context.Request["rotation"]))
            {
                state.err = new error();

                if (string.IsNullOrEmpty(context.Request["identifier"]))
                {
                    state.err.parameter = ", identifier";
                }
                if (string.IsNullOrEmpty(context.Request["size"]))
                {
                    state.err.parameter += ", size";
                }
                if (string.IsNullOrEmpty(context.Request["region"]))
                {
                    state.err.parameter += ", region";
                }
                if (string.IsNullOrEmpty(context.Request["rotation"]))
                {
                    state.err.parameter += ", rotation";
                }
                if (string.IsNullOrEmpty(quality))
                {
                    state.err.parameter += ", quality";
                }
                if (string.IsNullOrEmpty(context.Request["format"]))
                {
                    state.err.parameter += ", format";
                }

                state.err.parameter = state.err.parameter.Substring(2);
                state.err.text      = "missing " + state.err.parameter;

                state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
            }

            string size = context.Request["size"];

            if (state.err == null && !string.IsNullOrEmpty(size))
            {
                if (size.EndsWith(","))
                {
                    state.sizeType = "resize";
                    state.width    = size.Replace(",", "");

                    if (!string.IsNullOrEmpty(state.width) && int.Parse(state.width) < 1)
                    {
                        state.err            = new error();
                        state.err.parameter  = "size";
                        state.err.text       = "Invalid size specified";
                        state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    }
                }
                else if (size.StartsWith(","))
                {
                    state.sizeType = "resize";
                    state.height   = size.Replace(",", "");

                    if (!string.IsNullOrEmpty(state.height) && int.Parse(state.height) < 1)
                    {
                        state.err            = new error();
                        state.err.parameter  = "size";
                        state.err.text       = "Invalid size specified";
                        state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    }
                }
                else if (size.Equals("full"))
                {
                    state.sizeType = "proportion";
                    state.size     = "1";
                }
                else if (size.StartsWith("pct:"))
                {
                    state.sizeType = "proportion";
                    state.size     = (double.Parse(size.Replace("pct:", "")) / 100).ToString();

                    if (!string.IsNullOrEmpty(state.size) && double.Parse(state.size) <= 0)
                    {
                        state.err            = new error();
                        state.err.parameter  = "size";
                        state.err.text       = "Invalid size specified";
                        state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    }
                }
                else if (size.StartsWith("!"))
                {
                    Regex r = new Regex(@"!(?<w>\d+),(?<h>\d+)");
                    Match m = r.Match(size);

                    state.sizeType = "best";
                    state.width    = m.Groups["w"].Value;
                    state.height   = m.Groups["h"].Value;

                    if ((!string.IsNullOrEmpty(state.width) && int.Parse(state.width) < 1) ||
                        (!string.IsNullOrEmpty(state.height) && int.Parse(state.height) < 1))
                    {
                        state.err            = new error();
                        state.err.parameter  = "size";
                        state.err.text       = "Invalid size specified";
                        state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    }
                }
                else
                {
                    Regex r = new Regex(@"(?<w>\d+),(?<h>\d+)");
                    Match m = r.Match(size);

                    state.sizeType = "resize";
                    state.width    = m.Groups["w"].Value;
                    state.height   = m.Groups["h"].Value;

                    if ((!string.IsNullOrEmpty(state.width) && int.Parse(state.width) < 1) ||
                        (!string.IsNullOrEmpty(state.height) && int.Parse(state.height) < 1))
                    {
                        state.err            = new error();
                        state.err.parameter  = "size";
                        state.err.text       = "Invalid size specified";
                        state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    }
                }

                if (string.IsNullOrEmpty(state.sizeType) || (string.IsNullOrEmpty(state.size) && string.IsNullOrEmpty(state.height) && string.IsNullOrEmpty(state.width)))
                {
                    state.err            = new error();
                    state.err.parameter  = "size";
                    state.err.text       = "Invalid size specified";
                    state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                }
            }

            string region = context.Request["region"];

            if (state.err == null && !string.IsNullOrEmpty(region))
            {
                if (context.Request["region"].Equals("full"))
                {
                    state.region = "all";
                }
                else if (region.StartsWith("pct:"))
                {
                    Regex  r = new Regex(@"pct:(?<x>(\d+|\d+\.\d+)),(?<y>(\d+|\d+\.\d+)),(?<w>(\d+|\d+\.\d+)),(?<h>(\d+|\d+\.\d+))");
                    Match  m = r.Match(region);
                    double y = Convert.ToDouble(m.Groups["y"].Value) / 100;
                    double x = Convert.ToDouble(m.Groups["x"].Value) / 100;
                    double h = Convert.ToDouble(m.Groups["h"].Value) / 100;
                    double w = Convert.ToDouble(m.Groups["w"].Value) / 100;

                    state.region = "%" + x + "," + y + "," + w + "," + h;

                    state.regionHeight = h.ToString();
                    state.regionWidth  = w.ToString();

                    if ((!string.IsNullOrEmpty(state.regionWidth) && Convert.ToDouble(state.regionWidth) <= 0) ||
                        (!string.IsNullOrEmpty(state.regionHeight) && Convert.ToDouble(state.regionHeight) <= 0))
                    {
                        state.err            = new error();
                        state.err.parameter  = "region";
                        state.err.text       = "Invalid region specified";
                        state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    }
                }
                else
                {
                    Regex r = new Regex(@"(?<x>\d+),(?<y>\d+),(?<w>\d+),(?<h>\d+)");
                    Match m = r.Match(region);

                    state.region       = m.Groups["x"].Value + "," + m.Groups["y"].Value + "," + m.Groups["w"].Value + "," + m.Groups["h"].Value;
                    state.regionHeight = m.Groups["h"].Value;
                    state.regionWidth  = m.Groups["w"].Value;

                    if (state.sizeType.Equals("proportion"))
                    {
                        if (!string.IsNullOrEmpty(state.regionWidth) && Convert.ToInt32(state.regionWidth) * Convert.ToDouble(state.size) < 1)
                        {
                            state.err            = new error();
                            state.err.parameter  = "region";
                            state.err.text       = "Invalid region specified";
                            state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                        }
                        else if (!string.IsNullOrEmpty(state.regionHeight) && Convert.ToInt32(state.regionHeight) * Convert.ToDouble(state.size) < 1)
                        {
                            state.err            = new error();
                            state.err.parameter  = "region";
                            state.err.text       = "Invalid region specified";
                            state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                        }
                    }
                    if ((!string.IsNullOrEmpty(state.regionWidth) && int.Parse(state.regionWidth) < 1) ||
                        (!string.IsNullOrEmpty(state.regionHeight) && int.Parse(state.regionHeight) < 1))
                    {
                        state.err            = new error();
                        state.err.parameter  = "region";
                        state.err.text       = "Invalid region specified";
                        state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    }
                }

                if (string.IsNullOrEmpty(state.region.Replace(",", "")))
                {
                    state.err            = new error();
                    state.err.parameter  = "region";
                    state.err.text       = "Invalid region specified";
                    state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                }
            }

            if (state.err == null && !string.IsNullOrEmpty(quality))
            {
                switch (quality)
                {
                case "grey": quality = "grey"; break;

                case "bitonal": quality = "bitonal"; break;

                case "color": quality = "color"; break;

                case "native": quality = "native"; break;

                default:
                    state.err            = new error();
                    state.err.parameter  = "quality";
                    state.err.text       = "only native, color, grey, bitonal supported";
                    state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    break;
                }
            }

            string rotate = context.Request["rotation"];

            if (state.err == null && !string.IsNullOrEmpty(rotate))
            {
                switch (rotate)
                {
                case "90":
                case "180":
                case "270":
                    state.rotation = rotate; break;

                default:
                    int temp = 0;
                    if (int.TryParse(rotate, out temp))
                    {
                        state.rotation = temp.ToString();
                    }
                    else
                    {
                        state.err            = new error();
                        state.err.parameter  = "rotation";
                        state.err.text       = "only 0, 90, 180, 270 are accepted rotation values";
                        state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    }
                    break;
                }
            }

            if (state.err == null && !string.IsNullOrEmpty(format))
            {
                switch (format)
                {
                case "jpg": format = "jpg"; break;

                case "png": format = "png"; break;

                case "gif": format = "gif"; break;

                case "jp2":
                case "pdf":
                default:
                    state.err            = new error();
                    state.err.parameter  = "format";
                    state.err.text       = format + " not supported";
                    state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    break;         // no format matched
                }
            }

            state.identifier = context.Request["identifier"];
            state.rotation   = context.Request["rotation"];
            state.quality    = quality;
            state.format     = format;

            asynch = new ImageAsynchResult(cb, context, state);

            getImage = new GetImageDelegate(GetImage);
            getImage.BeginInvoke(asynch, null, state);

            return(asynch);
        }
        public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
        {
            _context = context;

            ImageRequestState state = new ImageRequestState();

            string quality = "";
            string format = "png";
            int dotPos = context.Request["quality_format"].IndexOf(".");

            if (dotPos != -1)
            {
                quality = context.Request["quality_format"].Substring(0, dotPos);
                format = context.Request["quality_format"].Substring(dotPos + 1);
            }
            else
            {
                quality = context.Request["quality_format"];
            }

            if (string.IsNullOrEmpty(context.Request["identifier"]) || string.IsNullOrEmpty(context.Request["size"]) ||
                string.IsNullOrEmpty(context.Request["region"]) || string.IsNullOrEmpty(quality) ||
                string.IsNullOrEmpty(context.Request["rotation"]))
            {
                state.err = new error();

                if (string.IsNullOrEmpty(context.Request["identifier"]))
                    state.err.parameter = ", identifier";
                if (string.IsNullOrEmpty(context.Request["size"]))
                    state.err.parameter += ", size";
                if (string.IsNullOrEmpty(context.Request["region"]))
                    state.err.parameter += ", region";
                if (string.IsNullOrEmpty(context.Request["rotation"]))
                    state.err.parameter += ", rotation";
                if (string.IsNullOrEmpty(quality))
                    state.err.parameter += ", quality";
                if (string.IsNullOrEmpty(context.Request["format"]))
                    state.err.parameter += ", format";

                state.err.parameter = state.err.parameter.Substring(2);
                state.err.text = "missing " + state.err.parameter;

                state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
            }

            string size = context.Request["size"];
            if (state.err == null && !string.IsNullOrEmpty(size))
            {
                if (size.EndsWith(","))
                {
                    state.sizeType = "resize";
                    state.width = size.Replace(",", "");

                    if (!string.IsNullOrEmpty(state.width) && int.Parse(state.width) < 1)
                    {
                        state.err = new error();
                        state.err.parameter = "size";
                        state.err.text = "Invalid size specified";
                        state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    }
                }
                else if (size.StartsWith(","))
                {
                    state.sizeType = "resize";
                    state.height = size.Replace(",", "");

                    if (!string.IsNullOrEmpty(state.height) && int.Parse(state.height) < 1)
                    {
                        state.err = new error();
                        state.err.parameter = "size";
                        state.err.text = "Invalid size specified";
                        state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    }
                }
                else if (size.Equals("full"))
                {
                    state.sizeType = "proportion";
                    state.size = "1";
                }
                else if (size.StartsWith("pct:"))
                {
                    state.sizeType = "proportion";
                    state.size = (double.Parse(size.Replace("pct:", "")) / 100).ToString();

                    if (!string.IsNullOrEmpty(state.size) && double.Parse(state.size) <= 0)
                    {
                        state.err = new error();
                        state.err.parameter = "size";
                        state.err.text = "Invalid size specified";
                        state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    }
                }
                else if (size.StartsWith("!"))
                {
                    Regex r = new Regex(@"!(?<w>\d+),(?<h>\d+)");
                    Match m = r.Match(size);

                    state.sizeType = "best";
                    state.width = m.Groups["w"].Value;
                    state.height = m.Groups["h"].Value;

                    if ((!string.IsNullOrEmpty(state.width) && int.Parse(state.width) < 1) ||
                        (!string.IsNullOrEmpty(state.height) && int.Parse(state.height) < 1))
                    {
                        state.err = new error();
                        state.err.parameter = "size";
                        state.err.text = "Invalid size specified";
                        state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    }
                }
                else
                {
                    Regex r = new Regex(@"(?<w>\d+),(?<h>\d+)");
                    Match m = r.Match(size);

                    state.sizeType = "resize";
                    state.width = m.Groups["w"].Value;
                    state.height = m.Groups["h"].Value;

                    if ((!string.IsNullOrEmpty(state.width) && int.Parse(state.width) < 1) ||
                        (!string.IsNullOrEmpty(state.height) && int.Parse(state.height) < 1))
                    {
                        state.err = new error();
                        state.err.parameter = "size";
                        state.err.text = "Invalid size specified";
                        state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    }
                }

                if (string.IsNullOrEmpty(state.sizeType) || (string.IsNullOrEmpty(state.size) && string.IsNullOrEmpty(state.height) && string.IsNullOrEmpty(state.width)))
                {
                    state.err = new error();
                    state.err.parameter = "size";
                    state.err.text = "Invalid size specified";
                    state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                }
            }

            string region = context.Request["region"];
            if (state.err == null && !string.IsNullOrEmpty(region))
            {
                if (context.Request["region"].Equals("full"))
                {
                    state.region = "all";
                }
                else if (region.StartsWith("pct:"))
                {
                    Regex r = new Regex(@"pct:(?<x>(\d+|\d+\.\d+)),(?<y>(\d+|\d+\.\d+)),(?<w>(\d+|\d+\.\d+)),(?<h>(\d+|\d+\.\d+))");
                    Match m = r.Match(region);
                    double y = Convert.ToDouble(m.Groups["y"].Value) / 100;
                    double x = Convert.ToDouble(m.Groups["x"].Value) / 100;
                    double h = Convert.ToDouble(m.Groups["h"].Value) / 100;
                    double w = Convert.ToDouble(m.Groups["w"].Value) / 100;

                    state.region = "%" + x + "," + y + "," + w + "," + h;

                    state.regionHeight = h.ToString();
                    state.regionWidth = w.ToString();

                    if ((!string.IsNullOrEmpty(state.regionWidth) && Convert.ToDouble(state.regionWidth) <= 0) ||
                        (!string.IsNullOrEmpty(state.regionHeight) && Convert.ToDouble(state.regionHeight) <= 0))
                    {
                        state.err = new error();
                        state.err.parameter = "region";
                        state.err.text = "Invalid region specified";
                        state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    }
                }
                else
                {
                    Regex r = new Regex(@"(?<x>\d+),(?<y>\d+),(?<w>\d+),(?<h>\d+)");
                    Match m = r.Match(region);

                    state.region = m.Groups["x"].Value + "," + m.Groups["y"].Value + "," + m.Groups["w"].Value + "," + m.Groups["h"].Value;
                    state.regionHeight = m.Groups["h"].Value;
                    state.regionWidth = m.Groups["w"].Value;

                    if (state.sizeType.Equals("proportion"))
                    {
                        if (!string.IsNullOrEmpty(state.regionWidth) && Convert.ToInt32(state.regionWidth) * Convert.ToDouble(state.size) < 1)
                        {
                            state.err = new error();
                            state.err.parameter = "region";
                            state.err.text = "Invalid region specified";
                            state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                        }
                        else if (!string.IsNullOrEmpty(state.regionHeight) && Convert.ToInt32(state.regionHeight) * Convert.ToDouble(state.size) < 1)
                        {
                            state.err = new error();
                            state.err.parameter = "region";
                            state.err.text = "Invalid region specified";
                            state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                        }
                    }
                    if ((!string.IsNullOrEmpty(state.regionWidth) && int.Parse(state.regionWidth) < 1) ||
                        (!string.IsNullOrEmpty(state.regionHeight) && int.Parse(state.regionHeight) < 1))
                    {
                        state.err = new error();
                        state.err.parameter = "region";
                        state.err.text = "Invalid region specified";
                        state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    }
                }

                if (string.IsNullOrEmpty(state.region.Replace(",", "")))
                {
                    state.err = new error();
                    state.err.parameter = "region";
                    state.err.text = "Invalid region specified";
                    state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                }
            }

            if (state.err == null && !string.IsNullOrEmpty(quality))
            {
                switch (quality)
                {
                    case "grey": quality = "grey"; break;
                    case "bitonal": quality = "bitonal"; break;
                    case "color": quality = "color"; break;
                    case "native": quality = "native"; break;
                    default:
                        state.err = new error();
                        state.err.parameter = "quality";
                        state.err.text = "only native, color, grey, bitonal supported";
                        state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                        break;
                }
            }

            string rotate = context.Request["rotation"];
            if (state.err == null && !string.IsNullOrEmpty(rotate))
            {
                switch (rotate)
                {
                    case "90":
                    case "180":
                    case "270":
                        state.rotation = rotate; break;
                    default:
                        int temp = 0;
                        if (int.TryParse(rotate, out temp))
                        {
                            state.rotation = temp.ToString();
                        }
                        else
                        {
                            state.err = new error();
                            state.err.parameter = "rotation";
                            state.err.text = "only 0, 90, 180, 270 are accepted rotation values";
                            state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                        }
                        break;
                }

            }

            if (state.err == null && !string.IsNullOrEmpty(format))
            {
                switch (format)
                {
                    case "jpg": format = "jpg"; break;
                    case "png": format = "png"; break;
                    case "gif": format = "gif"; break;
                    case "jp2":
                    case "pdf":
                    default:
                        state.err = new error();
                        state.err.parameter = "format";
                        state.err.text = format + " not supported";
                        state.err.statusCode = System.Net.HttpStatusCode.BadRequest;
                        break;     // no format matched
                }
            }

            state.identifier = context.Request["identifier"];
            state.rotation = context.Request["rotation"];
            state.quality = quality;
            state.format = format;

            asynch = new ImageAsynchResult(cb, context, state);

            getImage = new GetImageDelegate(GetImage);
            getImage.BeginInvoke(asynch, null, state);

            return asynch;
        }