示例#1
0
        public async Task <IActionResult> EditInstruction(BuildingInstructionEditViewModel editInstructionModel)
        {
            if (ModelState.IsValid)
            {
                if (editInstructionModel.InstructionPdf != null)
                {
                    var extension = ".pdf";
                    if (!extension.Contains(Path.GetExtension(editInstructionModel.InstructionPdf.FileName.ToLower())))
                    {
                        ModelState.AddModelError(string.Empty, "The building instruction format must be .pdf");
                        return(View());
                    }

                    string uploadsFolder     = Path.Combine(_webHostingEnvironment.WebRootPath, "buildinginstructions");
                    var    uniquePdfFileName = Guid.NewGuid().ToString() + " " + editInstructionModel.InstructionPdf.FileName;
                    string filePath          = Path.Combine(uploadsFolder, uniquePdfFileName);



                    editInstructionModel.InstructionPdfUrl = @$ "/buildingInstructions/{uniquePdfFileName}";

                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await editInstructionModel.InstructionPdf.CopyToAsync(stream);
                    }
                }

                if (editInstructionModel.InstructionImage != null)
                {
                    var extensions = new string[] { ".png", ".jpg" };

                    if (!extensions.Contains(Path.GetExtension(editInstructionModel.InstructionImage.FileName.ToLower())))
                    {
                        ModelState.AddModelError(string.Empty, "The picture format must be .png or .jpg");
                        return(View());
                    }

                    string uploadsFolder       = Path.Combine(_webHostingEnvironment.WebRootPath, "images/Instructions");
                    var    uniqueImageFileName = Guid.NewGuid().ToString() + " " + editInstructionModel.InstructionImage.FileName;
                    string filePath            = Path.Combine(uploadsFolder, uniqueImageFileName);


                    editInstructionModel.InstructionImageUrl = @$ "/Images/Instructions/{uniqueImageFileName}";

                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await editInstructionModel.InstructionImage.CopyToAsync(stream);
                    }
                }


                if (editInstructionModel.InstructionVideo != null)
                {
                    var extension = ".mp4";
                    if (!Path.GetExtension(editInstructionModel.InstructionVideo.FileName.ToLower()).Equals(extension))
                    {
                        ModelState.AddModelError(string.Empty, "The video format must be .mp4");
                        return(View());
                    }

                    string uploadsFolder       = Path.Combine(_webHostingEnvironment.WebRootPath, "video");
                    var    uniqueVideoFileName = Guid.NewGuid().ToString() + " " + editInstructionModel.InstructionVideo.FileName;
                    string filePath            = Path.Combine(uploadsFolder, uniqueVideoFileName);


                    editInstructionModel.InstructionVideoURL = @$ "/Video/{uniqueVideoFileName}";


                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await editInstructionModel.InstructionVideo.CopyToAsync(stream);
                    }
                }


                if (editInstructionModel.Program != null)
                {
                    if (!Path.GetExtension(editInstructionModel.Program.FileName.ToLower()).Equals(".ev3"))
                    {
                        ModelState.AddModelError(string.Empty, "The program format must be .ev3");
                        return(View());
                    }

                    string uploadsFolder         = Path.Combine(_webHostingEnvironment.WebRootPath, "programs");
                    var    uniqueProgramFileName = Guid.NewGuid().ToString() + " " + editInstructionModel.Program.FileName;
                    string filePath = Path.Combine(uploadsFolder, uniqueProgramFileName);


                    editInstructionModel.ProgramUrl = @$ "/Programs/{uniqueProgramFileName}";

                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await editInstructionModel.Program.CopyToAsync(stream);
                    }
                }


                var editBuildingInstruction = new BuildingInstruction()
                {
                    BuildingInstructionId = editInstructionModel.BuildingInstructionId,
                    CategoryId            = editInstructionModel.CategoryId,
                    CreatedAt             = DateTime.Now,
                    ImageUrl          = editInstructionModel.InstructionImageUrl,
                    PdfInstructionUrl = editInstructionModel.InstructionPdfUrl,
                    Pages             = editInstructionModel.Pages,
                    Name            = editInstructionModel.Name,
                    Set             = editInstructionModel.Set,
                    VideoUrl        = editInstructionModel.InstructionVideoURL,
                    ProgramUrl      = editInstructionModel.ProgramUrl,
                    LongDescription = editInstructionModel.LongDescription,
                };

                var updateInstruction = await _buildingInstructionRepository.UpdateBuildingInstruction(editBuildingInstruction);



                return(RedirectToAction("Details", new { id = updateInstruction.BuildingInstructionId }));
            }
            else
            {
                ModelState.AddModelError(string.Empty, "The building instruction cannot be added");
            }

            return(View(editInstructionModel));
        }
