Esempio n. 1
0
        private void PostUploadAction(Task task, string path, FileEncryption fileEncryption, AssetCreationOptions assetCreationOptions, CancellationToken token)
        {
            try
            {
                task.ThrowIfFaulted();
                token.ThrowIfCancellationRequested();

                FileInfo fileInfo = new FileInfo(path);

                //Updating Name based on file name to avoid exceptions trying to download file.Mapping to storage account is through file name
                this.Name = fileInfo.Name;

                // Set the ContentFileSize base on the local file size
                this.ContentFileSize = fileInfo.Length;

                // Update the files associated with the asset with the encryption related metadata.
                if (assetCreationOptions.HasFlag(AssetCreationOptions.StorageEncrypted))
                {
                    AssetBaseCollection.AddEncryptionMetadataToAssetFile(this, fileEncryption);
                }
                else if (assetCreationOptions.HasFlag(AssetCreationOptions.CommonEncryptionProtected))
                {
                    AssetBaseCollection.SetAssetFileForCommonEncryption(this);
                }
                else if (assetCreationOptions.HasFlag(AssetCreationOptions.EnvelopeEncryptionProtected))
                {
                    AssetBaseCollection.SetAssetFileForEnvelopeEncryption(this);
                }
                this.Update();
            }
            finally
            {
                if (fileEncryption != null)
                {
                    fileEncryption.Dispose();
                }
            }
        }
