예제 #1
0
        public HandlerRoutes(
            IAiringService airingSvc,
            IDestinationService destinationSvc,
            IPathingService pathingSvc,
            IFileService fileSvc,
            IHandlerHistoryService handlerHistorySvc,
            IQueueService queueSvc,
            IReportingService reporterSvc,
            EncodingFileContentValidator encodingFileValidator,
            Serilog.ILogger logger
            )
            : base("v1")
        {
            _airingSvc             = airingSvc;
            _destinationSvc        = destinationSvc;
            _pathingSvc            = pathingSvc;
            _fileSvc               = fileSvc;
            _handlerHistorySvc     = handlerHistorySvc;
            _queueSvc              = queueSvc;
            _encodingFileValidator = encodingFileValidator;
            _reporterSvc           = reporterSvc;
            _logger = logger;


            /// Persist Encoding related data through POST operation.
            /// The correlation between an existing airing and the provided payload
            /// is performed using the provided odt-media-id data point. As a result,
            /// odt-media-id is a required field in the payload.

            Post("/handler/encoding", _ =>
            {
                BLFileModel.File file = default(BLFileModel.File);

                try
                {
                    // Perform preliminary authorization
                    this.RequiresAuthentication();
                    this.RequiresClaims(c => c.Type == HttpMethod.Post.Verb());

                    // Deserealize JSON request to DataContracts.
                    EncodingFileContentViewModel encodingPayLoad = this.Bind <EncodingFileContentViewModel>();

                    // Retrieve encoding destination details
                    DF_ENCOM_DESTINATION_CODE = _destinationSvc.GetByDestinationNames(new List <String> {
                        "ENCOM"
                    }).First().ExternalId;

                    // Persist encoding raw JSON data before proceeding
                    this.Request.Body.Seek(0, SeekOrigin.Begin);
                    _handlerHistorySvc.Save(this.Request.Body.AsString(), encodingPayLoad.MediaId);

                    // Validate provided data contract. If validation errors are found
                    // then inform user, else continue
                    var validationResult = _encodingFileValidator.Validate(encodingPayLoad);
                    if (!validationResult.IsValid)
                    {
                        // Report error status (for encoding destination) to monitoring system
                        ReportStatusToMonitoringSystem(encodingPayLoad.MediaId, "Ingestion of encoding data failed due to validation errors in payload", DF_ERROR_STATUS_CODE, DF_ENCOM_DESTINATION_CODE);

                        // Return status
                        return(Negotiate.WithModel(validationResult.Errors.Select(c => c.ErrorMessage))
                               .WithStatusCode(HttpStatusCode.BadRequest));
                    }


                    ApplyPathTranslationInformation(encodingPayLoad);

                    // Translate DataContracts to Business Models
                    file = encodingPayLoad.ToBusinessModel <EncodingFileContentViewModel, BLFileModel.File>();

                    // Perform CRUD
                    _fileSvc.PersistVideoFile(file);

                    // Send notification to all subscribed consumers
                    SendNotification(file);

                    // Report completed status (for Encoding destination) to monitoring system
                    ReportStatusToMonitoringSystem(file.MediaId, "Successfully ingested encoding data", DF_COMPLETED_STATUS_CODE, DF_ENCOM_DESTINATION_CODE);

                    // Return
                    return(Response.AsJson(new
                    {
                        result = "success"
                    }, HttpStatusCode.OK));
                }
                catch (Exception ex)
                {
                    // Log error
                    logger.Error(ex, ex.Message);
                    throw;
                }
            });
        }
예제 #2
0
        /// <summary>
        /// Logic to translate/create a corresponding URL for the
        /// bucketUrl entry provided from Encoding
        /// </summary>
        /// <param name="file"></param>
        /// <param name="pathTranslationQuery"></param>
        private void ApplyPathTranslationInformation(EncodingFileContentViewModel file)
        {
            // Get an asset (the first one in the list) with the given media id
            var airing   = _airingSvc.GetByMediaId(file.MediaId).FirstOrDefault();
            var pathings = _pathingSvc.GetAll();

            foreach (var media in file.MediaCollection)
            {
                foreach (var playlist in media.Playlists)
                {
                    Dictionary <string, string> sourceUrls = playlist.Urls;

                    //If input URLs payload is empty then get bucket url
                    if (!sourceUrls.Any())
                    {
                        sourceUrls["bucketUrl"] = playlist.BucketURL;
                    }

                    foreach (var url in sourceUrls)
                    {
                        var urlType   = url.Key;
                        var sourceUrl = url.Value;

                        //Adds the given url
                        playlist.TranslatedUrls.Add(new TranslatedUrlViewModel {
                            UrlType = urlType, Url = sourceUrl
                        });

                        if (!string.IsNullOrEmpty(sourceUrl))
                        {
                            foreach (var pt in pathings.Where(x => sourceUrl.StartsWith(x.Source.BaseUrl)))
                            {
                                var translatedUrl = GetTranslatedUrl(sourceUrl, pt, airing);

                                if (!translatedUrl.IsNullOrEmpty())
                                {
                                    var tranlatedUrlType = pt.Target.UrlType;

                                    //if URL type not specified then default it to Akamai URL
                                    if (string.IsNullOrEmpty(tranlatedUrlType))
                                    {
                                        tranlatedUrlType = "akamaiUrl";
                                    }

                                    //Adds the translated URL
                                    playlist.TranslatedUrls.Add(new TranslatedUrlViewModel {
                                        UrlType = tranlatedUrlType, Url = translatedUrl
                                    });
                                }

                                // Add protectionType to properties if it doesn't already exist
                                if (playlist.ProtectionType.IsNullOrEmpty() && !translatedUrl.IsNullOrEmpty())
                                {
                                    playlist.ProtectionType = pt.Target.ProtectionType;
                                }
                            }
                        }
                    }
                }
            }
        }