예제 #1
0
        public ActionResult EditAvatar(UserEditAvatarViewModel model, HttpPostedFileBase avatar)
        {
            if (!Request.IsAuthenticated)
                throw new AuthenticationException();

            if (!ModelState.IsValid)
                return View(model);

            var user = DataService.PerThread.BaseUserSet.OfType<User>().Single(u => u.Id == UserContext.Current.Id);

            string fileName;
            const string avatarPath = "~/MediaContent/Avatars/";

            if (UploadImageValidationService.ValidateImageAndGetNewName(avatar, out fileName))
            {
                var path = Path.Combine(Server.MapPath(avatarPath), fileName);

                ImageService.DeleteImage<User>(model.AvatarImageName);

                avatar.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(10485760); // Ограничение по размеру в 10Мб
                    br.Close();
                }
                wResp.Close();

                if (UploadImageValidationService.ValidateImageTypeAndGetNewName(wResp, out fileName))
                {
                    ImageService.DeleteImage<Group>(model.AvatarImageName);

                    var path = Path.Combine(Server.MapPath(avatarPath), 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 ValidationException("Укажите источник");

            user.Avatar = fileName;

            DataService.PerThread.SaveChanges();
            UserContext.Abandon();

            return RedirectToAction("editavatar");
        }
예제 #2
0
        public ActionResult CropAvatar(UserEditAvatarViewModel model)
        {
            if (!Request.IsAuthenticated)
                throw new AuthenticationException();

            if (ModelState.IsValid)
            {
                var user = DataService.PerThread.BaseUserSet.OfType<User>().Single(u => u.Id == UserContext.Current.Id);

                var newImage = ImageService.CropExistedImage<User>(model.AvatarImageName, model.X1, model.Y1, model.X2, model.Y2);
                ImageService.DeleteImage<User>(model.AvatarImageName);

                user.Avatar = newImage;
                DataService.PerThread.SaveChanges();
                UserContext.Abandon();

                return RedirectToAction("editavatar");
            }

            return View("EditAvatar");
        }