Наследование: IAssetFile, ICloudMediaContextInit
        /// <summary>
        /// Creates the manifest asset file asynchronously.
        /// </summary>
        /// <param name="ingestManifestAsset">The parent manifest asset.</param>
        /// <param name="filePath">The file path.</param>
        /// <param name="token"><see cref="CancellationToken"/></param>
        /// <returns><see cref="Task"/>of type <see cref="IIngestManifestFile"/></returns>
        public Task <IIngestManifestFile> CreateAsync(IIngestManifestAsset ingestManifestAsset, string filePath, CancellationToken token)
        {
            if (ingestManifestAsset == null)
            {
                throw new ArgumentNullException("ingestManifestAsset");
            }

            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, StringTable.ErrorCreatingIngestManifestFileEmptyFilePath));
            }

            AssetCreationOptions options = ingestManifestAsset.Asset.Options;

            Task <IIngestManifestFile> rootTask = new Task <IIngestManifestFile>(() =>
            {
                token.ThrowIfCancellationRequested();

                IngestManifestAssetCollection.VerifyManifestAsset(ingestManifestAsset);
                if (!File.Exists(filePath))
                {
                    throw new FileNotFoundException(String.Format(CultureInfo.InvariantCulture, StringTable.BulkIngestProvidedFileDoesNotExist, filePath));
                }
                FileInfo info = new FileInfo(filePath);

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

                // Set a MIME type based on the extension of the file name
                string mimeType = AssetFileData.GetMimeType(filePath);

                IngestManifestFileData data = new IngestManifestFileData
                {
                    Name     = info.Name,
                    MimeType = mimeType,
                    ParentIngestManifestId      = ingestManifestAsset.ParentIngestManifestId,
                    ParentIngestManifestAssetId = ingestManifestAsset.Id,
                    Path = filePath,
                };

                SetEncryptionSettings(ingestManifestAsset, info, options, data);

                dataContext.AddObject(EntitySet, data);

                Task <IIngestManifestFile> task = dataContext.SaveChangesAsync(data).ContinueWith <IIngestManifestFile>(t =>
                {
                    t.ThrowIfFaulted();
                    token.ThrowIfCancellationRequested();
                    IngestManifestFileData ingestManifestFile = (IngestManifestFileData)t.AsyncState;
                    return(ingestManifestFile);
                });

                return(task.Result);
            });

            rootTask.Start();
            return(rootTask);
        }
Пример #2
0
        /// <summary>
        /// Adds the encryption metadata to the file info.
        /// </summary>
        /// <param name="file">The file information to update.</param>
        /// <param name="fileEncryption">The file encryption to use.</param>
        internal static void AddEncryptionMetadataToAssetFile(AssetFileData file, FileEncryption fileEncryption)
        {
            ulong iv = fileEncryption.GetInitializationVectorForFile(file.Name);

            file.IsEncrypted          = true;
            file.EncryptionKeyId      = fileEncryption.GetKeyIdentifierAsString();
            file.EncryptionScheme     = FileEncryption.SchemeName;
            file.EncryptionVersion    = FileEncryption.SchemeVersion;
            file.InitializationVector = iv.ToString(CultureInfo.InvariantCulture);
        }
        /// <summary>
        /// Creates the manifest asset file asynchronously.
        /// </summary>
        /// <param name="ingestManifestAsset">The parent manifest asset.</param>
        /// <param name="filePath">The file path.</param>
        /// <param name="token"><see cref="CancellationToken"/></param>
        /// <returns><see cref="Task"/>of type <see cref="IIngestManifestFile"/></returns>
        public Task <IIngestManifestFile> CreateAsync(IIngestManifestAsset ingestManifestAsset, string filePath, CancellationToken token)
        {
            if (ingestManifestAsset == null)
            {
                throw new ArgumentNullException("ingestManifestAsset");
            }

            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, StringTable.ErrorCreatingIngestManifestFileEmptyFilePath));
            }

            AssetCreationOptions options = ingestManifestAsset.Asset.Options;

            Task <IIngestManifestFile> rootTask = new Task <IIngestManifestFile>(() =>
            {
                token.ThrowIfCancellationRequested();

                IngestManifestAssetCollection.VerifyManifestAsset(ingestManifestAsset);

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

                // Set a MIME type based on the extension of the file name
                string mimeType = AssetFileData.GetMimeType(filePath);

                IngestManifestFileData data = new IngestManifestFileData
                {
                    Name     = Path.GetFileName(filePath),
                    MimeType = mimeType,
                    ParentIngestManifestId      = ingestManifestAsset.ParentIngestManifestId,
                    ParentIngestManifestAssetId = ingestManifestAsset.Id,
                    Path = filePath,
                };

                SetEncryptionSettings(ingestManifestAsset, options, data);

                dataContext.AddObject(EntitySet, data);

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

                Task <IIngestManifestFile> task = retryPolicy.ExecuteAsync <IMediaDataServiceResponse>(() => dataContext.SaveChangesAsync(data))
                                                  .ContinueWith <IIngestManifestFile>(t =>
                {
                    t.ThrowIfFaulted();
                    token.ThrowIfCancellationRequested();
                    IngestManifestFileData ingestManifestFile = (IngestManifestFileData)t.Result.AsyncState;
                    return(ingestManifestFile);
                });

                return(task.Result);
            });

            rootTask.Start();
            return(rootTask);
        }
