public async Task <bool> PopulateDownloadAnalyticObject(DownloadAnalytic analytic) { using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://freegeoip.net/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // New code: HttpResponseMessage response = await client.GetAsync("json/" + analytic.IPAddress); if (response.IsSuccessStatusCode) { IpAddressObject ips = new IpAddressObject(); try { ips = await response.Content.ReadAsAsync <IpAddressObject>(); } catch { return(false);; } analytic.Latitude = ips.latitude; analytic.Longitude = ips.longitude; analytic.City = ips.city; analytic.CountryCode = ips.country_code; analytic.MetroCode = ips.metro_code; analytic.RegionCode = ips.region_code; analytic.RegionName = ips.region_name; analytic.TimeZone = ips.time_zone; analytic.ZipCode = ips.zip_code; db.Entry(analytic).State = EntityState.Modified; int records = db.SaveChanges(); if (records > 0) { return(true); } // Console.WriteLine("{0}\t${1}\t{2}", ips.Name, product.Price, product.Category); } } return(false); }
public async Task <ActionResult> Download(int?id, bool?bought, string stripeId) { Service service = db.Services.Find(id); if (id == null) { return(View("NotFound")); } if (service.Video != null) { if (bought == true && stripeId != null) { int nonNullId = id.Value; string strIpAddress = string.Empty; strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (strIpAddress == "" || strIpAddress == null) { strIpAddress = Request.ServerVariables["REMOTE_ADDR"]; } //Don't record analytics coming from garbage IP addresses if (strIpAddress != "::1") { DownloadAnalytic analyitic = new DownloadAnalytic(); analyitic.IPAddress = strIpAddress; analyitic.ServiceId = nonNullId; service.DownloadAnalytics.Add(analyitic); db.SaveChanges(); await PopulateDownloadAnalyticObject(analyitic); } try { var chargeService = new StripeChargeService(); StripeCharge stripeCharge = chargeService.Get(stripeId); if (stripeCharge.Status == "succeeded") { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference("videos"); string userFileName = service.FirstName + "_" + service.LastName + "_Video.mp4"; CloudBlockBlob blob = container.GetBlockBlobReference(service.Video.ConvertedFilePath); SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { Permissions = SharedAccessBlobPermissions.Read, SharedAccessExpiryTime = DateTime.Now.AddYears(100) }; SharedAccessBlobHeaders blobHeaders = new SharedAccessBlobHeaders() { ContentDisposition = "attachment; filename=" + userFileName }; string sasToken = blob.GetSharedAccessSignature(policy, blobHeaders); var sasUrl = blob.Uri.AbsoluteUri + sasToken;//This is the URL you will use. It will force the user to download the vi return(Redirect(sasUrl)); } } catch { return(View("NotFound")); } } //publicly exposing URL for now if (Authorize(service) || service.IsSecured != true) { if (service.Video != null) { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference("videos"); string userFileName = service.FirstName + "_" + service.LastName + "_Video.mp4"; CloudBlockBlob blob = container.GetBlockBlobReference(service.Video.ConvertedFilePath); SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { Permissions = SharedAccessBlobPermissions.Read, SharedAccessExpiryTime = DateTime.Now.AddYears(100) }; SharedAccessBlobHeaders blobHeaders = new SharedAccessBlobHeaders() { ContentDisposition = "attachment; filename=" + userFileName }; string sasToken = blob.GetSharedAccessSignature(policy, blobHeaders); var sasUrl = blob.Uri.AbsoluteUri + sasToken;//This is the URL you will use. It will force the user to download the vi return(Redirect(sasUrl)); } } } return(View("NotFound")); }