Esempio n. 2
0
        private void Cleanup(BlobTransferClient blobTransfer, FileEncryption fileEncryption, ILocator locator, IAccessPolicy accessPolicy)
        {
            lock (this._lock)
            {
                if (blobTransfer != null)
                {
                    try
                    {
                        blobTransfer.TransferProgressChanged -= this.OnDownloadBlobTransferProgressChanged;
                    }
                    catch
                    {
                    }
                    finally
                    {
                        blobTransfer = null;
                    }
                }

                if (fileEncryption != null)
                {
                    try
                    {
                        fileEncryption.Dispose();
                    }
                    catch
                    {
                    }
                    finally
                    {
                        fileEncryption = null;
                    }
                }

                if (locator != null)
                {
                    try
                    {
                        locator.Delete();
                    }
                    catch
                    {
                    }
                    finally
                    {
                        locator = null;
                    }
                }

                if (accessPolicy != null)
                {
                    try
                    {
                        accessPolicy.Delete();
                    }
                    catch
                    {
                    }
                    finally
                    {
                        accessPolicy = null;
                    }
                }
            }
        }
        private void AssetEncryptAction(string outputPath, bool overwriteExistingEncryptedFiles, CancellationToken cancellationToken, ConcurrentDictionary <string, IContentKey> keys, IIngestManifestAsset asset)
        {
            cancellationToken.ThrowIfCancellationRequested();

            List <Task> encryptTasks = new List <Task>();

            AssetCreationOptions assetCreationOptions = asset.Asset.Options;

            if (assetCreationOptions.HasFlag(AssetCreationOptions.StorageEncrypted))
            {
                IContentKey contentKeyData = keys[asset.Id];
                var         fileEncryption = new FileEncryption(contentKeyData.GetClearKeyValue(), EncryptionUtils.GetKeyIdAsGuid(contentKeyData.Id));


                foreach (IngestManifestFileData file in asset.IngestManifestFiles)
                {
                    ulong iv = Convert.ToUInt64(file.InitializationVector, CultureInfo.InvariantCulture);
                    fileEncryption.SetInitializationVectorForFile(file.Name, iv);

                    FileInfo fileInfo = null;
                    fileInfo = TrackedFilesPaths.ContainsKey(file.Id) ? new FileInfo(TrackedFilesPaths[file.Id]) : new FileInfo(file.Name);

                    string destinationPath = Path.Combine(outputPath, fileInfo.Name);
                    if (File.Exists(destinationPath))
                    {
                        if (overwriteExistingEncryptedFiles)
                        {
                            File.Delete(destinationPath);
                        }
                        else
                        {
                            throw new IOException(string.Format(CultureInfo.InvariantCulture, StringTable.BulkIngestFileExists, destinationPath));
                        }
                    }

                    long fileSize     = fileInfo.Length;
                    int  maxBlockSize = GetBlockSize(fileSize);

                    int numThreads = MaxNumberOfEncryptionThreadsForFilePerCore * Environment.ProcessorCount;
                    ConcurrentQueue <Tuple <int, int> > queue = PrepareUploadQueue(maxBlockSize, fileSize);
                    if (queue.Count < numThreads)
                    {
                        numThreads = queue.Count;
                    }


                    File.Create(destinationPath).Dispose();

                    Action action = GetEncryptionAction(cancellationToken, fileEncryption, file, destinationPath, fileInfo, queue, maxBlockSize);
                    for (int i = 0; i < numThreads; i++)
                    {
                        encryptTasks.Add(Task.Factory.StartNew((action), cancellationToken));
                    }
                }
                try
                {
                    Task.WaitAll(encryptTasks.ToArray());
                }
                finally
                {
                    fileEncryption.Dispose();
                }
            }
        }
        private void AssetEncryptAction(string outputPath, bool overwriteExistingEncryptedFiles, CancellationToken cancellationToken, ConcurrentDictionary<string, IContentKey> keys, IIngestManifestAsset asset)
        {
            cancellationToken.ThrowIfCancellationRequested();

            List<Task> encryptTasks = new List<Task>();

            AssetCreationOptions assetCreationOptions = asset.Asset.Options;
            if (assetCreationOptions.HasFlag(AssetCreationOptions.StorageEncrypted))
            {
                IContentKey contentKeyData = keys[asset.Id];
                var fileEncryption = new FileEncryption(contentKeyData.GetClearKeyValue(), EncryptionUtils.GetKeyIdAsGuid(contentKeyData.Id));

                foreach (IngestManifestFileData file in asset.IngestManifestFiles)
                {
                    ulong iv = Convert.ToUInt64(file.InitializationVector, CultureInfo.InvariantCulture);
                    fileEncryption.SetInitializationVectorForFile(file.Name, iv);

                    FileInfo fileInfo = null;
                    fileInfo = TrackedFilesPaths.ContainsKey(file.Id) ? new FileInfo(TrackedFilesPaths[file.Id]) : new FileInfo(file.Name);

                    string destinationPath = Path.Combine(outputPath, fileInfo.Name);
                    if (File.Exists(destinationPath))
                    {
                        if (overwriteExistingEncryptedFiles)
                        {
                            File.Delete(destinationPath);
                        }
                        else
                        {
                            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, StringTable.BulkIngestFileExists, destinationPath));
                        }
                    }

                    long fileSize = fileInfo.Length;
                    int maxBlockSize = GetBlockSize(fileSize);

                    int numThreads = MaxNumberOfEncryptionThreadsForFilePerCore*Environment.ProcessorCount;
                    ConcurrentQueue<Tuple<int, int>> queue = PrepareUploadQueue(maxBlockSize, fileSize);
                    if (queue.Count < numThreads)
                    {
                        numThreads = queue.Count;
                    }

                    File.Create(destinationPath).Dispose();

                    Action action = GetEncryptionAction(cancellationToken, fileEncryption, file, destinationPath, fileInfo, queue, maxBlockSize);
                    for (int i = 0; i < numThreads; i++)
                    {
                        encryptTasks.Add(Task.Factory.StartNew((action), cancellationToken));
                    }

                }
                try
                {
                    Task.WaitAll(encryptTasks.ToArray());
                }
                finally
                {
                    fileEncryption.Dispose();
                }
            }
        }
        private void PostUploadAction(Task task,string path, FileEncryption fileEncryption, AssetCreationOptions assetCreationOptions, CancellationToken token)
        {
            try
            {
                task.ThrowIfFaulted();
                token.ThrowIfCancellationRequested();

                FileInfo fileInfo = new FileInfo(path);

                //Updating Name based on file name to avoid exceptions trying to download file.Mapping to storage account is through file name
                this.Name = fileInfo.Name;

                // Set the ContentFileSize base on the local file size
                this.ContentFileSize = fileInfo.Length;

                // Update the files associated with the asset with the encryption related metadata.
                if (assetCreationOptions.HasFlag(AssetCreationOptions.StorageEncrypted))
                {
                    AssetBaseCollection.AddEncryptionMetadataToAssetFile(this, fileEncryption);
                }
                else if (assetCreationOptions.HasFlag(AssetCreationOptions.CommonEncryptionProtected))
                {
                    AssetBaseCollection.SetAssetFileForCommonEncryption(this);
                }
                else if (assetCreationOptions.HasFlag(AssetCreationOptions.EnvelopeEncryptionProtected))
                {
                    AssetBaseCollection.SetAssetFileForEnvelopeEncryption(this);
                }
                this.Update();
            }
            finally
            {
                if (fileEncryption != null)
                {
                    fileEncryption.Dispose();
                }
            }
        }
        private void Cleanup(BlobTransferClient blobTransfer, FileEncryption fileEncryption, ILocator locator, IAccessPolicy accessPolicy)
        {
            lock (this._lock)
            {
                if (blobTransfer != null)
                {
                    try
                    {
                        blobTransfer.TransferProgressChanged -= this.OnDownloadBlobTransferProgressChanged;
                    }
                    catch
                    {
                    }
                    finally
                    {
                        blobTransfer = null;
                    }
                }

                if (fileEncryption != null)
                {
                    try
                    {
                        fileEncryption.Dispose();
                    }
                    catch
                    {
                    }
                    finally
                    {
                        fileEncryption = null;
                    }
                }

                if (locator != null)
                {
                    try
                    {
                        locator.Delete();
                    }
                    catch
                    {
                    }
                    finally
                    {
                        locator = null;
                    }
                }

                if (accessPolicy != null)
                {
                    try
                    {
                        accessPolicy.Delete();
                    }
                    catch
                    {
                    }
                    finally
                    {
                        accessPolicy = null;
                    }
                }
            }
        }