public async Task <IActionResult> AddVideoForUser(int businessId, [FromForm] VideoForCreationDto videoForCreationDto) { var businessFromRepo = await _repo.GetBusiness(businessId); if (businessFromRepo.UserId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var file = videoForCreationDto.File; var uploadResult = new VideoUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new VideoUploadParams { File = new FileDescription(file.Name, stream) }; uploadResult = _cloudinary.Upload(uploadParams); } } if (businessFromRepo.Video != null && businessFromRepo.Video.PublicId != null) { var deleteParams = new DeletionParams(businessFromRepo.Video.PublicId) { ResourceType = ResourceType.Video }; var result = _cloudinary.Destroy(deleteParams); if (result.Result == "ok") { _repo.Delete(businessFromRepo.Video); } } videoForCreationDto.Url = uploadResult.Uri.ToString(); videoForCreationDto.PublicId = uploadResult.PublicId; var video = _mapper.Map <VideoModel>(videoForCreationDto); businessFromRepo.Video = video; if (await _repo.SaveAll()) { var videoToReturn = _mapper.Map <VideoForReturnDto>(video); return(CreatedAtRoute("GetVideo", new { id = video.Id }, videoToReturn)); } return(BadRequest("Cloud not add the video")); }
public async Task <IActionResult> AddVideo([FromForm] VideoForCreationDto videoForCreationDto) { if (!await _repo.IsExistTopic(videoForCreationDto.TopicId)) { return(BadRequest("Topic doesn't Exist")); } var topic = await _repo.GetTopicsById(videoForCreationDto.TopicId); var file = videoForCreationDto.File; var uploadResult = new VideoUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new VideoUploadParams() { File = new FileDescription(file.FileName, stream), /* Transformation = new Transformation() * .Width(500).Height(500).Crop("fill").Gravity("face"), */ /* EagerTransforms = new List<Transformation>() * { * new EagerTransformation().Width(300).Height(300).Crop("pad").AudioCodec("none"), * new EagerTransformation().Width(160).Height(100).Crop("crop").Gravity("south").AudioCodec("none")}, * EagerAsync = true */ }; uploadResult = _cloudinary.UploadLarge(uploadParams); } } var videoToCreate = _mapper.Map <Videos>(videoForCreationDto); videoToCreate.Url = uploadResult.Uri.ToString(); videoToCreate.PublicId = uploadResult.PublicId; videoToCreate.ChapterName = topic.ChapterName; videoToCreate.ClassName = topic.ClassName; videoToCreate.CreatedDate = DateTime.Now; videoToCreate.SubjectName = topic.SubjctName; videoToCreate.TopicName = topic.Name; videoToCreate.ClassId = topic.ClassId; videoToCreate.SubjectForClassId = topic.SubjectIdInClass; videoToCreate.ChapterId = topic.ChapterId; _repo.Add <Videos>(videoToCreate); if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to add Video")); }
public async Task <IActionResult> Addvideo(int userId, int id, [FromForm] VideoForCreationDto videoForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } videoForCreationDto.UserId = userId; // var userFromRepo = await _repository.GetUser(userId, true); var file = videoForCreationDto.File; var uploadResult = new VideoUploadResult(); if (file != null && file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new VideoUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation() .Width(900).Height(800).Crop("crop") }; uploadResult = _cloudinary.Upload(uploadParams); } } videoForCreationDto.Url = uploadResult.Uri.ToString(); videoForCreationDto.publicId = uploadResult.PublicId; videoForCreationDto.PlaylistId = id; var Video = _mapper.Map <video>(videoForCreationDto); _repository.Add(Video); // userFromRepo.videos.Add(Video); if (await _repository.SaveAll()) { var VideosToReturn = _mapper.Map <VideoForReturnDto>(Video); return(CreatedAtRoute("GetVideo", new { id = Video.Id }, VideosToReturn)); } return(BadRequest("خطأ في إضافة الفيديو")); }
public async Task <IActionResult> AddVideoForUser(int userId, [FromForm] VideoForCreationDto videoForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); var file = videoForCreationDto.File; var uploadResults = new VideoUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()){ var uploadParams = new VideoUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation().Width(500).Height(500).Crop("fill") }; uploadResults = _cloudinary.Upload(uploadParams); } } videoForCreationDto.VideoUrl = uploadResults.Uri.ToString(); videoForCreationDto.PublicId = uploadResults.PublicId; var video = _mapper.Map <Models.Video>(videoForCreationDto); userFromRepo.Videos.Add(video); if (await _repo.SaveAll()) { var videoToReturn = _mapper.Map <VideoForReturnDto>(video); return(CreatedAtRoute("GetVideo", new VideoForReturnDto { Id = video.Id }, videoToReturn)); } return(BadRequest("Could not add the video!")); }