Exemple #1
0
        public async Task <DomainUserFile> AddAsync(DomainUserFile entity)
        {
            StorageFile storageFile;

            using (FileStream stream = File.OpenRead(entity.FileUri))
            {
                storageFile = await _fileSystem.UploadFileFromStreamAsync(
                    new StorageFile(stream, entity.ContentType)
                {
                    UserId      = entity.UserId,
                    ContentType = entity.ContentType,
                    Length      = entity.FileLength,
                    FileName    = entity.FileName
                });
            }

            return(new DomainUserFile
            {
                ContentType = storageFile.ContentType,
                Created = storageFile.Created,
                FileId = storageFile.Id,
                FileLength = storageFile.Length,
                FileUri = _uriProvider.CreateUri(storageFile.Id),
                Modified = storageFile.Modified,
                UserId = storageFile.UserId,
                FileName = storageFile.FileName
            });
        }
        public async Task <DomainVideo> AddAsync(string projectId, DomainVideo entity)
        {
            ProjectEntity project = await _projectRepository.GetAsync(projectId);

            if (project == null)
            {
                throw new NotFoundException(string.Format("Project {0} was not found", projectId));
            }

            // Check whether project already contains video
            if (project.ProjectType == (int)VideoType.Internal && !string.IsNullOrEmpty(project.OriginalVideoFileId) ||
                project.ProjectType == (int)VideoType.External && !string.IsNullOrEmpty(project.VideoSource))
            {
                throw new ConflictException();
            }

            StorageFile storageFile;

            // adding file to the file system
            using (FileStream stream = File.OpenRead(entity.FileUri))
            {
                storageFile = await _fileSystem.UploadFileFromStreamAsync(
                    new StorageFile(stream, entity.ContentType)
                {
                    UserId   = project.UserId,
                    FileName = entity.FileName,
                    Length   = entity.FileLength
                });
            }

            // updating project
            await _projectRepository.SetVideoAsync(project.Id, (int)VideoType.Internal, storageFile.Id);

            // building result
            DomainVideo result = _mapper.Map <StorageFile, DomainVideo>(storageFile);

            result.FileUri = _uriProvider.CreateUri(result.FileId);

            // adding task to the processing queue
            ProcessedMediaModel processedModel = await _processedEntityManager.GetProcessedMediaModel(entity.FileUri, projectId);

            await _processedVideoHandler.AddVideo(project.Id, project.UserId, result, processedModel);

            return(result);
        }
 private DomainProjectProcessedScreenshot BuildDomainObject(ProjectEntity project, ProjectEntity.EncodedScreenshot entity)
 {
     return(new DomainProjectProcessedScreenshot
     {
         FileId = entity.FileId,
         ContentType = entity.ContentType,
         Width = entity.Width,
         Height = entity.Height,
         FileUri = _uriProvider.CreateUri(entity.FileId),
         ProjectId = project.Id,
         UserId = project.UserId
     });
 }
        public async Task <DomainScreenshot> AddAsync(string projectId, DomainScreenshot entity)
        {
            ProjectEntity project = await _projectRepository.GetAsync(projectId);

            if (project == null)
            {
                throw new NotFoundException(string.Format("Project {0} was not found", projectId));
            }

            // Check whether project already contains screenshot
            if (!string.IsNullOrEmpty(project.ScreenshotFileId))
            {
                throw new ConflictException();
            }

            StorageFile storageFile;

            // uploading file
            using (FileStream stream = File.OpenRead(entity.FileUri))
            {
                storageFile = await _fileSystem.UploadFileFromStreamAsync(
                    new StorageFile(stream, entity.ContentType)
                {
                    UserId   = project.UserId,
                    FileName = entity.FileName,
                    Length   = entity.FileLength
                });
            }

            // updating project
            await _projectRepository.SetScreenshotFileIdAsync(project.Id, storageFile.Id);

            // building result
            DomainScreenshot result = _mapper.Map <StorageFile, DomainScreenshot>(storageFile);

            result.FileUri = _uriProvider.CreateUri(storageFile.Id);

            return(result);
        }
