public async Task <IActionResult> Upload([FromForm] VideoUploadViewModel viewModel) { try { if (viewModel.VideoFile != null) { var uploadFolder = _configuration["UploadFolder:Video"]; var uploadPath = Path.Combine(environment.WebRootPath, uploadFolder); if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } VideoUploadDto dto = MapToDto(viewModel); await _videoService.UploadVideo(dto, uploadPath); } } catch (Exception Ex) { return(Json(new ResultViewModel { IsSucces = false, Message = Ex.Message })); } return(Json(new ResultViewModel { IsSucces = true, Message = "File uploaded" })); }
public ActionResult AddNewVideo( [FromRoute] int courseId, [FromRoute] int chapterId, [FromRoute] int topicId, [FromForm] VideoUploadDto videoDto ) { var course = _context.Courses.Where(x => x.CourseId == courseId).FirstOrDefault(); if (course == null) { return(BadRequest(new { Message = "Course not found" })); } var chapter = _context.Chapters.Where(x => x.ChapterId == chapterId && x.CourseId == courseId).FirstOrDefault(); if (chapter == null) { return(BadRequest(new { Message = "Chapter not found" })); } var topic = _context.Topics.Where(x => x.ChapterId == chapterId && x.CourseId == courseId && x.TopicId == topicId).FirstOrDefault(); if (topic == null) { return(BadRequest(new { Message = "Topic not found" })); } string videoName = Guid.NewGuid().ToString() + Path.GetExtension(videoDto.Video.FileName); string savePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot" + Path.DirectorySeparatorChar + "Uploads" + Path.DirectorySeparatorChar + "Videos", videoName); using (var stream = new FileStream(savePath, FileMode.CreateNew)) { videoDto.Video.CopyTo(stream); } var video = new Video { VideoName = videoName, CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow }; topic.Video = video; _context.Topics.Update(topic); _context.SaveChanges(); // course = new Course(); // course.CourseTitle = courseDto.CourseTitle; // course.Image = imageName; // course.CreatedAt = DateTime.UtcNow; // course.UpdatedAt = DateTime.UtcNow; // course.UserId = _authenticatedUser.UserId; // _context.Courses.Add(course); // _context.SaveChanges(); return(Ok(new { Message = "New course added" })); }
//TODO: Refactor ! public async Task UploadVideo(VideoUploadDto videoDto, string path) { string fullPath = path + '\\' + videoDto.VideoName + ".mp4"; Video newVideo = CreateNewVideo(videoDto, fullPath); await _videoRepository.Add(newVideo); var result = _videoRepository.Save(); using (var stream = new FileStream(fullPath, FileMode.Create)) { await videoDto.VideoFile.CopyToAsync(stream); } }
//Move to mapper class ! private VideoUploadDto MapToDto(VideoUploadViewModel viewModel) { VideoUploadDto dto = new VideoUploadDto { VideoFile = viewModel.VideoFile, VideoName = viewModel.VideoName }; foreach (var gender in viewModel.VideoGenders) { dto.VideoGenders.Add(new VideoGendersDto() { GenderName = gender.GenderName, Id = gender.Id }); } return(dto); }
private static Video CreateNewVideo(VideoUploadDto videoDto, string fullPath) { Video newVideo = new Video { Id = Guid.NewGuid(), Version = 0, VideoName = videoDto.VideoName, VideoUrl = fullPath }; foreach (var gender in videoDto.VideoGenders) { newVideo.Genders.Add( new Domain.Videos.VideoGender { Id = Guid.NewGuid(), VideoId = newVideo.Id, GenderId = gender.Id, CreateDate = DateTime.Now, Version = 0, CreateBy = "Admin" }); } return(newVideo); }