コード例 #1
0
        public async Task <ActionResult> UserPreferencesAbout([Bind(Include = "Bio, Avatarfile")] UserAboutViewModel model)
        {
            ViewBag.UserName = User.Identity.Name;
            // save changes
            using (var db = new voatEntities())
            {
                var userPreferences = GetUserPreference(db);

                if (model.Avatarfile != null && model.Avatarfile.ContentLength > 0)
                {
                    // check uploaded file size is < 300000 bytes (300 kilobytes)
                    if (model.Avatarfile.ContentLength < 300000)
                    {
                        try
                        {
                            using (var img = Image.FromStream(model.Avatarfile.InputStream))
                            {
                                if (img.RawFormat.Equals(ImageFormat.Jpeg) || img.RawFormat.Equals(ImageFormat.Png))
                                {
                                    // resize uploaded file
                                    var thumbnailResult = await ThumbGenerator.GenerateAvatar(img, User.Identity.Name, model.Avatarfile.ContentType);

                                    if (thumbnailResult)
                                    {
                                        userPreferences.Avatar = User.Identity.Name + ".jpg";
                                    }
                                    else
                                    {
                                        // unable to generate thumbnail
                                        ModelState.AddModelError("", "Uploaded file is not recognized as a valid image.");
                                        return(RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat }));
                                    }
                                }
                                else
                                {
                                    // uploaded file was invalid
                                    ModelState.AddModelError("", "Uploaded file is not recognized as an image.");
                                    return(RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat }));
                                }
                            }
                        }
                        catch (Exception)
                        {
                            // uploaded file was invalid
                            ModelState.AddModelError("", "Uploaded file is not recognized as an image.");
                            return(RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat }));
                        }
                    }
                    else
                    {
                        // refuse to save the file and explain why
                        ModelState.AddModelError("", "Uploaded image may not exceed 300 kb, please upload a smaller image.");
                        return(RedirectToAction("Manage", new { Message = ManageMessageId.UploadedFileToolarge }));
                    }
                }

                var bio = model.Bio.TrimSafe();

                if (String.IsNullOrEmpty(bio))
                {
                    userPreferences.Bio = "I tried to delete my bio but they gave me this instead";
                }
                else if (bio == STRINGS.DEFAULT_BIO)
                {
                    userPreferences.Bio = null;
                }
                else
                {
                    userPreferences.Bio = bio;
                }
                await db.SaveChangesAsync();
            }

            ClearUserCache();

            return(RedirectToAction("Manage"));
        }
コード例 #2
0
ファイル: AccountController.cs プロジェクト: LeonSuper/voat
        public async Task <ActionResult> UserPreferencesAbout([Bind(Include = "Shortbio, Avatarfile")] UserAboutViewModel model)
        {
            // save changes
            using (var db = new whoaverseEntities())
            {
                var userPreferences = db.Userpreferences.Find(User.Identity.Name);
                var tmpModel        = new Userpreference();

                if (userPreferences == null)
                {
                    // create a new record for this user in userpreferences table
                    tmpModel.Shortbio = model.Shortbio;
                    tmpModel.Username = User.Identity.Name;
                }

                if (model.Avatarfile != null && model.Avatarfile.ContentLength > 0)
                {
                    // check uploaded file size is < 300000 bytes (300 kilobytes)
                    if (model.Avatarfile.ContentLength < 300000)
                    {
                        try
                        {
                            using (var img = Image.FromStream(model.Avatarfile.InputStream))
                            {
                                if (img.RawFormat.Equals(ImageFormat.Jpeg) || img.RawFormat.Equals(ImageFormat.Png))
                                {
                                    // resize uploaded file
                                    ThumbGenerator.GenerateAvatar(img, User.Identity.Name, model.Avatarfile.ContentType);
                                    if (userPreferences == null)
                                    {
                                        tmpModel.Avatar = User.Identity.Name + ".jpg";
                                    }
                                    else
                                    {
                                        userPreferences.Avatar = User.Identity.Name + ".jpg";
                                    }
                                }
                                else
                                {
                                    // uploaded file was invalid
                                    ModelState.AddModelError("", "Uploaded file is not recognized as an image.");
                                    return(RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat }));
                                }
                            }
                        }
                        catch (Exception)
                        {
                            // uploaded file was invalid
                            ModelState.AddModelError("", "Uploaded file is not recognized as an image.");
                            return(RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat }));
                        }
                    }
                    else
                    {
                        // refuse to save the file and explain why
                        ModelState.AddModelError("", "Uploaded image may not exceed 300 kb, please upload a smaller image.");
                        return(RedirectToAction("Manage", new { Message = ManageMessageId.UploadedFileToolarge }));
                    }
                }

                if (userPreferences == null)
                {
                    db.Userpreferences.Add(tmpModel);
                    await db.SaveChangesAsync();
                }
                else
                {
                    userPreferences.Shortbio = model.Shortbio;
                    userPreferences.Username = User.Identity.Name;
                    await db.SaveChangesAsync();
                }
            }

            return(RedirectToAction("Manage"));
        }