示例#2
0
        public async Task <IActionResult> CreateInstruction(BuildingInstructionEditViewModel instructionModel)
        {
            if (ModelState.IsValid)
            {
                if (instructionModel.InstructionPdf != null)
                {
                    var maxPdfSizeLimit = 30_000_000;
                    if (instructionModel.InstructionPdf.Length > maxPdfSizeLimit)
                    {
                        ModelState.AddModelError(string.Empty, "File instruction .pdf too large, max 30MB");
                        return(View());
                    }

                    var extension = ".pdf";
                    if (!extension.Contains(Path.GetExtension(instructionModel.InstructionPdf.FileName.ToLower())))
                    {
                        ModelState.AddModelError(string.Empty, "The building instruction format must be .pdf");
                        return(View());
                    }

                    string uploadsFolder     = Path.Combine(_webHostingEnvironment.WebRootPath, "buildinginstructions");
                    var    uniquePdfFileName = Guid.NewGuid().ToString() + " " + instructionModel.InstructionPdf.FileName;
                    string filePath          = Path.Combine(uploadsFolder, uniquePdfFileName);



                    instructionModel.InstructionPdfUrl = @$ "/buildingInstructions/{uniquePdfFileName}";

                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await instructionModel.InstructionPdf.CopyToAsync(stream);
                    }
                }

                if (instructionModel.InstructionImage != null)
                {
                    var maxImageSizeLimt = 2_000_000;
                    if (instructionModel.InstructionImage.Length > maxImageSizeLimt)
                    {
                        ModelState.AddModelError(string.Empty, "Image is too large, max size 2MB");
                    }

                    var extensions = new string[] { ".png", ".jpg" };

                    if (!extensions.Contains(Path.GetExtension(instructionModel.InstructionImage.FileName.ToLower())))
                    {
                        ModelState.AddModelError(string.Empty, "The picture format must be .png or .jpg");
                        return(View());
                    }

                    string uploadsFolder       = Path.Combine(_webHostingEnvironment.WebRootPath, "images/Instructions");
                    var    uniqueImageFileName = Guid.NewGuid().ToString() + " " + instructionModel.InstructionImage.FileName;
                    string filePath            = Path.Combine(uploadsFolder, uniqueImageFileName);


                    instructionModel.InstructionImageUrl = @$ "/Images/Instructions/{uniqueImageFileName}";

                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await instructionModel.InstructionImage.CopyToAsync(stream);
                    }
                }


                if (instructionModel.InstructionVideo != null)
                {
                    var maxVideoSizeLimit = 20_000_000;

                    if (instructionModel.InstructionVideo.Length > maxVideoSizeLimit)
                    {
                        ModelState.AddModelError(string.Empty, "Video too large, max size 20MB");
                    }

                    var extension = ".mp4";
                    if (!Path.GetExtension(instructionModel.InstructionVideo.FileName.ToLower()).Equals(extension))
                    {
                        ModelState.AddModelError(string.Empty, "The video format must be .mp4");
                        return(View());
                    }

                    string uploadsFolder       = Path.Combine(_webHostingEnvironment.WebRootPath, "video");
                    var    uniqueVideoFileName = Guid.NewGuid().ToString() + " " + instructionModel.InstructionVideo.FileName;
                    string filePath            = Path.Combine(uploadsFolder, uniqueVideoFileName);


                    instructionModel.InstructionVideoURL = @$ "/Video/{uniqueVideoFileName}";


                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await instructionModel.InstructionVideo.CopyToAsync(stream);
                    }
                }


                if (instructionModel.Program != null)
                {
                    if (!Path.GetExtension(instructionModel.Program.FileName.ToLower()).Equals(".ev3"))
                    {
                        ModelState.AddModelError(string.Empty, "The program format must be .ev3");
                        return(View());
                    }

                    string uploadsFolder         = Path.Combine(_webHostingEnvironment.WebRootPath, "programs");
                    var    uniqueProgramFileName = Guid.NewGuid().ToString() + " " + instructionModel.Program.FileName;
                    string filePath = Path.Combine(uploadsFolder, uniqueProgramFileName);


                    instructionModel.ProgramUrl = @$ "/Programs/{uniqueProgramFileName}";

                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await instructionModel.Program.CopyToAsync(stream);
                    }
                }


                var newBuildingInstruction = new BuildingInstruction()
                {
                    CategoryId        = instructionModel.CategoryId,
                    CreatedAt         = DateTime.Now,
                    ImageUrl          = instructionModel.InstructionImageUrl,
                    UserId            = _userManager.GetUserId(HttpContext.User),
                    PdfInstructionUrl = instructionModel.InstructionPdfUrl,
                    Pages             = instructionModel.Pages,
                    Name            = instructionModel.Name,
                    Set             = instructionModel.Set,
                    VideoUrl        = instructionModel.InstructionVideoURL,
                    ProgramUrl      = instructionModel.ProgramUrl,
                    LongDescription = instructionModel.LongDescription,
                };

                await _buildingInstructionRepository.AddNewBuildingInstructionAsync(newBuildingInstruction);

                var getNewBuildingInstruction = _buildingInstructionRepository.AllBuildingInstructions.Last();


                return(RedirectToAction("Details", new { id = getNewBuildingInstruction.BuildingInstructionId }));
            }
            else
            {
                ModelState.AddModelError(string.Empty, "The building instruction cannot be added");
            }

            return(View(instructionModel));
        }