Inheritance: IAsset, ICloudMediaContextInit
        /// <summary>
        /// Asynchronously creates an asset that does not contain any files and <see cref="AssetState"/> is Initialized.
        /// </summary>
        /// <param name="assetName">The asset name.</param>
        /// <param name="options">A <see cref="AssetCreationOptions"/> which will be associated with created asset.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// An <see cref="Task"/> of type <see cref="IAsset"/>created according to the specified creation <paramref name="options"/>.
        /// </returns>
        public override Task<IAsset> CreateAsync(string assetName, AssetCreationOptions options,CancellationToken cancellationToken)
        {
            AssetData emptyAsset = new AssetData
                                       {
                                           Name = assetName,
                                           Options = (int) options
                                       };

            emptyAsset.InitCloudMediaContext(this._cloudMediaContext);

            cancellationToken.ThrowIfCancellationRequested();
            DataServiceContext dataContext = this._cloudMediaContext.DataContextFactory.CreateDataServiceContext();
            dataContext.AddObject(AssetSet, emptyAsset);

            return dataContext
                .SaveChangesAsync(emptyAsset)
                .ContinueWith<IAsset>(
                    t =>
                        {
                            t.ThrowIfFaulted();
                            cancellationToken.ThrowIfCancellationRequested();

                            AssetData data = (AssetData) t.AsyncState;
                            if (options.HasFlag(AssetCreationOptions.StorageEncrypted))
                            {
                                using (var fileEncryption = new NullableFileEncryption())
                                {
                                    CreateStorageContentKey(data, fileEncryption, dataContext);
                                }
                            }

                            return data;
                        });
        }
        /// <summary>
        /// Asynchronously updates this asset instance.
        /// </summary>
        /// <returns>A function delegate that returns the future result to be available through the Task.</returns>
        public Task UpdateAsync()
        {
            AssetCollection.VerifyAsset(this);

            IMediaDataServiceContext dataContext = this._mediaContextBase.MediaServicesClassFactory.CreateDataServiceContext();

            dataContext.AttachTo(AssetCollection.AssetSet, this);
            dataContext.UpdateObject(this);

            MediaRetryPolicy retryPolicy = this._mediaContextBase.MediaServicesClassFactory.GetSaveChangesRetryPolicy();

            return(retryPolicy.ExecuteAsync <IMediaDataServiceResponse>(() => dataContext.SaveChangesAsync(this))
                   .ContinueWith <IAsset>(
                       t =>
            {
                t.ThrowIfFaulted();
                AssetData data = (AssetData)t.Result.AsyncState;
                return data;
            }));
        }
        private ContentKeyData CreateStorageContentKey(AssetData tempAsset, NullableFileEncryption fileEncryption, IMediaDataServiceContext dataContext)
        {
            // Create the content key.
            fileEncryption.Init();

            // Encrypt it for delivery to Nimbus.
            X509Certificate2 certToUse      = ContentKeyCollection.GetCertificateToEncryptContentKey(MediaContext, ContentKeyType.StorageEncryption);
            ContentKeyData   contentKeyData = ContentKeyBaseCollection.InitializeStorageContentKey(fileEncryption.FileEncryption, certToUse);

            dataContext.AddObject(ContentKeyBaseCollection.ContentKeySet, contentKeyData);

            MediaRetryPolicy retryPolicy = this.MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy(dataContext as IRetryPolicyAdapter);

            retryPolicy.ExecuteAction <IMediaDataServiceResponse>(() => dataContext.SaveChanges());

            // Associate it with the asset.
            ((IAsset)tempAsset).ContentKeys.Add(contentKeyData);

            return(contentKeyData);
        }
        /// <summary>
        /// Creates the locator async.
        /// </summary>
        /// <param name="locatorType">Type of the locator.</param>
        /// <param name="asset">The asset.</param>
        /// <param name="accessPolicy">The access policy.</param>
        /// <param name="startTime">The start time.</param>
        /// <param name="name">The name.</param>
        /// <returns>
        /// A function delegate that returns the future result to be available through the Task&lt;ILocator&gt;.
        /// </returns>
        public Task <ILocator> CreateLocatorAsync(LocatorType locatorType, IAsset asset, IAccessPolicy accessPolicy, DateTime?startTime, string name = null)
        {
            AccessPolicyBaseCollection.VerifyAccessPolicy(accessPolicy);
            AssetCollection.VerifyAsset(asset);

            AssetData assetData = (AssetData)asset;

            LocatorData locator = new LocatorData
            {
                AccessPolicy = (AccessPolicyData)accessPolicy,
                Asset        = assetData,
                AssetId      = assetData.Id,
                Type         = (int)locatorType,
                StartTime    = startTime,
                Name         = name
            };

            locator.SetMediaContext(this.MediaContext);
            IMediaDataServiceContext dataContext = this.MediaContext.MediaServicesClassFactory.CreateDataServiceContext();

            dataContext.AttachTo(AssetCollection.AssetSet, asset);
            dataContext.AttachTo(AccessPolicyBaseCollection.AccessPolicySet, accessPolicy);
            dataContext.AddObject(LocatorSet, locator);
            dataContext.SetLink(locator, AccessPolicyPropertyName, accessPolicy);
            dataContext.SetLink(locator, AssetPropertyName, asset);

            MediaRetryPolicy retryPolicy = this.MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy();

            return(retryPolicy.ExecuteAsync <IMediaDataServiceResponse>(() => dataContext.SaveChangesAsync(locator))
                   .ContinueWith <ILocator>(
                       t =>
            {
                t.ThrowIfFaulted();

                assetData.InvalidateLocatorsCollection();

                return (LocatorData)t.Result.AsyncState;
            },
                       TaskContinuationOptions.ExecuteSynchronously));
        }
