public async Task <DomainVideo> GetAsync(string projectId)
        {
            ProjectEntity project = await _projectRepository.GetAsync(projectId);

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

            if (string.IsNullOrEmpty(project.OriginalVideoFileId))
            {
                throw new NotFoundException(string.Format("Project {0} does not have a video", projectId));
            }


            StorageFile file = await _fileSystem.GetFilePropertiesAsync(new StorageFile { Id = project.OriginalVideoFileId });

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

            DomainVideo video = _mapper.Map <StorageFile, DomainVideo>(file);

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

            return(video);
        }
Exemple #2
0
        public async Task AddVideo(string projectId, string userId, DomainVideo entity, ProcessedMediaModel processedMediaModel)
        {
            List <DomainProcessedScreenshot> processedScreenshots = processedMediaModel.DomainProcessedScreenshots;
            List <DomainProcessedVideo>      processedVideos      = processedMediaModel.DomainProcessedVideos;

            // Fill entities data
            var entities = new List <IProcessedEntity>(processedScreenshots);

            entities.AddRange(processedVideos);

            foreach (IProcessedEntity processedEntity in entities)
            {
                processedEntity.UserId       = BackendId;
                processedEntity.SourceFileId = entity.FileId;
            }

            // Add video processing entities
            await Task.WhenAll(
                new Task[]
            {
                _processedScreenshotRepository.AddAsync(processedScreenshots.Select(p => _mapper.Map <DomainProcessedScreenshot, ProcessedScreenshotEntity>(p))),
                _processedVideoRepository.AddAsync(processedVideos.Select(p => _mapper.Map <DomainProcessedVideo, ProcessedVideoEntity>(p))),
                _videoQueueRepository.AddAsync(_mapper.Map <DomainVideoQueue, VideoQueueEntity>(
                                                   new DomainVideoQueue
                {
                    ProjectId = projectId,
                    FileId    = entity.FileId,
                    UserId    = userId
                }))
            });
        }
        public async Task AddVideo(string projectId, string userId, DomainVideo entity, ProcessedMediaModel processedMediaModel)
        {
            List<DomainProcessedScreenshot> processedScreenshots = processedMediaModel.DomainProcessedScreenshots;
            List<DomainProcessedVideo> processedVideos = processedMediaModel.DomainProcessedVideos;

            // Fill entities data
            var entities = new List<IProcessedEntity>(processedScreenshots);
            entities.AddRange(processedVideos);

            foreach (IProcessedEntity processedEntity in entities)
            {
                processedEntity.UserId = BackendId;
                processedEntity.SourceFileId = entity.FileId;
            }

            // Add video processing entities
            await Task.WhenAll(
                new Task[]
                {
                    _processedScreenshotRepository.AddAsync(processedScreenshots.Select(p => _mapper.Map<DomainProcessedScreenshot, ProcessedScreenshotEntity>(p))),
                    _processedVideoRepository.AddAsync(processedVideos.Select(p => _mapper.Map<DomainProcessedVideo, ProcessedVideoEntity>(p))),
                    _videoQueueRepository.AddAsync(_mapper.Map<DomainVideoQueue, VideoQueueEntity>(
                        new DomainVideoQueue
                        {
                            ProjectId = projectId,
                            FileId = entity.FileId,
                            UserId = userId
                        }))
                });
        }
Exemple #4
0
        public async Task <HttpResponseMessage> Post(string id, ProjectVideoModel model)
        {
            // Check whether project with such id exists
            await GetProjectAsync(id);

            // Add video to that project
            var video = new DomainVideo
            {
                FileName    = model.Video.Name,
                FileUri     = model.Video.Uri,
                ContentType = model.Video.ContentType,
                FileLength  = model.Video.Length
            };

            try
            {
                video = await _videoRepository.AddAsync(id, video);
            }
            catch (AggregateException aggregateException)
            {
                // Handle VideoFormatExceptions
                var errorsList = new StringBuilder(String.Format("{0}\n", aggregateException.Message));
                foreach (Exception exception in aggregateException.InnerExceptions)
                {
                    var videoFormatException = exception as VideoFormatException;
                    if (videoFormatException == null)
                    {
                        Trace.TraceError("Failed to add a video file: {0}", exception);
                        return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ResponseMessages.InternalServerError));
                    }

                    string errorMessage = GetExceptionDescription(videoFormatException.ParamType);
                    errorsList.AppendLine(errorMessage);
                    ModelState.AddModelError("Video", errorMessage);
                }

                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            var projectVideo = new ProjectVideo
            {
                Name        = video.FileName,
                Uri         = video.FileUri,
                ContentType = video.ContentType,
                Length      = video.FileLength
            };

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, projectVideo);

            response.SetLastModifiedDate(video.Modified);

            return(response);
        }
        public async Task<HttpResponseMessage> Post(string id, ProjectVideoModel model)
        {
            // Check whether project with such id exists
            await GetProjectAsync(id);

            // Add video to that project
            var video = new DomainVideo
            {
                FileName = model.Video.Name,
                FileUri = model.Video.Uri,
                ContentType = model.Video.ContentType,
                FileLength = model.Video.Length
            };

            try
            {
                video = await _videoRepository.AddAsync(id, video);
            }
            catch (AggregateException aggregateException)
            {
                // Handle VideoFormatExceptions
                var errorsList = new StringBuilder(String.Format("{0}\n", aggregateException.Message));
                foreach (Exception exception in aggregateException.InnerExceptions)
                {
                    var videoFormatException = exception as VideoFormatException;
                    if (videoFormatException == null)
                    {
                        Trace.TraceError("Failed to add a video file: {0}", exception);
                        return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ResponseMessages.InternalServerError);
                    }

                    string errorMessage = GetExceptionDescription(videoFormatException.ParamType);
                    errorsList.AppendLine(errorMessage);
                    ModelState.AddModelError("Video", errorMessage);
                }

                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            var projectVideo = new ProjectVideo
            {
                Name = video.FileName,
                Uri = video.FileUri,
                ContentType = video.ContentType,
                Length = video.FileLength
            };

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, projectVideo);
            response.SetLastModifiedDate(video.Modified);

            return response;
        }
        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);
        }
        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;
        }
        // GET api/projects/{id}/video
        public async Task <HttpResponseMessage> Get(string id)
        {
            // Get project
            await GetProjectAsync(id);

            DomainVideo video = await _videoService.GetAsync(id);

            var projectVideo = new ProjectVideo
            {
                Name        = video.FileName,
                Uri         = video.FileUri,
                ContentType = video.ContentType,
                Length      = video.FileLength
            };

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, projectVideo);

            response.SetLastModifiedDate(video.Modified);

            return(response);
        }