/// <summary> /// This method uploads an avatar image /// </summary> /// <param name="server">HttpServerUtilityBase object. Used to map the server path</param> /// <param name="avatar">Contains the file bytes to save</param> /// <param name="id">The id to use for the image</param> /// <param name="avatarType">Currently canbe either Team or Player</param> /// <returns></returns> public static AvatarImage UploadFile2(HttpServerUtilityBase server, HttpPostedFileBase avatar, int id, AvatarTypeEnum avatarType) { try { String fileType = Path.GetExtension(avatar.FileName); String name = id + fileType; AvatarImage image = null; switch (avatar.ContentType) { //Only accepct these content types case "image/gif": case "image/png": case "image/jpg": case "image/jpeg": break; //invalid file type default: throw new InvalidFileTypeException("Invalid File Type"); } switch (avatarType) { case AvatarTypeEnum.Team: image = new TeamAvatarImage(server, name); break; case AvatarTypeEnum.Player: image = new PlayerAvatarImage(server, name); break; } try { String absPath = image.AbsolutePath; avatar.SaveAs(absPath); } catch (Exception e) { //log throw new FileUploadException("There was an error uploading the file"); } return image; } catch (Exception e) { throw new GeneralException(); } }
public ActionResult Delete(int id) { var player = _playerDb.Find(id); if (player == null) return HttpNotFound(); PlayerAvatarImage image = new PlayerAvatarImage(Server, player.AvatarName); PlayerViewModel viewmodel = player.ToPlayerViewModel(); viewmodel.Avatar = image.Exists ? image.RelativePath : null; return View(viewmodel); }
public ActionResult Edit(PlayerViewModel viewmodel, HttpPostedFileBase avatar) { if (ModelState.IsValid) { Player currentPlayer = _playerDb.Find(viewmodel.ID); if (currentPlayer == null) return HttpNotFound(); currentPlayer.Name = viewmodel.Name; currentPlayer.RBI = viewmodel.RBI; currentPlayer.HomeRun = viewmodel.HomeRuns; currentPlayer.Average = viewmodel.Average; currentPlayer.TeamId = viewmodel.TeamId; try { _playerDb.Update(currentPlayer); if (avatar != null && avatar.ContentLength > 0) { PlayerAvatarImage currentImage = new PlayerAvatarImage(Server, currentPlayer.AvatarName); try { System.IO.File.Delete(currentImage.AbsolutePath); } catch (Exception e) { // } var image = AvatarUploader.UploadFile2(Server, avatar, currentPlayer.ID, AvatarTypeEnum.Player); currentPlayer.AvatarName = image.Name; _playerDb.Update(currentPlayer);//updates the new image file name } return RedirectToAction("Details", new { id = currentPlayer.ID }); } catch (CustomExceptionBase e) { ModelState.AddModelError("", e.Message); } catch (Exception e) { //log error ModelState.AddModelError("", GenericErrorMessage); } } viewmodel.TeamSelectList = DropDownHelper.BuildTeamSelectList(_teamDb); return View(viewmodel); }
public ActionResult Edit(int id) { var player = _playerDb.Find(id); if (player == null) return HttpNotFound(); PlayerAvatarImage image = new PlayerAvatarImage(Server, player.AvatarName); PlayerViewModel viewmodel = player.ToPlayerViewModel(); viewmodel.Avatar = image.Exists ? image.RelativePath : null; viewmodel.TeamSelectList = DropDownHelper.BuildTeamSelectList(_teamDb); return View(viewmodel); }
public ActionResult Details(int id) { Player model = _playerDb.Find(id);//can cause Exception. Currently bubbles up the the default error handler. It may be better do display a more specific error message in this method. if (model == null) return HttpNotFound(); //PlayerAvatarImage holds the absolute and relative paths to the image PlayerAvatarImage image = new PlayerAvatarImage(Server, model.AvatarName); PlayerViewModel viewmodel = model.ToPlayerViewModel(); viewmodel.Avatar = image.Exists ? image.RelativePath : null; return View(viewmodel); }
public ActionResult Delete(PlayerViewModel viewmodel) { try { var player = _playerDb.Find(viewmodel.ID); if (player == null) return HttpNotFound(); PlayerAvatarImage image = new PlayerAvatarImage(Server, player.AvatarName); try { System.IO.File.Delete(image.AbsolutePath); } catch (Exception e) { //log error... //continue this method } } catch (Exception e) { //log error ModelState.AddModelError("", GenericErrorMessage); return View(viewmodel); } try { _playerDb.Delete(viewmodel.ID); } catch (Exception e) { //log error, redirect to error view isntead ModelState.AddModelError("", GenericDatabaseError); return View(viewmodel); } return RedirectToAction("Index"); }
public ActionResult Delete(TeamViewModel viewmodel) { try { var team = _teamDb.Find(viewmodel.ID); if (team == null) return HttpNotFound(); //if there is a current image then delete it if (!String.IsNullOrEmpty(team.AvatarName)) { TeamAvatarImage image = new TeamAvatarImage(Server, team.AvatarName); System.IO.File.Delete(image.AbsolutePath); } } catch (Exception e) { //log error... ModelState.AddModelError("", GenericErrorMessage); return View(viewmodel); } try { var playersOnTeam = _playerDb.FindAll().Where(x => x.TeamId == viewmodel.ID).ToList(); playersOnTeam.ForEach(x => { try { //delete this player from the database _playerDb.Delete(x.ID); //delete the players avatar PlayerAvatarImage image = new PlayerAvatarImage(Server, x.AvatarName); if(!String.IsNullOrEmpty(x.AvatarName))System.IO.File.Delete(image.AbsolutePath); } catch (Exception e) { //log error... } });//delete all players on the team first _teamDb.Delete(viewmodel.ID); } catch (Exception e) { //log error return Content("Error");//temp } return RedirectToAction("Index"); }