public async Task <HttpResponseMessage> Post()
        {
            //Check if the request contains multipart / form - data.
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            try
            {
                //Retrieve current user's email
                var customerEmail = ClaimsPrincipal.Current.FindFirst("emails").Value;

                string MTCName = (await MtcsHelper.GetMtcByCustomerEmail(customerEmail))["name"].ToString();

                string fileName = MTCName + "_" + HttpContext.Current.Request.Files[0].FileName;

                // Retrieve storage account from connection string.
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                    Settings.StorageConnectionString);

                // Create the blob client.
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                // Retrieve a reference to a container.
                CloudBlobContainer container = blobClient.GetContainerReference(Settings.BlobContainerName);

                // Create the container if it doesn't already exist.
                container.CreateIfNotExists();

                container.SetPermissions(new BlobContainerPermissions {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });

                // Retrieve reference to a blob named "myblob".
                CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);

                blockBlob.Properties.ContentType = "image/jpeg";

                // Create or overwrite the "myblob" blob with contents from a local file.
                // Read the form data.
                blockBlob.UploadFromStream(HttpContext.Current.Request.Files[0].InputStream);

                return(Request.CreateResponse(HttpStatusCode.OK, blockBlob.SnapshotQualifiedUri.ToString()));
            }
            catch (System.Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message));
            }
        }
Esempio n. 2
0
        public async Task <HttpResponseMessage> Post([FromBody] Models.MobilePropertyInsurance value)
        {
            try
            {
                var properties = new Dictionary <string, string>();

                properties.Add("ClaimDescription", value.ClaimDescription == null
                    ? string.Empty : value.ClaimDescription);

                properties.Add("ClaimDateTime", value.ClaimDateTime.ToString());

                //Retrieve current user's email
                var customerEmail = ClaimsPrincipal.Current.FindFirst("emails").Value;

                string claimsAdjusterEmail = (await MtcsHelper.GetMtcByCustomerEmail(customerEmail))["adjuster"]["email"].ToString();

                var PropertyInsurance = new Models.PropertyInsurance
                {
                    claimDetailsPageUrl = Settings.ClaimDetailsPageUrl,
                    claimsAdjusterEmail = claimsAdjusterEmail,
                    CustomerEmail       = customerEmail,
                    CorrelationId       = "0",
                    ImageUrl            = value.ImageUrl,
                    BlobFilePath        = new Uri(value.ImageUrl).PathAndQuery,
                    Properties          = properties,
                    TagExpression       = value.Tag != null ? value.Tag : string.Empty
                };

                var json = new JavaScriptSerializer().Serialize(PropertyInsurance);

                var httpContent = new StringContent(json, Encoding.UTF8, "application/json");

                HttpClient httpClient = new HttpClient();

                var httpResponse = await httpClient.PostAsync(Settings.ApprovePictureLogicApp, httpContent);

                var responseContent = await httpResponse.Content.ReadAsStringAsync();

                return(Request.CreateResponse(HttpStatusCode.OK, responseContent, new MediaTypeHeaderValue("application/json")));
            }
            catch (System.Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
        }