Beispiel #1
0
        public ActionResult NewCourseVideo(NewCourseStep2ViewModel model)
        {
            Course course = repository.Courses
                            .FirstOrDefault(c => c.CourseId == model.CourseId);
            Video video = repository.Videos.FirstOrDefault(v => v.For == "Course" && v.ForId == course.CourseId);

            string base64 = model.videoUrl.Substring(model.videoUrl.IndexOf(',') + 1);

            byte[] data = Convert.FromBase64String(base64);

            string videoTitle = model.TitlePlaceholder;

            if (model.VideoTitle != null)
            {
                videoTitle = (model.VideoTitle.Trim() == "") ?
                             model.TitlePlaceholder.Replace(" ", "_") :
                             model.VideoTitle;
            }

            var dirPath = Path.Combine(hostingEnvironment.WebRootPath,
                                       $@"UsersData\{model.UserId}\Courses\{course.CourseId}");

            System.IO.Directory.CreateDirectory(dirPath);
            var fileName = $@"{course.CourseId}.mp4";
            var filePath = Path.Combine(dirPath, fileName);

            //save course video
            if (video != null)
            {
                video.Title = videoTitle;
                video.Views = 0;
                repository.SaveVideo(video);
                repository.SaveCourse(course);

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

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

            TempData["messageCM"] = "'" + course.Title + " 'has been saved!";

            return(RedirectToAction("EditCourse", new { id = model.UserId, courseId = model.CourseId }));
        }