Пример #4
0
        /// <summary>
        /// Asynchronously saves this instance.
        /// </summary>
        /// <returns>A function delegate that returns the future result to be available through the Task.</returns>
        public Task UpdateAsync()
        {
            if (this.Asset.State != AssetState.Initialized)
            {
                throw new NotSupportedException(StringTable.NotSupportedFileInfoSave);
            }

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

            dataContext.AttachTo(FileSet, this);
            dataContext.UpdateObject(this);

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

            return(retryPolicy.ExecuteAsync <IMediaDataServiceResponse>(() => dataContext.SaveChangesAsync(this))
                   .ContinueWith <IAssetFile>(
                       t =>
            {
                t.ThrowIfFaulted();
                AssetFileData data = (AssetFileData)t.Result.AsyncState;
                return data;
            }));
        }
        public override Task<IAssetFile> CreateAsync(string name, CancellationToken cancelation)
        {
            if (_parentAsset == null)
            {
                throw new InvalidOperationException(StringTable.AssetFileCreateParentAssetIsNull);
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, StringTable.ErrorCreatingAssetFileEmptyFileName));
            }
            cancelation.ThrowIfCancellationRequested();
            IMediaDataServiceContext dataContext = null;
            AssetFileData assetFile = null;
            return Task.Factory.StartNew(() =>
            {
                cancelation.ThrowIfCancellationRequested();
                dataContext = MediaContext.MediaServicesClassFactory.CreateDataServiceContext();

                bool isEncrypted = _parentAsset.Options.HasFlag(AssetCreationOptions.CommonEncryptionProtected) || _parentAsset.Options.HasFlag(AssetCreationOptions.StorageEncrypted) ||
                                   _parentAsset.Options.HasFlag(AssetCreationOptions.EnvelopeEncryptionProtected);

                string scheme = null;
                string schemeVersion = null;
                string encryptionKeyId = null;

                if (_parentAsset.Options.HasFlag(AssetCreationOptions.StorageEncrypted))
                {
                    IContentKey storageEncryptionKey = _parentAsset.ContentKeys.Where(c => c.ContentKeyType == ContentKeyType.StorageEncryption).FirstOrDefault();
                    cancelation.ThrowIfCancellationRequested();
                    if (storageEncryptionKey != null)
                    {
                        encryptionKeyId = storageEncryptionKey.Id.ToString();
                    }
                    else
                    {
                        throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, StringTable.StorageEncryptionContentKeyIsMissing, _parentAsset.Id));
                    }

                    scheme = FileEncryption.SchemeName;
                    schemeVersion = FileEncryption.SchemeVersion;
                }
                else if (_parentAsset.Options.HasFlag(AssetCreationOptions.CommonEncryptionProtected))
                {
                    scheme = CommonEncryption.SchemeName;
                    schemeVersion = CommonEncryption.SchemeVersion;
                }
                else if (_parentAsset.Options.HasFlag(AssetCreationOptions.EnvelopeEncryptionProtected))
                {
                    scheme = EnvelopeEncryption.SchemeName;
                    schemeVersion = EnvelopeEncryption.SchemeVersion;
                }

                // Set a MIME type based on the extension of the file name
                string mimeType = AssetFileData.GetMimeType(name);
                assetFile = new AssetFileData
                {
                    Name = name,
                    IsEncrypted = isEncrypted,
                    EncryptionScheme = scheme,
                    EncryptionVersion = schemeVersion,
                    EncryptionKeyId = encryptionKeyId,
                    ParentAssetId = _parentAsset.Id,
                    MimeType = mimeType,
                };

                dataContext.AddObject(AssetFileCollection.FileSet, assetFile);
                cancelation.ThrowIfCancellationRequested();
                cancelation.ThrowIfCancellationRequested();
                MediaRetryPolicy retryPolicy = this.MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy(dataContext as IRetryPolicyAdapter);
                return retryPolicy.ExecuteAsync<IMediaDataServiceResponse>(() =>
                {
                    cancelation.ThrowIfCancellationRequested();
                    return dataContext.SaveChangesAsync(assetFile);

                }, cancelation).Result;
            }, cancelation)
            .ContinueWith<IAssetFile>(t =>
            {

                t.ThrowIfFaulted();
                AssetFileData data = (AssetFileData)t.Result.AsyncState;
                return data;
            }, cancelation);
        }