Exemple #5
0
        /// <summary>
        /// Creates the locator async.
        /// </summary>
        /// <param name="locatorType">Type of the locator.</param>
        /// <param name="asset">The asset.</param>
        /// <param name="accessPolicy">The access policy.</param>
        /// <param name="startTime">The start time.</param>
        /// <returns>A function delegate that returns the future result to be available through the Task&lt;ILocator&gt;.</returns>
        public Task <ILocator> CreateLocatorAsync(LocatorType locatorType, IAsset asset, IAccessPolicy accessPolicy, DateTime?startTime)
        {
            AccessPolicyBaseCollection.VerifyAccessPolicy(accessPolicy);
            AssetCollection.VerifyAsset(asset);

            AssetData assetData = (AssetData)asset;

            LocatorData locator = new LocatorData
            {
                AccessPolicy = (AccessPolicyData)accessPolicy,
                Asset        = assetData,
                Type         = (int)locatorType,
                StartTime    = startTime,
            };

            locator.InitCloudMediaContext(this._cloudMediaContext);

            DataServiceContext dataContext = this.DataContextFactory.CreateDataServiceContext();

            dataContext.AttachTo(AssetCollection.AssetSet, asset);
            dataContext.AttachTo(AccessPolicyBaseCollection.AccessPolicySet, accessPolicy);
            dataContext.AddObject(LocatorSet, locator);
            dataContext.SetLink(locator, AccessPolicyPropertyName, accessPolicy);
            dataContext.SetLink(locator, AssetPropertyName, asset);

            return(dataContext
                   .SaveChangesAsync(locator)
                   .ContinueWith <ILocator>(
                       t =>
            {
                t.ThrowIfFaulted();

                assetData.InvalidateLocatorsCollection();

                return (LocatorData)t.AsyncState;
            },
                       TaskContinuationOptions.ExecuteSynchronously));
        }
        /// <summary>
        /// Asynchronously creates an asset for specified storage account. Asset  does not contain any files and <see cref="AssetState" /> is Initialized.
        /// </summary>
        /// <param name="assetName">The asset name.</param>
        /// <param name="storageAccountName">The storage account name</param>
        /// <param name="options">A <see cref="AssetCreationOptions" /> which will be associated with created asset.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// An <see cref="Task" /> of type <see cref="IAsset" />, where IAsset created according to the specified creation <paramref name="options" />.
        /// </returns>
        public override Task <IAsset> CreateAsync(string assetName, string storageAccountName, AssetCreationOptions options, CancellationToken cancellationToken)
        {
            AssetData emptyAsset = new AssetData
            {
                Name               = assetName,
                Options            = (int)options,
                StorageAccountName = storageAccountName
            };

            emptyAsset.SetMediaContext(this.MediaContext);

            cancellationToken.ThrowIfCancellationRequested();
            IMediaDataServiceContext dataContext = this.MediaContext.MediaServicesClassFactory.CreateDataServiceContext();

            dataContext.AddObject(AssetSet, (IAsset)emptyAsset);

            MediaRetryPolicy retryPolicy = this.MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy(dataContext as IRetryPolicyAdapter);

            return(retryPolicy.ExecuteAsync <IMediaDataServiceResponse>(() => dataContext.SaveChangesAsync(emptyAsset))
                   .ContinueWith <IAsset>(
                       t =>
            {
                t.ThrowIfFaulted();
                cancellationToken.ThrowIfCancellationRequested();

                AssetData data = (AssetData)t.Result.AsyncState;
                if (options.HasFlag(AssetCreationOptions.StorageEncrypted))
                {
                    using (var fileEncryption = new NullableFileEncryption())
                    {
                        CreateStorageContentKey(data, fileEncryption, dataContext);
                    }
                }

                return data;
            }));
        }
