///<exception cref="BadCropArgumentsException"/> ///<exception cref="CropZeroSizeException"/> public static CropArgs ExtractCropArguments(string url) { string argsAsOne = ExtractCropArgumentsAsOne(url); CropArgs argsIndividual = ExtractCropArgumentsIndividual(argsAsOne); if (argsIndividual.width == 0 || argsIndividual.height == 0) { throw new CropZeroSizeException(); } return(argsIndividual); }
private static CropArgs ExtractCropArgumentsIndividual(string cropArgsAsOne) { Regex regexForIndividualArgs = new Regex(@"-?[0-9]+"); MatchCollection matchCollection = regexForIndividualArgs.Matches(cropArgsAsOne); CropArgs cropArgs = new CropArgs(); cropArgs.x = Convert.ToInt32(matchCollection[0].Value); cropArgs.y = Convert.ToInt32(matchCollection[1].Value); cropArgs.width = Convert.ToInt32(matchCollection[2].Value); cropArgs.height = Convert.ToInt32(matchCollection[3].Value); return(cropArgs); }
protected override void HandleContext(HttpListenerContext listenerContext) { string url = listenerContext.Request.Url.ToString(); try { // Transform image string transformName = UrlPartsExtractor.ExtractTransformName(url); Image imageFromRequest = HttpImageExtractor.ExtractImage(listenerContext, imageMaxSizeConstraint, imageMaxWidthConstraint, imageMaxHeightConstraint); Image transformedImage = ImageTransformer.TransformImage(imageFromRequest, transformName); // Crop image CropArgs cropArgs = UrlPartsExtractor.ExtractCropArguments(url); Image croppedImage = ImageTransformer.CropImage(transformedImage, cropArgs.x, cropArgs.y, cropArgs.width, cropArgs.height); RespondWithOK(listenerContext.Response, croppedImage); } catch (Exception e) { if (e is NoTransformNameException || e is BadImageException || e is UnknownTransformException || e is BadCropArgumentsException || e is CropZeroSizeException) { RespondWithError(listenerContext.Response, BAD_REQUEST); } else if (e is EmptyIntersectionException) { RespondWithError(listenerContext.Response, NO_CONTENT); } else { RespondWithError(listenerContext.Response, INTERNAL_SERVER_ERROR); } return; } }