Пример #6
0
        public override Task <IAssetFile> CreateAsync(string name, CancellationToken cancelation)
        {
            if (_parentAsset == null)
            {
                throw new InvalidOperationException(StringTable.AssetFileCreateParentAssetIsNull);
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, StringTable.ErrorCreatingAssetFileEmptyFileName));
            }

            cancelation.ThrowIfCancellationRequested();
            IMediaDataServiceContext dataContext = null;
            AssetFileData            assetFile   = null;

            return(Task.Factory.StartNew(() =>
            {
                cancelation.ThrowIfCancellationRequested();
                dataContext = MediaContext.MediaServicesClassFactory.CreateDataServiceContext();

                FileEncryption fileEncryption = null;

                // Set a MIME type based on the extension of the file name
                string mimeType = AssetFileData.GetMimeType(name);
                assetFile = new AssetFileData
                {
                    Name = name,
                    ParentAssetId = _parentAsset.Id,
                    MimeType = mimeType,
                };
                try
                {
                    // Update the files associated with the asset with the encryption related metadata.
                    if (_parentAsset.Options.HasFlag(AssetCreationOptions.StorageEncrypted))
                    {
                        IContentKey storageEncryptionKey = _parentAsset.ContentKeys.Where(c => c.ContentKeyType == ContentKeyType.StorageEncryption).FirstOrDefault();
                        cancelation.ThrowIfCancellationRequested();
                        if (storageEncryptionKey == null)
                        {
                            throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, StringTable.StorageEncryptionContentKeyIsMissing, _parentAsset.Id));
                        }
                        fileEncryption = new FileEncryption(storageEncryptionKey.GetClearKeyValue(), EncryptionUtils.GetKeyIdAsGuid(storageEncryptionKey.Id));
                        AssetBaseCollection.AddEncryptionMetadataToAssetFile(assetFile, fileEncryption);
                    }
                    else if (_parentAsset.Options.HasFlag(AssetCreationOptions.CommonEncryptionProtected))
                    {
                        AssetBaseCollection.SetAssetFileForCommonEncryption(assetFile);
                    }
                    else if (_parentAsset.Options.HasFlag(AssetCreationOptions.EnvelopeEncryptionProtected))
                    {
                        AssetBaseCollection.SetAssetFileForEnvelopeEncryption(assetFile);
                    }
                }
                finally
                {
                    if (fileEncryption != null)
                    {
                        fileEncryption.Dispose();
                    }
                }
                dataContext.AddObject(FileSet, assetFile);
                cancelation.ThrowIfCancellationRequested();
                cancelation.ThrowIfCancellationRequested();
                MediaRetryPolicy retryPolicy = this.MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy(dataContext as IRetryPolicyAdapter);
                return retryPolicy.ExecuteAsync <IMediaDataServiceResponse>(() =>
                {
                    cancelation.ThrowIfCancellationRequested();
                    return dataContext.SaveChangesAsync(assetFile);
                }, cancelation).Result;
            }, cancelation)
                   .ContinueWith <IAssetFile>(t =>
            {
                t.ThrowIfFaulted();
                AssetFileData data = (AssetFileData)t.Result.AsyncState;
                return data;
            }, cancelation));
        }