Exemple #7
0
        /// <summary>
        /// Asynchronously creates an asset for specified storage account. Asset  does not contain any files and <see cref="AssetState" /> is Initialized.
        /// </summary>
        /// <param name="assetName">The asset name.</param>
        /// <param name="storageAccountName">The storage account name</param>
        /// <param name="options">A <see cref="AssetCreationOptions" /> which will be associated with created asset.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// An <see cref="Task" /> of type <see cref="IAsset" />, where IAsset created according to the specified creation <paramref name="options" />.
        /// </returns>
        public override Task <IAsset> CreateAsync(string assetName, string storageAccountName, AssetCreationOptions options, CancellationToken cancellationToken)
        {
            AssetData emptyAsset = new AssetData
            {
                Name               = assetName,
                Options            = (int)options,
                StorageAccountName = storageAccountName
            };

            emptyAsset.InitCloudMediaContext(this._cloudMediaContext);

            cancellationToken.ThrowIfCancellationRequested();
            DataServiceContext dataContext = this._cloudMediaContext.DataContextFactory.CreateDataServiceContext();

            dataContext.AddObject(AssetSet, emptyAsset);

            return(dataContext
                   .SaveChangesAsync(emptyAsset)
                   .ContinueWith <IAsset>(
                       t =>
            {
                t.ThrowIfFaulted();
                cancellationToken.ThrowIfCancellationRequested();

                AssetData data = (AssetData)t.AsyncState;
                if (options.HasFlag(AssetCreationOptions.StorageEncrypted))
                {
                    using (var fileEncryption = new NullableFileEncryption())
                    {
                        CreateStorageContentKey(data, fileEncryption, dataContext);
                    }
                }

                return data;
            }));
        }
        private ContentKeyData CreateStorageContentKey(AssetData tempAsset, NullableFileEncryption fileEncryption, IMediaDataServiceContext dataContext)
        {
            // Create the content key.
            fileEncryption.Init();

            // Encrypt it for delivery to Nimbus.
            X509Certificate2 certToUse = ContentKeyCollection.GetCertificateToEncryptContentKey(dataContext, ContentKeyType.StorageEncryption);
            ContentKeyData contentKeyData = ContentKeyBaseCollection.CreateStorageContentKey(fileEncryption.FileEncryption, certToUse);

            dataContext.AddObject(ContentKeyCollection.ContentKeySet, contentKeyData);

            MediaRetryPolicy retryPolicy = this.MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy();

            retryPolicy.ExecuteAction<IMediaDataServiceResponse>(() => dataContext.SaveChanges());

            // Associate it with the asset.
            ((IAsset) tempAsset).ContentKeys.Add(contentKeyData);

            return contentKeyData;
        }
        /// <summary>
        /// Asynchronously creates an asset for specified storage account. Asset  does not contain any files and <see cref="AssetState" /> is Initialized.
        /// </summary>
        /// <param name="assetName">The asset name.</param>
        /// <param name="storageAccountName">The storage account name</param>
        /// <param name="options">A <see cref="AssetCreationOptions" /> which will be associated with created asset.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// An <see cref="Task" /> of type <see cref="IAsset" />, where IAsset created according to the specified creation <paramref name="options" />.
        /// </returns>
        public override Task<IAsset> CreateAsync(string assetName, string storageAccountName, AssetCreationOptions options, CancellationToken cancellationToken)
        {
            AssetData emptyAsset = new AssetData
            {
                Name = assetName,
                Options = (int)options,
                StorageAccountName = storageAccountName
            };

            cancellationToken.ThrowIfCancellationRequested();
            IMediaDataServiceContext dataContext = this.MediaContext.MediaServicesClassFactory.CreateDataServiceContext();
            dataContext.AddObject(AssetSet, (IAsset)emptyAsset);

            MediaRetryPolicy retryPolicy = this.MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy();

            return retryPolicy.ExecuteAsync<IMediaDataServiceResponse>(() => dataContext.SaveChangesAsync(emptyAsset))
                .ContinueWith<IAsset>(
                    t =>
                    {
                        t.ThrowIfFaulted();
                        cancellationToken.ThrowIfCancellationRequested();

                        AssetData data = (AssetData)t.Result.AsyncState;
                        if (options.HasFlag(AssetCreationOptions.StorageEncrypted))
                        {
                            using (var fileEncryption = new NullableFileEncryption())
                            {
                                CreateStorageContentKey(data, fileEncryption, dataContext);
                            }
                        }

                        return data;
                    });
        }
        private void InnerSubmit(DataServiceContext dataContext)
        {
            if (!string.IsNullOrWhiteSpace(this.TemplateId))
            {
                dataContext.AddObject(JobBaseCollection.JobSet, this);

                foreach (IAsset asset in this.InputMediaAssets)
                {
                    AssetData target = asset as AssetData;
                    if (target == null)
                    {
                        throw new ArgumentException(StringTable.ErrorInputTypeNotSupported);
                    }

                    dataContext.AttachTo(AssetCollection.AssetSet, asset);
                    dataContext.AddLink(this, InputMediaAssetsPropertyName, target);
                }
            }
            else
            {
                X509Certificate2 certToUse = null;
                Verify(this);

                dataContext.AddObject(JobBaseCollection.JobSet, this);

                List<AssetData> inputAssets = new List<AssetData>();
                AssetNamingSchemeResolver<AssetData, OutputAsset> assetNamingSchemeResolver = new AssetNamingSchemeResolver<AssetData, OutputAsset>(inputAssets);

                foreach (ITask task in ((IJob)this).Tasks)
                {
                    Verify(task);
                    TaskData taskData = (TaskData)task;

                    if (task.Options.HasFlag(TaskOptions.ProtectedConfiguration))
                    {
                        ProtectTaskConfiguration(taskData, ref certToUse, dataContext);
                    }

                    taskData.TaskBody = CreateTaskBody(assetNamingSchemeResolver, task.InputAssets.ToArray(), task.OutputAssets.ToArray());
                    taskData.InputMediaAssets.AddRange(task.InputAssets.OfType<AssetData>().ToArray());
                    taskData.OutputMediaAssets.AddRange(
                        task.OutputAssets
                            .OfType<OutputAsset>()
                            .Select(
                                c =>
                                {
                                    AssetData assetData = new AssetData { Name = c.Name, Options = (int)c.Options, AlternateId = c.AlternateId };
                                    assetData.InitCloudMediaContext(this._cloudMediaContext);

                                    return assetData;
                                })
                            .ToArray());
                    dataContext.AddRelatedObject(this, TasksPropertyName, taskData);
                }

                foreach (IAsset asset in inputAssets)
                {
                    dataContext.AttachTo(AssetCollection.AssetSet, asset);
                    dataContext.AddLink(this, InputMediaAssetsPropertyName, asset);
                }
            }
        }
