public virtual ActionResult UploadPostFiles(AttachFileToPostViewModel attachFileToPostViewModel) { var topic = new Topic(); try { User.GetMembershipUser(MembershipService); var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService); // First this to do is get the post var post = _postService.Get(attachFileToPostViewModel.UploadPostId); if (post != null) { // Now get the topic topic = post.Topic; // Check we get a valid post back and have some file if (attachFileToPostViewModel.Files != null && attachFileToPostViewModel.Files.Any()) { // Now get the Group var Group = topic.Group; // Get the permissions for this Group, and check they are allowed to update and // not trying to be a sneaky mofo var permissions = RoleService.GetPermissions(Group, loggedOnUsersRole); if (permissions[ForumConfiguration.Instance.PermissionAttachFiles].IsTicked == false) { TempData[Constants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("Errors.NoPermission"), MessageType = GenericMessages.danger }; return(Redirect(topic.NiceUrl)); } // woot! User has permission and all seems ok // Before we save anything, check the user already has an upload folder and if not create one var uploadFolderPath = HostingEnvironment.MapPath( string.Concat(ForumConfiguration.Instance.UploadFolderPath, LoggedOnReadOnlyUser?.Id)); if (!Directory.Exists(uploadFolderPath)) { Directory.CreateDirectory(uploadFolderPath); } // Loop through each file and get the file info and save to the users folder and Db foreach (var file in attachFileToPostViewModel.Files) { if (file != null) { // If successful then upload the file var uploadResult = file.UploadFile(uploadFolderPath, LocalizationService); if (!uploadResult.UploadSuccessful) { TempData[Constants.MessageViewBagName] = new GenericMessageViewModel { Message = uploadResult.ErrorMessage, MessageType = GenericMessages.danger }; return(Redirect(topic.NiceUrl)); } // Add the filename to the database var loggedOnUser = MembershipService.GetUser(LoggedOnReadOnlyUser?.Id); var uploadedFile = new UploadedFile { Filename = uploadResult.UploadedFileName, Post = post, MembershipUser = loggedOnUser }; _uploadedFileService.Add(uploadedFile); } } //Commit Context.SaveChanges(); // Redirect to the topic with a success message TempData[Constants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("Post.FilesUploaded"), MessageType = GenericMessages.success }; return(Redirect(topic.NiceUrl)); } // Else return with error to home page return(topic != null ? Redirect(topic.NiceUrl) : ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage"))); } // Else return with error to home page return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage"))); } catch (Exception ex) { Context.RollBack(); LoggingService.Error(ex); TempData[Constants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("Errors.GenericMessage"), MessageType = GenericMessages.danger }; return(topic != null ? Redirect(topic.NiceUrl) : ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage"))); } }
public ActionResult UploadPostFiles(AttachFileToPostViewModel attachFileToPostViewModel) { using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { var topic = new Topic(); try { // First this to do is get the post var post = _postService.Get(attachFileToPostViewModel.UploadPostId); if (post != null) { // Now get the topic topic = post.Topic; // Check we get a valid post back and have some file if (attachFileToPostViewModel.Files != null && attachFileToPostViewModel.Files.Any()) { // Now get the category var category = topic.Category; // Get the permissions for this category, and check they are allowed to update and // not trying to be a sneaky mofo var permissions = RoleService.GetPermissions(category, UsersRole); if (permissions[SiteConstants.Instance.PermissionAttachFiles].IsTicked == false || LoggedOnReadOnlyUser.DisableFileUploads == true) { TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("Errors.NoPermission"), MessageType = GenericMessages.danger }; return Redirect(topic.NiceUrl); } // woot! User has permission and all seems ok // Before we save anything, check the user already has an upload folder and if not create one var uploadFolderPath = HostingEnvironment.MapPath(string.Concat(SiteConstants.Instance.UploadFolderPath, LoggedOnReadOnlyUser.Id)); if (!Directory.Exists(uploadFolderPath)) { Directory.CreateDirectory(uploadFolderPath); } // Loop through each file and get the file info and save to the users folder and Db foreach (var file in attachFileToPostViewModel.Files) { if (file != null) { // If successful then upload the file var uploadResult = AppHelpers.UploadFile(file, uploadFolderPath, LocalizationService); if (!uploadResult.UploadSuccessful) { TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = uploadResult.ErrorMessage, MessageType = GenericMessages.danger }; return Redirect(topic.NiceUrl); } // Add the filename to the database var loggedOnUser = MembershipService.GetUser(LoggedOnReadOnlyUser.Id); var uploadedFile = new UploadedFile { Filename = uploadResult.UploadedFileName, Post = post, MembershipUser = loggedOnUser }; _uploadedFileService.Add(uploadedFile); } } //Commit unitOfWork.Commit(); // Redirect to the topic with a success message TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("Post.FilesUploaded"), MessageType = GenericMessages.success }; return Redirect(topic.NiceUrl); } // Else return with error to home page return topic != null ? Redirect(topic.NiceUrl) : ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage")); } // Else return with error to home page return ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage")); } catch (Exception ex) { unitOfWork.Rollback(); LoggingService.Error(ex); TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("Errors.GenericMessage"), MessageType = GenericMessages.danger }; return topic != null ? Redirect(topic.NiceUrl) : ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage")); } } }
public ActionResult UploadPostFiles(AttachFileToPostViewModel attachFileToPostViewModel) { if (attachFileToPostViewModel != null && attachFileToPostViewModel.Files != null) { using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { // First this to do is get the post var post = _postService.Get(attachFileToPostViewModel.UploadPostId); // Check we get a valid post back and have some file if (post != null && attachFileToPostViewModel.Files != null) { Topic topic = null; try { // Now get the topic topic = post.Topic; // Now get the category var category = topic.Category; // Get the permissions for this category, and check they are allowed to update and // not trying to be a sneaky mofo var permissions = RoleService.GetPermissions(category, UsersRole); if (permissions[AppConstants.PermissionAttachFiles].IsTicked == false || LoggedOnUser.DisableFileUploads == true) { TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("Errors.NoPermission"), MessageType = GenericMessages.danger }; return(Redirect(topic.NiceUrl)); } // woot! User has permission and all seems ok // Before we save anything, check the user already has an upload folder and if not create one var uploadFolderPath = Server.MapPath(string.Concat(SiteConstants.UploadFolderPath, LoggedOnUser.Id)); if (!Directory.Exists(uploadFolderPath)) { Directory.CreateDirectory(uploadFolderPath); } // Loop through each file and get the file info and save to the users folder and Db foreach (var file in attachFileToPostViewModel.Files) { if (file != null) { // If successful then upload the file var uploadResult = AppHelpers.UploadFile(file, uploadFolderPath, LocalizationService); if (!uploadResult.UploadSuccessful) { TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = uploadResult.ErrorMessage, MessageType = GenericMessages.danger }; return(Redirect(topic.NiceUrl)); } // Add the filename to the database var uploadedFile = new UploadedFile { Filename = uploadResult.UploadedFileName, Post = post, MembershipUser = LoggedOnUser }; _uploadedFileService.Add(uploadedFile); } } //Commit unitOfWork.Commit(); // Redirect to the topic with a success message TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("Post.FilesUploaded"), MessageType = GenericMessages.success }; return(Redirect(topic.NiceUrl)); } catch (Exception ex) { unitOfWork.Rollback(); LoggingService.Error(ex); TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("Errors.GenericMessage"), MessageType = GenericMessages.danger }; return(topic != null?Redirect(topic.NiceUrl) : ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage"))); } } } } // Else return with error to home page return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage"))); }
public ActionResult UploadPostFiles(AttachFileToPostViewModel attachFileToPostViewModel) { if (attachFileToPostViewModel != null && attachFileToPostViewModel.Files != null) { using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { var message = new GenericMessageViewModel(); // First this to do is get the post var post = PostService.Get(attachFileToPostViewModel.UploadPostId); // Check we get a valid post back and have some file if (post != null && attachFileToPostViewModel.Files != null) { Topic topic = null; try { // Now get the topic topic = post.Topic; // Now get the category var category = CategoryService.Get(topic.CategoryId); // Get the permissions for this category, and check they are allowed to update and // not trying to be a sneaky mofo var permissions = PermissionService.GetPermissions(category, _membersGroup, MemberService, CategoryPermissionService); if (permissions[AppConstants.PermissionAttachFiles].IsTicked == false && CurrentMember.DisableFileUploads != true) { return(ErrorToHomePage(Lang("Errors.NoPermission"))); } // woot! User has permission and all seems ok // Before we save anything, check the user already has an upload folder and if not create one var uploadFolderPath = Server.MapPath(string.Concat(AppConstants.UploadFolderPath, CurrentMember.Id)); if (!Directory.Exists(uploadFolderPath)) { Directory.CreateDirectory(uploadFolderPath); } // Loop through each file and get the file info and save to the users folder and Db foreach (var file in attachFileToPostViewModel.Files) { if (file != null) { // If successful then upload the file var uploadResult = UploadedFileService.UploadFile(file, uploadFolderPath); if (!uploadResult.UploadSuccessful) { message.Message = uploadResult.ErrorMessage; message.MessageType = GenericMessages.Danger; ShowMessage(message); return(Redirect(topic.Url)); } // Add the filename to the database var uploadedFile = new UploadedFile { Filename = uploadResult.UploadedFileName, Post = post, MemberId = CurrentMember.Id }; UploadedFileService.Add(uploadedFile); } } //Commit unitOfWork.Commit(); // Redirect to the topic with a success message message.Message = Lang("Post.FilesUploaded"); message.MessageType = GenericMessages.Success; ShowMessage(message); return(Redirect(topic.Url)); } catch (Exception ex) { unitOfWork.Rollback(); LogError(ex); message.Message = Lang("Errors.GenericMessage"); message.MessageType = GenericMessages.Danger; ShowMessage(message); return(topic != null?Redirect(topic.Url) : ErrorToHomePage(Lang("Errors.GenericMessage"))); } } } } // Else return with error to home page return(ErrorToHomePage(Lang("Errors.GenericMessage"))); }