public ActionResult EditLogo(GroupEditLogoViewModel model, HttpPostedFileBase logo) { if (!Request.IsAuthenticated) throw new AuthenticationException(); var group = GroupService.GetGroupByLabelOrId(model.GroupUrl); var redirect = HideInvisibleGroup(group); if (redirect != null) return redirect; if (GroupService.UserInGroup(UserContext.Current.Id, group.Id).State != (byte)GroupMemberState.Moderator) throw new BusinessLogicException("Вы не являетесь модератором данной группы"); if (ModelState.IsValid) { string fileName; const string logoPath = "~/MediaContent/GroupLogos/"; if (UploadImageValidationService.ValidateImageAndGetNewName(logo, out fileName)) { var path = Path.Combine(Server.MapPath(logoPath), fileName); ImageService.DeleteImage<Group>(model.LogoImageName); logo.SaveAs(path); } else if (!string.IsNullOrWhiteSpace(model.Url)) { byte[] content; var hwReq = (HttpWebRequest)WebRequest.Create(model.Url); var wResp = hwReq.GetResponse(); var stream = wResp.GetResponseStream(); if (stream == null) throw new BusinessLogicException("Произошла ошибка загрузки"); using (var br = new BinaryReader(stream)) { content = br.ReadBytes(5242880); // Ограничение по размеру в 5Мб br.Close(); } wResp.Close(); if (UploadImageValidationService.ValidateImageTypeAndGetNewName(wResp, out fileName)) { ImageService.DeleteImage<Group>(model.LogoImageName); var path = Path.Combine(Server.MapPath(logoPath), fileName); var fs = new FileStream(path, FileMode.Create); var w = new BinaryWriter(fs); try { w.Write(content); } finally { fs.Close(); w.Close(); } } } else throw new BusinessLogicException("Укажите источник"); group.Logo = fileName; DataService.PerThread.SaveChanges(); return RedirectToAction("editlogo"); } return View(model); }
public ActionResult CropLogo(GroupEditLogoViewModel model) { if (!Request.IsAuthenticated) throw new AuthenticationException(); var group = GroupService.GetGroupByLabelOrId(model.GroupUrl); var redirect = HideInvisibleGroup(group); if (redirect != null) return redirect; if (GroupService.UserInGroup(UserContext.Current.Id, group.Id).State != (byte)GroupMemberState.Moderator) throw new BusinessLogicException("Вы не являетесь модератором данной группы"); if (ModelState.IsValid) { var newImage = ImageService.CropExistedImage<Group>(model.LogoImageName, model.X1, model.Y1, model.X2, model.Y2); ImageService.DeleteImage<Group>(model.LogoImageName); group.Logo = newImage; DataService.PerThread.SaveChanges(); return RedirectToAction("editlogo", new { id = group.Url }); } return RedirectToAction("editlogo", new { id = group.Url }); }