コード例 #1
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "Upload")] HttpRequest req)
        {
            try
            {
                if (!req.Form.Files.Any() || req.Form["container"] == string.Empty)
                {
                    return(Utilities.Utilities.GetHttpResponseMessage_ReturnsStatusCodeAndMessage(
                               HttpStatusCode.BadRequest, "invalid request data"));
                }

                IImageService service = Utilities.Utilities.GetImageService();

                IContainerService container = new ContainerClass(req.Form["container"]);

                if (!service.CheckIfContainerNameIsValid(container))
                {
                    return(Utilities.Utilities.GetHttpResponseMessage_ReturnsStatusCodeAndMessage(
                               HttpStatusCode.BadRequest, "invalid container name"));
                }

                foreach (var imageFile in req.Form.Files)
                {
                    if (!service.CheckIfFileIsSupported(imageFile.FileName))
                    {
                        return(Utilities.Utilities.GetHttpResponseMessage_ReturnsStatusCodeAndMessage(
                                   HttpStatusCode.BadRequest, "invalid image format"));
                    }

                    if (service.GetUploadImageSecurityKey(container.GetContainerName(), imageFile.FileName, imageFile.Length.ToString()) != imageFile.Name)
                    {
                        return(Utilities.Utilities.GetHttpResponseMessage_ReturnsStatusCodeAndMessage(
                                   HttpStatusCode.Forbidden, ""));
                    }
                }

                List <string>    notUploadedFiles = new List <string>();
                List <ImageData> filesToUpload    = new List <ImageData>();

                IDatabaseService databaseService = Utilities.Utilities.GetDatabaseService();

                foreach (var imageToUpload in req.Form.Files)
                {
                    string    imagePath    = service.GetImagePathUpload(imageToUpload.FileName);
                    ImageData uploadResult = service.UploadImage(
                        req.Form.Files.GetFile(imageToUpload.Name).OpenReadStream(),
                        container,
                        imagePath);

                    if (uploadResult.ImageName.IsNullOrEmpty())
                    {
                        notUploadedFiles.Add(imageToUpload.FileName);
                    }
                    else
                    {
                        filesToUpload.Add(uploadResult);
                    }
                }

                if (filesToUpload.Any())
                {
                    databaseService.SaveImagesData(filesToUpload);
                }

                if (notUploadedFiles.Any())
                {
                    return(Utilities.Utilities.GetHttpResponseMessage_ReturnsStatusCodeAndMessage(
                               HttpStatusCode.MultiStatus, JsonConvert.SerializeObject(notUploadedFiles)));
                }


                return(Utilities.Utilities.GetHttpResponseMessage_ReturnsStatusCodeAndMessage(
                           HttpStatusCode.Created, "Uploaded successfully"));
            }
            catch (Exception e)
            {
                Log.Error(e.Message);
                return(Utilities.Utilities.GetHttpResponseMessage_ReturnsStatusCodeAndMessage(
                           HttpStatusCode.InternalServerError, "Something gone wrong"));
            }
        }
