public void MoveAttachmentsFromTemporaryFolder(string temporaryAttachmentFolder, int threadId) { var attachmentsDirPath = HttpContext.Server.MapPath("~/Attachments/"); Directory.SetCurrentDirectory(attachmentsDirPath); var destinationDir = "Thread_" + threadId; Directory.Move(attachmentsDirPath + temporaryAttachmentFolder, attachmentsDirPath + destinationDir); var thread = Repository.Thread.Get(threadId); var post = new Post { Attachments = new List<Attachment>(), Author = thread.Author, Type = PostType.Attachment, Content = "Pytanie zawiera załączniki" }; var directoryInfo = new DirectoryInfo(attachmentsDirPath + destinationDir); foreach (var file in directoryInfo.GetFiles()) { var attachment = new Attachment { AttachmentName = file.Name, AttachmentPath = string.Format("\\Attachments\\{0}\\{1}", destinationDir, file.Name), Author = thread.Author, ContentType = file.Extension, AttachmentSize = (int)file.Length }; attachment.Type = UploadHelper.SpecifyAttachmentType(file.Extension); post.Attachments.Add(attachment); } Repository.Thread.AddPost(thread, post); }
public virtual ActionResult ThreadAttachmentUpload(int threadId) { var post = new Post { Attachments = new List<Attachment>() }; var user = AuthenticationHelper.CurrentUser; var thread = Repository.Thread.Get(threadId); if (!IsAuthorExpertOrModerator(thread)) { FormsAuthentication.RedirectToLoginPage(); return null; } for (int fileCount = 0; fileCount < Request.Files.Count; fileCount++) { var file = Request.Files[fileCount]; if (UploadHelper.ValidateUpload(file.ContentLength, file.ContentType, UploadHelper.FileType.PostAttachment)) { var attachmentCount = thread.Posts.Sum(p => p.Attachments.Count()); var attachmentPath = string.Format("\\Attachments\\Thread_{0}\\{1}{2}", thread.Id, attachmentCount, file.FileName); var attachmentsDirPath = HttpContext.Server.MapPath("~/Attachments/"); Directory.SetCurrentDirectory(attachmentsDirPath); Directory.CreateDirectory("Thread_" + thread.Id); var filePath = Path.Combine(HttpContext.Server.MapPath(attachmentPath)); file.SaveAs(filePath); if (fileCount == 0) { post.Author = user; post.Content = Request.Files.Count == 1 ? Resources.Attachments.AttachmentAdded : Resources.Attachments.AttachmentsAdded; post.Type = PostType.Attachment; } if (fileCount == 0 && thread.Posts.Last().Author == user && thread.Posts.Last().Type == PostType.Attachment) { post = thread.Posts.Last(); } var attachment = new Attachment { AttachmentName = file.FileName, AttachmentPath = attachmentPath, Author = user, ContentType = file.ContentType, AttachmentSize = file.ContentLength }; attachment.Type = UploadHelper.SpecifyAttachmentType(file.ContentType); if (fileCount == 0 && (thread.Posts.Last().Type != PostType.Attachment || thread.Posts.Last().Author != AuthenticationHelper.CurrentUser)) { Repository.Thread.AddPost(thread, post); } post.Attachments.Add(attachment); Repository.Thread.Update(thread); Flash.Success(Resources.Attachments.AttachmentAdded + " " + attachment.AttachmentName + " " + UploadHelper.RoundBytes(attachment.AttachmentSize, 2)); return null; } } Flash.Error(Resources.Attachments.AttachmentSizeError); return new HttpStatusCodeResult(400); }