Пример #7
0
        public override Task <IAssetFile> CreateAsync(string name, CancellationToken cancelation)
        {
            if (_parentAsset == null)
            {
                throw new InvalidOperationException(StringTable.AssetFileCreateParentAssetIsNull);
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, StringTable.ErrorCreatingAssetFileEmptyFileName));
            }
            cancelation.ThrowIfCancellationRequested();
            var dataContext = _cloudMediaContext.DataContextFactory.CreateDataServiceContext();

            bool isEncrypted = _parentAsset.Options.HasFlag(AssetCreationOptions.CommonEncryptionProtected) ||
                               _parentAsset.Options.HasFlag(AssetCreationOptions.StorageEncrypted) ||
                               _parentAsset.Options.HasFlag(AssetCreationOptions.EnvelopeEncryptionProtected);

            string scheme          = null;
            string schemeVersion   = null;
            string encryptionKeyId = null;

            if (_parentAsset.Options.HasFlag(AssetCreationOptions.StorageEncrypted))
            {
                IContentKey storageEncryptionKey = _parentAsset.ContentKeys.Where(c => c.ContentKeyType == ContentKeyType.StorageEncryption).FirstOrDefault();
                if (storageEncryptionKey != null)
                {
                    encryptionKeyId = storageEncryptionKey.Id.ToString();
                }
                else
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, StringTable.StorageEncryptionContentKeyIsMissing, _parentAsset.Id));
                }

                scheme        = FileEncryption.SchemeName;
                schemeVersion = FileEncryption.SchemeVersion;
            }
            else if (_parentAsset.Options.HasFlag(AssetCreationOptions.CommonEncryptionProtected))
            {
                scheme        = CommonEncryption.SchemeName;
                schemeVersion = CommonEncryption.SchemeVersion;
            }
            else if (_parentAsset.Options.HasFlag(AssetCreationOptions.EnvelopeEncryptionProtected))
            {
                scheme        = EnvelopeEncryption.SchemeName;
                schemeVersion = EnvelopeEncryption.SchemeVersion;
            }

            // Set a MIME type based on the extension of the file name
            string mimeType  = AssetFileData.GetMimeType(name);
            var    assetFile = new AssetFileData
            {
                Name              = name,
                IsEncrypted       = isEncrypted,
                EncryptionScheme  = scheme,
                EncryptionVersion = schemeVersion,
                EncryptionKeyId   = encryptionKeyId,
                ParentAssetId     = _parentAsset.Id,
                MimeType          = mimeType,
            };

            dataContext.AddObject(AssetFileCollection.FileSet, assetFile);
            cancelation.ThrowIfCancellationRequested();
            return(dataContext.SaveChangesAsync(assetFile).ContinueWith <IAssetFile>(t =>
            {
                t.ThrowIfFaulted();
                AssetFileData data = (AssetFileData)t.AsyncState;
                IAssetFile file = this._cloudMediaContext.Files.Where(c => c.Id == data.Id).First();
                return file;
            }));
        }