Exemple #11
0
        private void InnerSubmit(IMediaDataServiceContext dataContext)
        {
            if (!string.IsNullOrWhiteSpace(this.TemplateId))
            {
                dataContext.AddObject(JobBaseCollection.JobSet, this);

                foreach (IAsset asset in this.InputMediaAssets)
                {
                    AssetData target = asset as AssetData;
                    if (target == null)
                    {
                        throw new ArgumentException(StringTable.ErrorInputTypeNotSupported);
                    }

                    dataContext.AttachTo(AssetCollection.AssetSet, asset);
                    dataContext.AddLink(this, InputMediaAssetsPropertyName, target);
                }
            }
            else
            {
                X509Certificate2 certToUse = null;
                Verify(this);

                dataContext.AddObject(JobBaseCollection.JobSet, this);

                List <AssetData> inputAssets = new List <AssetData>();
                AssetNamingSchemeResolver <AssetData, OutputAsset> assetNamingSchemeResolver = new AssetNamingSchemeResolver <AssetData, OutputAsset>(inputAssets);

                foreach (ITask task in ((IJob)this).Tasks)
                {
                    Verify(task);
                    TaskData taskData = (TaskData)task;

                    if (task.Options.HasFlag(TaskOptions.ProtectedConfiguration))
                    {
                        ProtectTaskConfiguration(taskData, ref certToUse, dataContext);
                    }

                    taskData.TaskBody = CreateTaskBody(assetNamingSchemeResolver, task.InputAssets.ToArray(), task.OutputAssets.ToArray());
                    taskData.InputMediaAssets.AddRange(task.InputAssets.OfType <AssetData>().ToArray());
                    taskData.OutputMediaAssets.AddRange(
                        task.OutputAssets
                        .OfType <OutputAsset>()
                        .Select(
                            c =>
                    {
                        AssetData assetData = new AssetData {
                            Name = c.Name, Options = (int)c.Options, AlternateId = c.AlternateId
                        };
                        assetData.SetMediaContext(this.GetMediaContext());

                        return(assetData);
                    })
                        .ToArray());
                    dataContext.AddRelatedObject(this, TasksPropertyName, taskData);
                }

                foreach (IAsset asset in inputAssets)
                {
                    dataContext.AttachTo(AssetCollection.AssetSet, asset);
                    dataContext.AddLink(this, InputMediaAssetsPropertyName, asset);
                }
            }
        }
        private static ContentKeyData CreateStorageContentKey(AssetData tempAsset, NullableFileEncryption fileEncryption, DataServiceContext dataContext)
        {
            // Create the content key.
            fileEncryption.Init();

            // Encrypt it for delivery to Nimbus.
            X509Certificate2 certToUse = ContentKeyCollection.GetCertificateToEncryptContentKey(dataContext, ContentKeyType.StorageEncryption);
            ContentKeyData contentKeyData = ContentKeyBaseCollection.CreateStorageContentKey(fileEncryption.FileEncryption, certToUse);

            dataContext.AddObject(ContentKeyCollection.ContentKeySet, contentKeyData);
            dataContext.SaveChanges();

            // Associate it with the asset.
            ((IAsset) tempAsset).ContentKeys.Add(contentKeyData);

            return contentKeyData;
        }