Beispiel #1
0
        public IActionResult NewPresentationStep3(NewPresentationStep3ViewModel model)
        {
            AppUser      user         = userManager.Users.FirstOrDefault(u => u.Id == model.UserId);
            Presentation presentation = repository.Presentations
                                        .FirstOrDefault(p => p.PresentationId == model.PresentationId);
            int filePart = repository.PresentationFiles
                           .Where(f => f.PresentationId == model.PresentationId).Count() + 1;

            //Create user directory
            string dirPath = Path.Combine(hostingEnvironment.WebRootPath, $@"UsersData\{user.Id}\Presentations\");

            System.IO.Directory.CreateDirectory(dirPath);

            string fileName = (model.FileTitle == null || model.FileTitle.Trim() == "") ?
                              presentation.Title + "_File_Part_" + filePart : model.FileTitle;

            //save presentation file
            PresentationFiles file = new PresentationFiles
            {
                Title          = fileName,
                CreatedById    = user.Id,
                Part           = model.Part,
                PresentationId = presentation.PresentationId,
                CreatedAt      = DateTime.Now,
                UpdatedAt      = DateTime.Now
            };

            repository.SaveFile(file);
            file.FilePath = $@"/UsersData/{user.Id}/Presentations/{file.FileId}.pdf";
            repository.SaveFile(file);

            string filePath = Path.Combine(dirPath, file.FileId.ToString() + ".pdf");

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                model.File.CopyTo(stream);
            }

            ViewData["Id"] = user.Id;
            filePart       = repository.PresentationFiles
                             .Where(f => f.PresentationId == model.PresentationId).Count() + 1;

            return(View(new NewPresentationStep3ViewModel
            {
                UserId = user.Id,
                PresentationId = presentation.PresentationId,
                FileTitle = presentation.Title + "_File_Part_" + filePart,
                CourseId = model.CourseId,
                Part = filePart,
                PresentationFiles = repository.PresentationFiles
                                    .Where(f => f.PresentationId == presentation.PresentationId)
            }));
        }