Ejemplo n.º 1
0
        public async Task <IActionResult> NewCourse(string id, NewEditCourse model)
        {
            ViewData["Id"]    = id;
            ViewData["Title"] = "Create Course";
            AppUser user = await userManager.FindByIdAsync(id);

            if (user != null)
            {
                if (ModelState.IsValid)
                {
                    model.Course.Status       = "Private";
                    model.Course.CreatedBy    = user;
                    model.Course.DateAdded    = DateTime.Now;
                    model.Course.DateModified = DateTime.Now;
                    model.Course.Status       = "Private";
                    if (model.NoEndDate)
                    {
                        model.Course.EndDate = DateTime.MinValue;
                    }
                    repository.SaveCourse(model.Course);

                    return(RedirectToAction("NewCourseStep2", new
                    {
                        id,
                        model.Course.CourseId
                    }));
                }
            }
            return(View());
        }
        public async Task <IActionResult> SaveVideo(VideoModel model)
        {
            AppUser user = await userManager.FindByIdAsync(model.UserId);


            if (user != null)
            {
                ViewData["Id"] = user.Id;
                int videoId = 0;

                string base64 = model.videoUrl.Substring(model.videoUrl.IndexOf(',') + 1);
                byte[] data   = Convert.FromBase64String(base64);

                //Create video directory
                var dirPath  = "";
                var fileName = "";
                var filePath = "";
                switch (model.For)
                {
                case "Course":
                    Course course = repository.Courses
                                    .FirstOrDefault(c => c.CourseId == model.ForId);

                    dirPath = Path.Combine(hostingEnvironment.WebRootPath,
                                           $@"UsersData\{model.UserId}\Courses\{course.CourseId}");
                    System.IO.Directory.CreateDirectory(dirPath);
                    fileName = $@"{course.Title}.mp4";
                    filePath = Path.Combine(dirPath, fileName);

                    //save course video
                    Video video = new Video
                    {
                        Title     = $"{course.Title}Video",
                        CreatedBy = model.UserId,
                        Status    = "Public",
                        For       = "Course",
                        ForId     = course.CourseId,
                        DateAdded = DateTime.Now,
                        VideoPath = $@"/UsersData/{model.UserId}/Courses/{course.CourseId}/{course.Title}.mp4"
                    };
                    repository.SaveVideo(video);
                    course.VideoId = repository.Videos
                                     .FirstOrDefault(v => v.ForId == course.CourseId).Id;
                    repository.SaveCourse(course);

                    videoId = course.VideoId;

                    break;

                case "Presentation":
                    Presentation presentation = repository.Presentations
                                                .FirstOrDefault(p => p.PresentationId == model.ForId);

                    dirPath = Path.Combine(hostingEnvironment.WebRootPath,
                                           $@"UsersData\{user.Id}\Presentations\");
                    System.IO.Directory.CreateDirectory(dirPath);
                    fileName = $@"{presentation.PresentationId}.mp4";
                    filePath = Path.Combine(dirPath, fileName);

                    //save presentation video
                    Video video2 = new Video
                    {
                        Title     = $"{presentation.Title}Video",
                        CreatedBy = model.UserId,
                        Status    = "Public",
                        For       = "Presentation",
                        ForId     = presentation.PresentationId,
                        DateAdded = DateTime.Now,
                        VideoPath = $@"/UsersData/{user.Id}/Presentations/{presentation.PresentationId}.mp4"
                    };
                    repository.SaveVideo(video2);
                    presentation.VideoId = repository.Videos
                                           .FirstOrDefault(v => v.ForId == presentation.PresentationId).Id;
                    repository.SavePresentation(presentation);
                    videoId = presentation.VideoId;

                    break;

                case "Representation":
                    Representation representation = repository.Representations
                                                    .FirstOrDefault(r => r.RepresentationId == model.ForId);
                    dirPath = Path.Combine(hostingEnvironment.WebRootPath,
                                           $@"UsersData\{user.Id}\Representations");
                    System.IO.Directory.CreateDirectory(dirPath);
                    fileName = $@"{representation.RepresentationId}.mp4";
                    filePath = Path.Combine(dirPath, fileName);

                    //save representation vide path
                    Video video3 = new Video
                    {
                        Title     = $"{representation.Title}Video",
                        CreatedBy = model.UserId,
                        Status    = "Public",
                        For       = "Representation",
                        ForId     = representation.RepresentationId,
                        DateAdded = DateTime.Now,
                        VideoPath = $@"/UsersData/{user.Id}/Representations/{representation.RepresentationId}.mp4"
                    };
                    repository.SaveVideo(video3);
                    representation.VideoId = repository.Videos
                                             .FirstOrDefault(v => v.Id == representation.RepresentationId).Id;
                    repository.SaveRepresentation(representation);
                    videoId = representation.VideoId;

                    break;
                }

                using (var videoFile = new FileStream(filePath, FileMode.Create))
                {
                    videoFile.Write(data, 0, data.Length);
                    videoFile.Flush();
                }

                return(RedirectToAction("Index", new { id = user.Id, videoId }));
            }
            else
            {
                ModelState.AddModelError("", "User Not Found");
            }

            return(Redirect(model.returnUrl));
        }