Пример #8
0
 /// <summary>
 /// Sets the file info for envelope encryption.
 /// </summary>
 /// <param name="file">The file info to update.</param>
 internal static void SetAssetFileForEnvelopeEncryption(AssetFileData file)
 {
     file.IsEncrypted       = true;
     file.EncryptionScheme  = EnvelopeEncryption.SchemeName;
     file.EncryptionVersion = EnvelopeEncryption.SchemeVersion;
 }
        public override Task<IAssetFile> CreateAsync(string name, CancellationToken cancelation)
        {
            if (_parentAsset == null)
            {
                throw new InvalidOperationException(StringTable.AssetFileCreateParentAssetIsNull);
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, StringTable.ErrorCreatingAssetFileEmptyFileName));
            }
            cancelation.ThrowIfCancellationRequested();
            IMediaDataServiceContext dataContext = null;
            AssetFileData assetFile = null;
            return Task.Factory.StartNew(() =>
            {
                cancelation.ThrowIfCancellationRequested();
                dataContext = MediaContext.MediaServicesClassFactory.CreateDataServiceContext();

                FileEncryption fileEncryption = null;

                // Set a MIME type based on the extension of the file name
                string mimeType = AssetFileData.GetMimeType(name);
                assetFile = new AssetFileData
                {
                    Name = name,
                    ParentAssetId = _parentAsset.Id,
                    MimeType = mimeType,
                };
                try
                {
                    // Update the files associated with the asset with the encryption related metadata.
                    if (_parentAsset.Options.HasFlag(AssetCreationOptions.StorageEncrypted))
                    {
                        IContentKey storageEncryptionKey = _parentAsset.ContentKeys.Where(c => c.ContentKeyType == ContentKeyType.StorageEncryption).FirstOrDefault();
                        cancelation.ThrowIfCancellationRequested();
                        if (storageEncryptionKey == null)
                        {
                            throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, StringTable.StorageEncryptionContentKeyIsMissing, _parentAsset.Id));
                        }
                        fileEncryption = new FileEncryption(storageEncryptionKey.GetClearKeyValue(), EncryptionUtils.GetKeyIdAsGuid(storageEncryptionKey.Id));
                        AssetBaseCollection.AddEncryptionMetadataToAssetFile(assetFile, fileEncryption);
                    }
                    else if (_parentAsset.Options.HasFlag(AssetCreationOptions.CommonEncryptionProtected))
                    {
                        AssetBaseCollection.SetAssetFileForCommonEncryption(assetFile);
                    }
                    else if (_parentAsset.Options.HasFlag(AssetCreationOptions.EnvelopeEncryptionProtected))
                    {
                        AssetBaseCollection.SetAssetFileForEnvelopeEncryption(assetFile);
                    }
                }
                finally
                {
                    if (fileEncryption != null)
                    {
                        fileEncryption.Dispose();
                    }
                }
                dataContext.AddObject(FileSet, assetFile);
                cancelation.ThrowIfCancellationRequested();
                cancelation.ThrowIfCancellationRequested();
                MediaRetryPolicy retryPolicy = this.MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy(dataContext as IRetryPolicyAdapter);
                return retryPolicy.ExecuteAsync<IMediaDataServiceResponse>(() =>
                {
                    cancelation.ThrowIfCancellationRequested();
                    return dataContext.SaveChangesAsync(assetFile);

                }, cancelation).Result;
            }, cancelation)
            .ContinueWith<IAssetFile>(t =>
            {

                t.ThrowIfFaulted();
                AssetFileData data = (AssetFileData)t.Result.AsyncState;
                return data;
            }, cancelation);
        }