コード例 #2
0
ファイル: Resize.cs プロジェクト: Shadowcaster4/AzureImage
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Resize/{parameters}/{container}/{imagename}")] HttpRequest req,
            string parameters,
            string container,
            string imagename)
        {
            try
            {
                Log.Info("Resize");
                IImageService     service          = Utilities.Utilities.GetImageService();
                IContainerService containerService = new ContainerClass(container);

                var requestedParameters = new QueryParameterValues(parameters);

                if (service.CheckIfParametersAreInRange(requestedParameters.Width, requestedParameters.Height))
                {
                    return(Utilities.Utilities.GetHttpResponseMessage_ReturnsStatusCodeAndMessage(
                               HttpStatusCode.BadRequest, "invalid parameter values"));
                }

                if (!service.CheckIfFileIsSupported(imagename))
                {
                    return(Utilities.Utilities.GetHttpResponseMessage_ReturnsStatusCodeAndMessage(
                               HttpStatusCode.BadRequest, "Not supported image type"));
                }

                if (!service.CheckIfContainerExists(containerService))
                {
                    throw new Exception("Container doesn't exists");
                }

                if (!service.CheckIfImageExists(service.GetImagePathUpload(imagename), containerService))
                {
                    return(Utilities.Utilities.GetHttpResponseMessage_ReturnsStatusCodeAndMessage(
                               HttpStatusCode.NotFound, "Requested image doesn't exists"));
                }

                //check if watermark  exist if not watermark presence parameter will be set to false
                if (!service.CheckIfImageExists(service.GetImagePathUpload("watermark.png"), containerService))
                {
                    requestedParameters.SetWatermarkPresence(false);
                }

                //check if hash from parameter is valid for requested picture
                else if (service.GetImageSecurityHash(container, imagename).Substring(0, 4) == requestedParameters.WatermarkString)
                {
                    requestedParameters.SetWatermarkPresence(false);
                }

                //set extension variable
                var imageExtension = service.GetImageExtension(imagename);
                var imagePath      = service.GetImagePathResize(requestedParameters, imagename);

                //check if requested resolution is valid - original image resolution is >= requested resolution
                IDatabaseService databaseService = Utilities.Utilities.GetDatabaseService();
                var imageData = databaseService.GetImageData(imagename, containerService);

                var flagIsInOriginalImageRange = service.CheckIfImageRequestedImageResolutionIsInRange(
                    requestedParameters.Width,
                    requestedParameters.Height,
                    imageData);

                //if requested image resolution is out of range and requested image doesn't contain watermark then it will return image from original image stream
                if (!flagIsInOriginalImageRange)
                {
                    if (!requestedParameters.WatermarkPresence)
                    {
                        var tmpImg = service.DownloadImageFromStorageToStream(service.GetImagePathUpload(imagename), containerService);
                        return(Utilities.Utilities.GetImageHttpResponseMessage(tmpImg, imagename, imageExtension));
                    }
                    //if watermark will be used then new image will be created and saved in location created with "0,0" parameters
                    string newParametersValues     = "0,0";
                    var    oversizeImageParameters = new QueryParameterValues(newParametersValues);
                    requestedParameters = oversizeImageParameters;
                    imagePath           = service.GetImagePathResize(requestedParameters, imagename);
                }

                if (service.CheckIfImageExists(imagePath, containerService))
                {
                    var tmpImg = service.DownloadImageFromStorageToStream(imagePath, containerService);
                    return(Utilities.Utilities.GetImageHttpResponseMessage(tmpImg, imagename, imageExtension));
                }

                //this part creates new resized image
                if (service.CheckIfImageExists(service.GetImagePathUpload(imagename), containerService))
                {
                    //download base image from storage
                    var imageFromStorage = service.DownloadImageFromStorageToStream(service.GetImagePathUpload(imagename), containerService);
                    //create new image with requested parameters
                    var mutatedImage = service.MutateImage(
                        imageFromStorage,
                        containerService,
                        requestedParameters.Width,
                        requestedParameters.Height,
                        requestedParameters.Padding,
                        imageExtension,
                        requestedParameters.WatermarkPresence);
                    //save created image
                    service.SaveImage(mutatedImage, imagePath, containerService);

                    return(Utilities.Utilities.GetImageHttpResponseMessage(mutatedImage, imagename, imageExtension));
                }

                return(Utilities.Utilities.GetHttpResponseMessage_ReturnsStatusCodeAndMessage(
                           HttpStatusCode.NotFound, ""));
            }
            catch (Exception e)
            {
                Log.Error(e.Message);

                return(Utilities.Utilities.GetHttpResponseMessage_ReturnsStatusCodeAndMessage(
                           HttpStatusCode.BadRequest, "Something gone wrong"));
            }
        }