public async Task VideoViews(int videoId)
        {
            Video video = repository.Videos
                          .FirstOrDefault(v => v.Id == videoId);
            int views = video.Views + 1;

            video.Views = views;
            repository.SaveVideo(video);

            await Clients.All.SendAsync("NewVideoView", videoId, views);
        }
Beispiel #2
0
        public IActionResult NewCourseStep2(NewCourseStep2ViewModel model)
        {
            Course course = repository.Courses.FirstOrDefault(c => c.CourseId == model.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
            Video 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("CourseManage", new { id = model.UserId, courseId = model.CourseId }));
        }
        public ActionResult NewView(int videoId)
        {
            Video video = repository.Videos
                          .FirstOrDefault(v => v.Id == videoId);
            int totalViews = video.Views + 1;

            video.Views = totalViews;
            repository.SaveVideo(video);

            return(PartialView("VideoTotalViews", totalViews));
        }
        public async Task <IActionResult> NewRepresentation(NewRepresentation model)
        {
            AppUser user = await userManager.FindByIdAsync(model.UserId);

            if (user != null)
            {
                if (ModelState.IsValid)
                {
                    //create new representation
                    string representationTitle = model.TitlePlaceholder.Replace("_", " ");
                    if (model.RepresentationTitle != null)
                    {
                        representationTitle = (model.RepresentationTitle.Trim() == "") ?
                                              model.TitlePlaceholder.Replace("_", " ") :
                                              model.RepresentationTitle;
                    }

                    Presentation presentation = repository.Presentations
                                                .FirstOrDefault(p => p.PresentationId == model.PresentationId);
                    Representation representation = new Representation
                    {
                        CreatedBy = user,
                        Title     = representationTitle,
                        DateAdded = DateTime.Now,
                        Status    = "Public"
                    };
                    presentation.Representations.Add(representation);
                    repository.SavePresentation(presentation);

                    //save representation video
                    representation = repository.Presentations
                                     .FirstOrDefault(p => p.PresentationId == model.PresentationId)
                                     .Representations.FirstOrDefault(r => r.CreatedBy == user);

                    int videoId = 0;

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

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

                    var dirPath = Path.Combine(hostingEnvironment.WebRootPath,
                                               $@"UsersData\{model.UserId}\Representations");
                    System.IO.Directory.CreateDirectory(dirPath);
                    var fileName = $@"{videoTitle}.mp4";
                    var filePath = Path.Combine(dirPath, fileName);

                    //save video
                    Video video = new Video
                    {
                        Title     = $"{videoTitle.Replace("_", " ")}",
                        CreatedBy = model.UserId,
                        Status    = "Public",
                        For       = "Representation",
                        ForId     = representation.RepresentationId,
                        DateAdded = DateTime.Now,
                        VideoPath = $@"/UsersData/{model.UserId}/Representations/{videoTitle}.mp4"
                    };
                    repository.SaveVideo(video);

                    videoId = repository.Videos
                              .FirstOrDefault(v => v.For == "Representation" &&
                                              v.ForId == representation.RepresentationId).Id;

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

                    representation.VideoId = videoId;
                    repository.SaveRepresentation(representation);

                    TempData["sccMsgCourse"] = "'" + representation.Title + "' has been saved!";
                }
                else
                {
                    TempData["errMsgCourse"] = "Sorry, something went wrong!";
                }
            }
            return(RedirectToAction("CourseDetails", new
            {
                id = model.UserId,
                courseId = model.CourseId
            }));
        }