protected void rptCoupons_ItemDataBound(Object Sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Header || e.Item.ItemType == ListItemType.Footer) { e.Item.Visible = false; if (_coupons.Count <= 0) return; e.Item.Visible = true; } else if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { if (e.Item != null && e.Item.DataItem != null) { e.Item.Visible = false; Coupon coupon = new Coupon(); coupon = (Coupon)e.Item.DataItem; GFCK.UserControls.Coupon.home CouponDisplay = (GFCK.UserControls.Coupon.home)e.Item.FindControl("cd"); if (coupon != null && CouponDisplay != null) { //if the number of coupons set up by the manufacturer has already been reached, don't display. if (coupon.Clicks >= coupon.NumberOfCoupons) { return; } //if the coupon has expired, don't display - already built into the SP IMerchantDAO merchantDAO = _factoryDAO.GetMerchantDAO(); Merchant m = merchantDAO.GetMerchant(coupon.MerchantID); CouponDisplay.Name = m.MerchantName; if (!String.IsNullOrEmpty(coupon.Image)) { CouponDisplay.Picture = string.Format("https://s3.amazonaws.com/gfck/coupon/{0}", coupon.Image); //"https://s3.amazonaws.com/gfck/coupon/Carvel/3.gif";// coupon.Image.ToString(); } else { CouponDisplay.Picture = ""; } CouponDisplay.Description = coupon.Name; CouponDisplay.Amount = coupon.Value; CouponDisplay.CouponID = coupon.ID.ToString(); e.Item.Visible = true; } } } }
protected void btnSave_Click(object sender, EventArgs e) { try { ICouponDAO couponDAO = _factoryDAO.GetCouponDAO(); Coupon coupon = new Coupon(); coupon.TemplateID = 1; // Convert.ToInt64(ddlTemplate.SelectedValue); coupon.CategoryID = Convert.ToInt32(ddlCategory.SelectedValue); coupon.MerchantID = _manufacturerID; //coupon.Image = imgByte; coupon.Name = txtName.Text; coupon.Value = txtValue.Text; //coupon.Discount = txtDiscount.Text; coupon.Details = txtDetails.Text; coupon.Terms = txtTerms.Text; coupon.StartDate = Convert.ToDateTime(txtStartDate.Text); coupon.ExpirationDate = Convert.ToDateTime(txtExpirationDate.Text); coupon.GlutenFreeFacility = chkGlutenFreeFacility.Checked; coupon.ContainGluten20PPM = chkContainGluten20PPM.Checked; coupon.LessThan10PPM = chkLessThan10PPM.Checked; coupon.LessThan5PPM = chkLessThan5PPM.Checked; coupon.CaseinFree = chkCaseinFree.Checked; coupon.SoyFree = chkSoyFree.Checked; coupon.NutFree = chkNutFree.Checked; coupon.EggFree = chkEggFree.Checked; coupon.CornFree = chkCornFree.Checked; coupon.YeastFree = chkYeastFree.Checked; coupon.Barcode1Type = "UPCACCA"; coupon.Barcode1Value = txtBarcode1Value.Text; coupon.Barcode2Type = "RSSExpandedStacked"; coupon.Barcode2Value = txtBarcode2Value.Text; coupon.NumberOfCoupons = Convert.ToInt32(txtNumberOfCoupons.Text); coupon.BottomAdvertisement = "keep"; coupon.Enabled = Convert.ToBoolean(txtEnabled.Value); if (_couponID == 0) { // Need to add a record _couponID = couponDAO.AddCoupon(coupon); } if (_couponID > 0) { coupon.ID = _couponID; } try { bool success = false; string err = ""; FileUpload img = (FileUpload)imgUpload; if (img == null) { err = "Bad image."; } else if (img.HasFile && img.PostedFile != null && !CheckFileType(img.PostedFile.FileName)) { err = "Sorry, Please only upload images for coupon."; } else if (img.HasFile && img.PostedFile != null && img.PostedFile.ContentLength > 5242880) { err = "Sorry, File size is too large. Coupon image needs to be under 5MB."; } else if (img.HasFile && img.PostedFile != null) { //To create a PostedFile HttpPostedFile File = imgUpload.PostedFile; success = AddImageToAmazon(coupon, File, ""); } else { coupon.Image = "keep"; success = couponDAO.UpdateCoupon(coupon); } // Need to update the record if (success) { // Update was successfull lblEditSuccessfull.Visible = true; lblError.Visible = false; lblAddSuccessfull.Visible = false; } else { if (err != "") lblError.Text = err; lblError.Visible = true; lblAddSuccessfull.Visible = false; lblEditSuccessfull.Visible = false; _log.DebugFormat("Unable to Update the Coupon. {0}", err); } } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { _log.Error("Please check the provided AWS Credentials."); } else { _log.Error(string.Format("An error occurred with the message '{0}' when writing an object", amazonS3Exception.Message)); } lblError.Text = "Error saving coupon to S3."; lblError.Visible = true; lblAddSuccessfull.Visible = false; lblEditSuccessfull.Visible = false; _log.Debug("Unable to Add a Coupon."); } catch (Exception ex) { lblError.Text = "Error saving image."; lblError.Visible = true; lblAddSuccessfull.Visible = false; lblEditSuccessfull.Visible = false; _log.DebugFormat("Unable to Add a Coupon. {0}", ex.Message); } //upload marketing image now only if no errors if (!lblError.Visible) { try { bool success = false; string err = ""; FileUpload img = (FileUpload)imgUploadMarketing; if (img == null) { err = "Bad image."; } else if (img.HasFile && img.PostedFile != null && !CheckFileType(img.PostedFile.FileName)) { err = "Sorry, Please only upload images for coupon."; } else if (img.HasFile && img.PostedFile != null && img.PostedFile.ContentLength > 10485760) { err = "Sorry, File size is too large. Advertisement image needs to be under 10MB."; } else if (img.HasFile && img.PostedFile != null) { //To create a PostedFile HttpPostedFile File = imgUploadMarketing.PostedFile; success = AddImageToAmazon(coupon, File, "_marketing"); } else { coupon.BottomAdvertisement = "keep"; success = couponDAO.UpdateCoupon(coupon); } if (success) { // Update was successfull lblEditSuccessfull.Visible = true; lblError.Visible = false; lblAddSuccessfull.Visible = false; //if everything went smooth, return to manufacturer page Response.Redirect("/manufacturer/default.aspx"); } else { if (err != "") lblError.Text = err; lblError.Visible = true; lblAddSuccessfull.Visible = false; lblEditSuccessfull.Visible = false; _log.DebugFormat("Unable to Update the Coupon. {0}", err); } } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { _log.Error("Please check the provided AWS Credentials."); } else { _log.Error(string.Format("An error occurred with the message '{0}' when writing an object", amazonS3Exception.Message)); } lblError.Text = "Error saving coupon to S3"; lblError.Visible = true; lblAddSuccessfull.Visible = false; lblEditSuccessfull.Visible = false; _log.Debug("Unable to Add a Coupon."); } catch (Exception ex) { lblError.Text = "Error saving marketing materials"; lblError.Visible = true; lblAddSuccessfull.Visible = false; lblEditSuccessfull.Visible = false; _log.DebugFormat("Unable to Add a Coupon. {0}", ex.Message); } } } catch (Exception ex) { _log.ErrorFormat("Exception Occurred: Exception={0}", ex.Message); lblError.Text = String.Format("Error saving coupon. {0}", ex.Message); lblError.Visible = true; lblAddSuccessfull.Visible = false; lblEditSuccessfull.Visible = false; } }
private bool AddImageToAmazon(Coupon coupon, HttpPostedFile File, string ext) { string count = ""; string keyname = ""; //ext = ".jpg"; //try //{ string accessKeyID = WebConfigurationManager.AppSettings["AWSAccessKey"]; string secretAccessKeyID = WebConfigurationManager.AppSettings["AWSSecretKey"]; AmazonS3Config s3Config = new AmazonS3Config(); s3Config.UseSecureStringForAwsSecretKey = false; client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID, s3Config); //count += "1"; ICouponDAO couponDAO = _factoryDAO.GetCouponDAO(); Byte[] imgByte = new byte[0]; //count += "2"; //Create byte Array with file len imgByte = new Byte[File.ContentLength]; //count += "3"; //force the control to load data in array File.InputStream.Read(imgByte, 0, File.ContentLength); //count += "4"; //count += ext; //count += _merchantname; //count += _couponID.ToString(); //count += File.FileName; //count += File.FileName.Substring(0, File.FileName.IndexOf(".")); //count += ":"; //count += string.Format("{0}{1}", _couponID.ToString(), ext); keyname = string.Format("{0}/{1}", _merchantname, File.FileName.Replace(File.FileName.Substring(0, File.FileName.IndexOf(".")), string.Format("{0}{1}", _couponID.ToString(), ext))); //keyname = string.Format("{0}/{1}", _merchantname, File.FileName.Replace(File.FileName.Substring(0, File.FileName.IndexOf(".")), _couponID.ToString())); count += keyname; /*try { //first try deleting old item if applicable DeleteObjectRequest delete_request = new DeleteObjectRequest(); delete_request.WithBucketName("gfck").WithKey(string.Format("coupon/{0}", keyname)); S3Response delete_response = client.DeleteObject(delete_request); delete_response.Dispose(); } catch (Exception ex) { _log.ErrorFormat("Error trying to delete object from bucket: {0} with exception {1}", keyname, ex.Message); }*/ string BUCKET_NAME = String.Format("gfck/coupon/{0}", _merchantname); try { //add the bucket just in case not already there. //PutObjectRequest request1 = new PutObjectRequest(); PutBucketRequest req = new PutBucketRequest(); req.WithBucketName(BUCKET_NAME); //count += "6"; //request1.WithBucketName("gfck").WithContentType(File.ContentType).WithCannedACL(S3CannedACL.PublicReadWrite).WithKey(BUCKET_NAME); //S3Response response = client.PutObject(request1); //response.Dispose(); client.PutBucket(req); } catch (Exception ex) { _log.DebugFormat("This bucket already exists: {0} with exception {1}", BUCKET_NAME, ex.Message); } //count += "5"; PutObjectRequest request = new PutObjectRequest(); //count += "6"; request.WithBucketName("gfck").WithContentType(File.ContentType).WithCannedACL(S3CannedACL.PublicRead).WithKey(string.Format("coupon/{0}", keyname)).WithInputStream(File.InputStream); count += "here"; S3Response response = client.PutObject(request); count += "7"; response.Dispose(); count += "8"; if (ext == "") { coupon.Image = keyname; } else { coupon.BottomAdvertisement = keyname; } count += "9"; return couponDAO.UpdateCoupon(coupon); /*} catch (Exception ex) { _log.ErrorFormat("Exception Occurred: Exception={0}", ex.Message); lblError.Text = String.Format("Error with coupon image. {0} {1} {2}", count, keyName, ex.Message); lblError.Visible = true; lblAddSuccessfull.Visible = false; lblEditSuccessfull.Visible = false; return false; }*/ }