public ActionResult Edit(Felbook.Models.User model)
{
TryUpdateModel(CurrentUser);
//upload změna profilového obrázku
Felbook.Helpers.ImageHelper imageOperator = new Felbook.Helpers.ImageHelper(); //pomocná třída pro operace s obrázky
HttpPostedFileBase imageToUpload = Request.Files["profileimage"];
int userId = CurrentUser.Id;
string fileDir = "../Web_Data/profile_images/";
//název souboru je vždy stejný
string fileName = "profileimage.png";
string fileFullPath = Path.Combine(HttpContext.Server.MapPath(fileDir + userId), fileName);
string fileDirPath = Path.GetDirectoryName(fileFullPath);
bool uploadImage = false;
if (imageToUpload.ContentLength == 0)
{
uploadImage = false;
}
else if (Felbook.Helpers.ImageHelper.IsImage(imageToUpload.ContentType))
{
uploadImage = true;
}
else
{
ModelState.AddModelError("file", "Your file wasn't image.");
}
if (model.OldPassword != null && String.Equals(model.Password, model.ConfirmPassword))
{
if (CurrentUser.CheckPassword(model.OldPassword))
{
CurrentUser.ChangePassword(model.Password);
}
else
{
ModelState.AddModelError("", "The password provided is incorrect.");
}
}
if (ModelState.IsValid)
{
if (uploadImage == true)
{
try
{
System.IO.File.Delete(fileFullPath);
}
catch (Exception)
{
ModelState.AddModelError("file", "Unexpected file error.");
return View("Index", "Profile", new { username = model.Username });
}
imageOperator.ImageResize(imageToUpload, fileFullPath, 90, 120);
}
Model.UserService.Edit(CurrentUser);
return View("Index", "Profile", new { username = model.Username });
}
//v případě nějaké chyby se vrátí tohle
return View("Edit", "../Profile", new { username = model.Username });
}