public FileResult Download(Guid id) { var uploadedFileById = _uploadedFileService.Get(id); if (uploadedFileById != null) { var loggedOnReadOnlyUser = User.GetMembershipUser(MembershipService); var loggedOnUsersRole = loggedOnReadOnlyUser.GetRole(RoleService); // Check the user has permission to download this file var fileCategory = uploadedFileById.Post.Topic.Category; var allowedCategoryIds = _categoryService.GetAllowedCategories(loggedOnUsersRole).Select(x => x.Id); if (allowedCategoryIds.Contains(fileCategory.Id)) { //if(AppHelpers.FileIsImage(uploadedFileById.FilePath)) //{ //} var fileBytes = System.IO.File.ReadAllBytes(HostingEnvironment.MapPath(uploadedFileById.FilePath)); return(File(fileBytes, MediaTypeNames.Application.Octet, uploadedFileById.Filename)); } } return(null); }
public virtual ActionResult DeleteUploadedFile(Guid id) { if (id != Guid.Empty) { Topic topic = null; try { User.GetMembershipUser(MembershipService); var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService); // Get the file and associated objects we'll need var uploadedFile = _uploadedFileService.Get(id); var post = uploadedFile.Post; topic = post.Topic; if (loggedOnUsersRole.RoleName == Constants.AdminRoleName || uploadedFile.MembershipUser.Id == LoggedOnReadOnlyUser?.Id) { // Ok to delete file // Remove it from the post post.Files.Remove(uploadedFile); // store the file path as we'll need it to delete on the file system var filePath = uploadedFile.FilePath; // Now delete it _uploadedFileService.Delete(uploadedFile); // And finally delete from the file system System.IO.File.Delete(HostingEnvironment.MapPath(filePath)); } else { TempData[Constants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("Errors.NoPermission"), MessageType = GenericMessages.danger }; Redirect(topic.NiceUrl); } //Commit Context.SaveChanges(); TempData[Constants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("File.SuccessfullyDeleted"), MessageType = GenericMessages.success }; return(Redirect(topic.NiceUrl)); } 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"))); } } return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage"))); }
public ActionResult DeleteUploadedFile(Guid id) { if (id != Guid.Empty) { using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { Topic topic = null; try { // Get the file and associated objects we'll need var uploadedFile = _uploadedFileService.Get(id); var post = uploadedFile.Post; topic = post.Topic; if (UsersRole.RoleName == AppConstants.AdminRoleName || uploadedFile.MembershipUser.Id == LoggedOnUser.Id) { // Ok to delete file // Remove it from the post post.Files.Remove(uploadedFile); // store the file path as we'll need it to delete on the file system var filePath = uploadedFile.FilePath; // Now delete it _uploadedFileService.Delete(uploadedFile); // And finally delete from the file system System.IO.File.Delete(Server.MapPath(filePath)); } else { TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("Errors.NoPermission"), MessageType = GenericMessages.error }; Redirect(topic.NiceUrl); } //Commit unitOfWork.Commit(); TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("File.SuccessfullyDeleted"), 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.error }; return(topic != null?Redirect(topic.NiceUrl) : ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage"))); } } } return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage"))); }