/// <summary> /// Save photo to disk, used by Edit and Register with two different models. model.Photo, this, string model.CategoryName, string model.SubCategoryName /// </summary> /// <param name="photo">HttpPostedFileWrapper</param> /// <param name="controller">Controller calling</param> /// <returns>Path where photo is stored with it's calculated filename, or default photo "BlankPhoto.jpg" or null on error</returns> public static string[] SaveAcdfPhotoFileToDisk(System.Web.Helpers.WebImage webImage, System.Web.Mvc.Controller controller, string categoryName, string subCategoryName) { string[] paths = new string[2]; string photoPath = string.Empty; string thumbPath = string.Empty; string fileName = string.Empty; string thumbFileName = string.Empty; string subCatName = subCategoryName.Contains("AUCUNE") ? string.Empty : subCategoryName; if (webImage != null) { // If photo is uploaded calculate his name string guid = Guid.NewGuid().ToString(); fileName = guid + "_" + webImage.FileName; thumbFileName = guid + "_thumb_" + webImage.FileName; //Calculate photoUrl and save photo on disk try { string path = HttpContext.Current.Request.ApplicationPath + "/Medias/_Photos/" + categoryName + @"\" + subCatName; //string path = @"~/Medias\_Photos\" + categoryName + @"\" + subCatName; //string path = @"/ACDF/Medias\_Photos\"; string filePath = Path.Combine(controller.Server.MapPath(path), fileName); webImage.AddTextWatermark("http://jow-alva.net/ACDF", "White", 15); webImage.Save(filePath); photoPath = Path.Combine(HttpRuntime.AppDomainAppVirtualPath, path, fileName); string thumbFilePath = Path.Combine(controller.Server.MapPath(path), thumbFileName); webImage.Resize(150, 150, preserveAspectRatio: true).Save(thumbFilePath); thumbPath = Path.Combine(HttpRuntime.AppDomainAppVirtualPath, path, thumbFileName); paths[0] = photoPath; paths[1] = thumbPath; } catch (Exception ex) { // Handled exception catch code Helpers.Utils.SignalExceptionToElmahAndTrace(ex, controller); throw ex; //return null; } } else { // Handled exception catch code //TODO: Helpers.Utils.SignalExceptionToElmahAndTrace(null, controller); return(null); } return(paths); }
public async Task <ActionResult> Create(IssueVM model) { if (!ModelState.IsValid) { ModelState.AddModelError("", "Hata Oluştu."); return(RedirectToAction("Create", "Issue", model)); } try { var id = HttpContext.GetOwinContext().Authentication.User.Identity.GetUserId(); var user = NewUserManager().FindById(id); var issue = new Issue() { Description = model.Description, IssueState = model.IssueState, Location = model.Location == Models.Enums.Locations.KonumYok ? user.Location : model.Location, ProductType = model.ProductType, CustomerId = model.CustomerId, PurchasedDate = model.PurchasedDate, PhotoPath = model.PhotoPath, ServiceCharge = model.ServiceCharge, ClosedDate = model.ClosedDate, CreatedDate = model.CreatedDate, OperatorId = model.OperatorId, TechReport = model.TechReport }; switch (issue.ProductType) { case Models.Enums.ProductTypes.Buzdolabı: if (issue.PurchasedDate.AddYears(1) > DateTime.Now) { issue.WarrantyState = true; } break; case Models.Enums.ProductTypes.BulaşıkMakinesi: if (issue.PurchasedDate.AddYears(2) > DateTime.Now) { issue.WarrantyState = true; } break; case Models.Enums.ProductTypes.Fırın: if (issue.PurchasedDate.AddYears(3) > DateTime.Now) { issue.WarrantyState = true; } break; case Models.Enums.ProductTypes.ÇamaşırMakinesi: if (issue.PurchasedDate.AddYears(4) > DateTime.Now) { issue.WarrantyState = true; } break; case Models.Enums.ProductTypes.Mikrodalga: if (issue.PurchasedDate.AddYears(5) > DateTime.Now) { issue.WarrantyState = true; } break; default: if (issue.PurchasedDate.AddYears(2) > DateTime.Now) { issue.WarrantyState = true; } break; } if (issue.WarrantyState) { issue.ServiceCharge = 0; } var repo = new IssueRepo(); repo.Insert(issue); var fotorepo = new PhotographRepo(); if (model.PostedPhoto.Count > 0) { model.PostedPhoto.ForEach(file => { if (file == null || file.ContentLength <= 0) { var filepath2 = Server.MapPath("~/assets/images/image-not-available.png"); var img2 = new WebImage(filepath2); img2.Resize(250, 250, false); img2.Save(filepath2); fotorepo.Insert(new Photograph() { IssueId = issue.Id, Path = "/assets/images/image-not-available.png" }); return; } var fileName = Path.GetFileNameWithoutExtension(file.FileName); var extName = Path.GetExtension(file.FileName); fileName = StringHelpers.UrlFormatConverter(fileName); fileName += StringHelpers.GetCode(); var directorypath = Server.MapPath("~/Upload/"); var filepath = Server.MapPath("~/Upload/") + fileName + extName; if (!Directory.Exists(directorypath)) { Directory.CreateDirectory(directorypath); } file.SaveAs(filepath); var img = new WebImage(filepath); img.Resize(250, 250, false); img.Save(filepath); fotorepo.Insert(new Photograph() { IssueId = issue.Id, Path = "/Upload/" + fileName + extName }); }); } var fotograflar = fotorepo.GetAll(x => x.IssueId == issue.Id).ToList(); var foto = fotograflar.Select(x => x.Path).ToList(); issue.PhotoPath = foto; repo.Update(issue); TempData["Message"] = "Arıza kaydınız başarı ile oluşturuldu."; var emailService = new EmailService(); var body = $"Merhaba <b>{user.Name} {user.Surname}</b><br>Arıza kaydınız başarıyla oluşturuldu.Birimlerimiz sorunu çözmek için en kısa zamanda olay yerine intikal edecektir.<br><br> Ayrıntılı bilgi için telefon numaramız:<i>0212 684 75 33</i>"; await emailService.SendAsync(new IdentityMessage() { Body = body, Subject = "Arıza kaydı oluşturuldu." }, user.Email); var issueLog = new IssueLog() { IssueId = issue.Id, Description = "Arıza Kaydı Oluşturuldu.", FromWhom = "Müşteri" }; new IssueLogRepo().Insert(issueLog); return(RedirectToAction("Index", "Issue")); } catch (DbEntityValidationException ex) { TempData["Message3"] = new ErrorVM() { Text = $"Bir hata oluştu: {EntityHelpers.ValidationMessage(ex)}", ActionName = "Create", ControllerName = "Issue", ErrorCode = 500 }; return(RedirectToAction("Error500", "Home")); } catch (Exception ex) { TempData["Message2"] = new ErrorVM() { Text = $"Bir hata oluştu {ex.Message}", ActionName = "Create", ControllerName = "Issue", ErrorCode = 500 }; return(RedirectToAction("Error500", "Home")); } }