Example #1
0
        public async Task <Video> Create(VideoRequest videoRequest, List <IFormFile> listPost)
        {
            var video = new Video();

            if (videoRequest != null)
            {
                video.Name        = videoRequest.Name;
                video.Description = videoRequest.Description;
                video.AppUserId   = videoRequest.AppUserId;
                video.CreateDate  = new GetDateNow().DateNow;
                video.HidenVideo  = videoRequest.HidenVideo;
                video.CategorysId = videoRequest.CategorysId;
                _context.Video.Add(video);
                _context.SaveChanges();
                var findVideo = _context.Video.OrderByDescending(x => x.Id).FirstOrDefault(x => x.Name.Contains(videoRequest.Name));
                if (listPost.Count > 0)
                {
                    var paths = "";
                    foreach (var item in listPost)
                    {
                        var filename = item.FileName.Split('.');
                        var name     = filename[filename.Length - 1].ToLower();
                        if (name.Contains("mp4"))
                        {
                            name  = findVideo.Id.ToString() + "." + filename[filename.Length - 1].ToLower();
                            paths = "wwwroot/Client/video";
                            findVideo.LinkVideo = name;
                        }
                        else
                        {
                            name = findVideo.Id.ToString() + "." + filename[filename.Length - 1].ToLower();
                            findVideo.PosterImg = name;
                            paths = "wwwroot/Client/imgPoster";
                        }
                        using (var fileStream = new FileStream(Path.Combine(paths, name),
                                                               FileMode.Create, FileAccess.Write))
                        {
                            item.CopyTo(fileStream);
                        }
                    }
                    _context.Update(findVideo);
                    await _context.SaveChangesAsync();

                    return(findVideo);
                }
            }
            return(null);
        }