Ejemplo n.º 1
0
        private async Task <string> CreateAttachmentAsync(RequestCreationBindingModel model, Request request)
        {
            string failedAttachments = string.Empty;

            foreach (var attachment in model.Attachments)
            {
                var extension           = attachment.FileName.Split('.').Last();
                var isAllowedFileFormat = FileFormatValidator.IsValidFormat(extension);

                if (!isAllowedFileFormat)
                {
                    failedAttachments += Environment.NewLine;
                    failedAttachments += $"{attachment.FileName} failed to upload because the file format is forbidden.";
                    continue;
                }
                string currentDirectory = Directory.GetCurrentDirectory();
                string destination      = currentDirectory + "/Files" + "/Requests/" + model.Subject;
                Directory.CreateDirectory(destination);
                string path       = Path.Combine(destination, attachment.FileName);
                var    fileStream = new FileStream(path, FileMode.Create);
                using (fileStream)
                {
                    await attachment.CopyToAsync(fileStream);
                }
                this.dbContext.RequestAttachments.Add(new RequestAttachment
                {
                    FileName   = attachment.FileName,
                    PathToFile = path,
                    RequestId  = request.Id
                });
            }
            return(failedAttachments);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var solution = mapper.Map <Solution>(Model);

            solution.AuthorId = this.userManager.GetUserId(User);

            this.dbContext.Solutions.Add(solution);

            if (Model.Attachment != null)
            {
                var extension = Model.Attachment.FileName.Split('.').Last();

                var isAllowedFileFormat = FileFormatValidator.IsValidFormat(extension);

                if (!isAllowedFileFormat)
                {
                    this.TempData.Put("__Message", new MessageModel()
                    {
                        Type    = MessageType.Danger,
                        Message = "Forbidden file type!"
                    });
                    return(this.Page());
                }

                await CreateAttachmentAsync(Model, solution);
            }

            this.dbContext.SaveChanges();

            this.TempData.Put("__Message", new MessageModel()
            {
                Type    = MessageType.Success,
                Message = "Solution created successfully"
            });


            return(RedirectToPage("/Index"));
        }