Ejemplo n.º 1
0
 public UploadedFile Add(UploadedFile uploadedFile)
 {
     uploadedFile.DateCreated = DateTime.UtcNow;
     return ContextPerRequest.Db.UploadedFile.Add(uploadedFile);
 }
Ejemplo n.º 2
0
 public void Delete(UploadedFile uploadedFile)
 {
     ContextPerRequest.Db.UploadedFile.Remove(uploadedFile);
 }
        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 = ServiceFactory.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 = ServiceFactory.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 = ServiceFactory.PermissionService.GetPermissions(category, _membersGroup);
                            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 = AppHelpers.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
                                    };
                                    ServiceFactory.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"));
        }