public async Task CopyVideoToDataBlock(DataBlock dataBlock, Video video, CancellationToken cancellationToken) { Video entity = new Video { Title = video.Title, Description = video.Description, PreviewImageData = video.PreviewImageData, PreviewImageType = video.PreviewImageType, FilePath = video.FilePath, FileType = video.FileType, Privacy = new PrivacyEntity() { PrivacyLevel = video.Privacy.PrivacyLevel, BeginDate = video.Privacy.BeginDate, EndDate = video.Privacy.EndDate, IsAlways = video.Privacy.IsAlways } }; DataBlockVideo dataBlockVideo = new DataBlockVideo { DataBlock = dataBlock, Video = entity }; _context.DataBlockVideos.Add(dataBlockVideo); _context.Videos.Add(entity); await _context.SaveChangesAsync(cancellationToken); }
public async Task <int> Handle(CreateVideoCommand request, CancellationToken cancellationToken) { DataBlock dataBlock = await _context.DataBlocks .Include(db => db.DataCategory) .ThenInclude(dc => dc.Person) .SingleOrDefaultAsync(db => db.CreatedBy.Equals(request.UserId) && db.Id == request.DataBlockId, cancellationToken); if (dataBlock == null) { throw new NotFoundException(nameof(DataBlock), request.DataBlockId); } int treeId = dataBlock.DataCategory.Person.FamilyTreeId; int personId = dataBlock.DataCategory.PersonId; int dataCategoryId = dataBlock.DataCategoryId; string rootPath = Path.Combine(_configuration["FilesStorageFolderPath"], _configuration["UploadsFolderPath"]); string subDirectoryPath = $"{treeId}_tree\\{personId}_person\\" + $"{dataCategoryId}_datacategory\\{dataBlock.Id}_datablock\\Videos"; string directoryPath = Path.Combine(rootPath, subDirectoryPath); string fileType = request.VideoFile.ContentType.Split('/')[1]; string fileName = $"{Guid.NewGuid()}.{fileType}"; string filePath = Path.Combine(directoryPath, fileName); Directory.CreateDirectory(directoryPath); using (var stream = File.OpenWrite(filePath)) { await request.VideoFile.CopyToAsync(stream); } Video entity = new Video() { Title = request.Title, Description = request.Description, FilePath = filePath, FileType = fileType, PreviewImageData = _thumbnailService.GetVideoThumbnailBytes(filePath), PreviewImageType = "jpeg", Privacy = new PrivacyEntity() { PrivacyLevel = PrivacyLevel.Confidential, IsAlways = true } }; DataBlockVideo dataBlockVideo = new DataBlockVideo() { DataBlock = dataBlock, Video = entity }; _context.Videos.Add(entity); _context.DataBlockVideos.Add(dataBlockVideo); await _context.SaveChangesAsync(cancellationToken); return(entity.Id); }