Exemple #5
0
        private async Task <ExampleProject> GetProjectAsync(DomainProject project)
        {
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }

            // Avsx
            Task <StorageFile> avsxTask = Task.FromResult <StorageFile>(null);

            if (!string.IsNullOrEmpty(project.AvsxFileId))
            {
                avsxTask = _fileSystem.GetFilePropertiesSlimAsync(new StorageFile {
                    Id = project.AvsxFileId
                });
            }

            // Screenshot
            Task <StorageFile> screenshotTask = Task.FromResult <StorageFile>(null);

            if (!string.IsNullOrEmpty(project.ScreenshotFileId))
            {
                screenshotTask = _fileSystem.GetFilePropertiesSlimAsync(new StorageFile {
                    Id = project.ScreenshotFileId
                });
            }

            // Original video
            Task <StorageFile> videoTask = Task.FromResult <StorageFile>(null);

            if (!string.IsNullOrEmpty(project.OriginalVideoFileId))
            {
                videoTask = _fileSystem.GetFilePropertiesSlimAsync(new StorageFile {
                    Id = project.OriginalVideoFileId
                });
            }

            await Task.WhenAll(new[]
            {
                avsxTask,
                screenshotTask,
                videoTask
            });


            var result = new ExampleProject
            {
                Id             = project.Id,
                Name           = project.Name,
                Description    = project.Description,
                Created        = project.Created,
                Views          = project.HitsCount,
                Likes          = project.LikesCount,
                Dislikes       = project.DislikesCount,
                ProjectType    = (int)project.ProjectType,
                ProjectSubtype = (int)project.ProjectSubtype
            };


            if (avsxTask.Result != null)
            {
                result.AvsxUri    = _uriProvider.CreateUri(avsxTask.Result.Id);
                result.TotalSize += avsxTask.Result.Length;
            }

            if (screenshotTask.Result != null)
            {
                result.ScreenshotUri = _uriProvider.CreateUri(screenshotTask.Result.Id);
                result.TotalSize    += screenshotTask.Result.Length;
            }

            if (videoTask.Result != null)
            {
                result.VideoUri   = _uriProvider.CreateUri(videoTask.Result.Id);
                result.VideoHash  = videoTask.Result.Id;
                result.TotalSize += videoTask.Result.Length;
            }

            return(result);
        }
        private Watch AggregateProject(ProjectEntity projectEntity, string userId, Dictionary <string, UserEntity> users, Dictionary <string, int> commentsCounts,
                                       Dictionary <string, List <CommentEntity> > comments)
        {
            DomainProject project = _mapper.Map <ProjectEntity, DomainProject>(projectEntity);

            // 1. Aggregate Data

            UserEntity           projectUser     = users.ContainsKey(project.UserId) ? users[project.UserId] : null;
            int                  commentsCount   = commentsCounts.ContainsKey(project.Id) ? commentsCounts[project.Id] : 0;
            List <CommentEntity> projectComments = comments.ContainsKey(project.Id) ? comments[project.Id] : new List <CommentEntity>();


            // Processed Videos
            var watchVideos = new List <WatchVideo>();

            watchVideos.AddRange(
                from @group in project.EncodedVideos.GroupBy(q => q.Width)
                select @group.ToList()
                into processedVideos
                where processedVideos.Count == 2
                from v in processedVideos
                select new WatchVideo
            {
                ContentType = v.ContentType,
                Width       = v.Width,
                Height      = v.Height,
                Uri         = _uriProvider.CreateUri(v.FileId)
            });

            // Processed Screenshots
            List <WatchScreenshot> watchScreenshots = project.EncodedScreenshots.Select(
                s => new WatchScreenshot
            {
                ContentType = s.ContentType,
                Uri         = _uriProvider.CreateUri(s.FileId)
            }).ToList();

            // External Video
            ExternalVideo externalVideo = !string.IsNullOrEmpty(project.VideoSource)
                ? new ExternalVideo
            {
                ProductName  = project.VideoSourceProductName,
                VideoUri     = project.VideoSource,
                AcsNamespace = _settings.AcsNamespace
            }
                : null;


            // 2. Calculate Video State
            WatchState state;

            if (string.IsNullOrEmpty(project.AvsxFileId) || (string.IsNullOrEmpty(project.OriginalVideoFileId) && externalVideo == null))
            {
                state = WatchState.Uploading;
            }
            else if (externalVideo == null && watchVideos.Count == 0)
            {
                state = WatchState.Encoding;
            }
            else
            {
                state = WatchState.Ready;
            }

            // 3. Map comments
            var watchComments = new List <Comment>();

            foreach (CommentEntity projectComment in projectComments)
            {
                UserEntity    commentAuthor = users.ContainsKey(projectComment.UserId) ? users[projectComment.UserId] : null;
                DomainComment domainComment = _mapper.Map <Tuple <CommentEntity, UserEntity>, DomainComment>(new Tuple <CommentEntity, UserEntity>(projectComment, commentAuthor));

                Comment comment = _mapper.Map <DomainComment, Comment>(domainComment);
                if (commentAuthor != null)
                {
                    comment.AvatarUrl = _userAvatarProvider.GetAvatar(commentAuthor.Email);
                }

                watchComments.Add(comment);
            }
            watchComments.Reverse();

            // Result
            return(new Watch
            {
                Name = project.Name,
                UserId = project.UserId,
                UserName = projectUser != null ? projectUser.Name : null,
                UserAvatarUrl = projectUser != null?_userAvatarProvider.GetAvatar(new DomainUser { Email = projectUser.Email }) : null,
                                    Description = project.Description,
                                    Created = project.Created,
                                    Avsx = !string.IsNullOrEmpty(project.AvsxFileId) ? _uriProvider.CreateUri(project.AvsxFileId) : null,
                                    Screenshots = watchScreenshots,
                                    Videos = watchVideos,
                                    PublicUrl = _projectUriProvider.GetUri(project.Id),
                                    Id = project.Id,
                                    Access = project.Access,
                                    IsEditable = project.UserId == userId,
                                    HitsCount = project.HitsCount,
                                    External = externalVideo,
                                    ScreenshotUrl = !string.IsNullOrEmpty(project.ScreenshotFileId) ? _uriProvider.CreateUri(project.ScreenshotFileId) : null,
                                    State = state,
                                    Generator = (int)project.ProductType,
                                    ProjectType = project.ProjectType,
                                    ProjectSubtype = project.ProjectSubtype,
                                    EnableComments = project.EnableComments,
                                    CommentsCount = commentsCount,
                                    LikesCount = project.LikesCount,
                                    DislikesCount = project.DislikesCount,
                                    Comments = watchComments
            });
        }