コード例 #3
0
        public async Task <ActionResult> UserPreferencesAbout([Bind("Bio, Avatarfile")] UserAboutViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("Manage", model));
            }

            ViewBag.UserName = User.Identity.Name;

            string avatarKey = null;

            //ThumbGenerator.GenerateThumbnail
            if (model.Avatarfile != null)
            {
                try
                {
                    var stream = model.Avatarfile.OpenReadStream();
                    var result = await ThumbGenerator.GenerateAvatar(stream, model.Avatarfile.FileName, model.Avatarfile.ContentType);

                    if (result.Success)
                    {
                        avatarKey = result.Response;
                    }
                    else
                    {
                        ModelState.AddModelError("Avatarfile", result.Message);
                        return(View("Manage", model));
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Avatarfile", "Uploaded file is not recognized as a valid image.");
                    return(RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat }));
                }
            }

            var bio = model.Bio.TrimSafe();

            //This is a hack
            var context = new VoatOutOfRepositoryDataContextAccessor();

            using (var repo = new Repository(User, context))
            {
                var p = await repo.GetUserPreferences(User.Identity.Name);

                if (bio != p.Bio)
                {
                    if (String.IsNullOrEmpty(bio))
                    {
                        p.Bio = "I tried to delete my bio but they gave me this instead";
                    }
                    else if (bio == STRINGS.DEFAULT_BIO)
                    {
                        p.Bio = null;
                    }
                    else
                    {
                        p.Bio = bio;
                    }
                }
                if (!String.IsNullOrEmpty(avatarKey))
                {
                    p.Avatar = avatarKey;
                }
                await context.SaveChangesAsync();
            }

            /*
             * using (var db = new VoatUIDataContextAccessor())
             * {
             *  var userPreferences = GetUserPreference(db);
             *
             *  if (model.Avatarfile != null && model.Avatarfile.ContentLength > 0)
             *  {
             *      // check uploaded file size is < 300000 bytes (300 kilobytes)
             *      if (model.Avatarfile.ContentLength < 300000)
             *      {
             *          try
             *          {
             *              using (var img = Image.FromStream(model.Avatarfile.InputStream))
             *              {
             *                  if (img.RawFormat.Equals(ImageFormat.Jpeg) || img.RawFormat.Equals(ImageFormat.Png))
             *                  {
             *                      // resize uploaded file
             *                      var thumbnailResult = await ThumbGenerator.GenerateAvatar(img, User.Identity.Name, model.Avatarfile.ContentType);
             *                      if (thumbnailResult)
             *                      {
             *                          userPreferences.Avatar = User.Identity.Name + ".jpg";
             *                      }
             *                      else
             *                      {
             *                          // unable to generate thumbnail
             *                          ModelState.AddModelError("", "Uploaded file is not recognized as a valid image.");
             *                          return RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat });
             *                      }
             *                  }
             *                  else
             *                  {
             *                      // uploaded file was invalid
             *                      ModelState.AddModelError("", "Uploaded file is not recognized as an image.");
             *                      return RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat });
             *                  }
             *              }
             *          }
             *          catch (Exception)
             *          {
             *              // uploaded file was invalid
             *              ModelState.AddModelError("", "Uploaded file is not recognized as an image.");
             *              return RedirectToAction("Manage", new { Message = ManageMessageId.InvalidFileFormat });
             *          }
             *      }
             *      else
             *      {
             *          // refuse to save the file and explain why
             *          ModelState.AddModelError("", "Uploaded image may not exceed 300 kb, please upload a smaller image.");
             *          return RedirectToAction("Manage", new { Message = ManageMessageId.UploadedFileToolarge });
             *      }
             *  }
             *
             *  var bio = model.Bio.TrimSafe();
             *
             *  if (String.IsNullOrEmpty(bio))
             *  {
             *      userPreferences.Bio = "I tried to delete my bio but they gave me this instead";
             *  }
             *  else if (bio == STRINGS.DEFAULT_BIO)
             *  {
             *      userPreferences.Bio = null;
             *  }
             *  else
             *  {
             *      userPreferences.Bio = bio;
             *  }
             *  await db.SaveChangesAsync();
             * }
             */
            ClearUserCache();

            return(RedirectToAction("Manage"));
        }