internal static async Task <IAssetFile> CreateAssetFileFromLocalFileAsync(this IAsset asset, string filePath, ILocator sasLocator, EventHandler <UploadProgressChangedEventArgs> uploadProgressChangedEventArgs, CancellationToken cancellationToken) { string assetFileName = Path.GetFileName(filePath); IAssetFile assetFile = await asset.AssetFiles.CreateAsync(assetFileName, cancellationToken).ConfigureAwait(false); MediaContextBase context = asset.GetMediaContext(); assetFile.UploadProgressChanged += uploadProgressChangedEventArgs; BlobTransferClient blobTransferClient = new BlobTransferClient { NumberOfConcurrentTransfers = context.NumberOfConcurrentTransfers, ParallelTransferThreadCount = context.ParallelTransferThreadCount }; await assetFile.UploadAsync(filePath, blobTransferClient, sasLocator, cancellationToken).ConfigureAwait(false); assetFile.UploadProgressChanged -= uploadProgressChangedEventArgs; if (assetFileName.EndsWith(ILocatorExtensions.ManifestFileExtension, StringComparison.OrdinalIgnoreCase)) { assetFile.IsPrimary = true; await assetFile.UpdateAsync().ConfigureAwait(false); } return(assetFile); }
private async Task <WMAssetOutputMessage> AddRenderToAsset(CloudBlockBlob sourceBlob, CloudBlockBlob destBlob, IAssetFile renderFile, string WMEmbedCode, string WatermarkedAssetId) { WMAssetOutputMessage result = new WMAssetOutputMessage() { MMRKURLAdded = sourceBlob.Uri.AbsoluteUri, EmbedCode = WMEmbedCode, WMAssetId = WatermarkedAssetId }; try { string name = HttpUtility.UrlDecode(HttpUtility.UrlDecode(Path.GetFileName(sourceBlob.Uri.AbsolutePath))); string copyId = await destBlob.StartCopyAsync(sourceBlob); await sourceBlob.FetchAttributesAsync(); renderFile.ContentFileSize = sourceBlob.Properties.Length; await renderFile.UpdateAsync(); result.Status = "MMRK File Added"; result.StatusMessage = destBlob.Name + " added to watermarked asset"; } catch (Exception X) { result.Status = $"Copy error"; //Add Blob Info to the error result.StatusMessage = $"{sourceBlob.Uri.AbsoluteUri} Error {X.Message}"; Trace.TraceError(result.StatusMessage); } return(result); }
private static async Task <IAssetFile> CreateCopyAsync(this AssetFileBaseCollection assetFiles, IAssetFile assetFile, CancellationToken cancellationToken) { IAssetFile assetFileCopy = await assetFiles.CreateAsync(assetFile.Name, cancellationToken).ConfigureAwait(false); assetFileCopy.IsPrimary = assetFile.IsPrimary; assetFileCopy.ContentFileSize = assetFile.ContentFileSize; assetFileCopy.MimeType = assetFile.MimeType; await assetFileCopy.UpdateAsync().ConfigureAwait(false); return(assetFileCopy); }
// ReSharper disable ReplaceWithSingleCallToFirstOrDefault public async Task Handle(MarkUploadComplete markComplete) { // Find the details for the upload destination based on the Upload Url PreparedStatement getPrepared = await _statementCache.NoContext.GetOrAddAsync("SELECT * FROM uploaded_video_destinations WHERE upload_url = ?"); RowSet rows = await _session.ExecuteAsync(getPrepared.Bind(markComplete.UploadUrl)).ConfigureAwait(false); Row uploadDestination = rows.SingleOrDefault(); if (uploadDestination == null) { throw new InvalidOperationException(string.Format("Could not find upload destination details for URL {0}", markComplete.UploadUrl)); } var assetId = uploadDestination.GetValue <string>("assetid"); var filename = uploadDestination.GetValue <string>("filename"); var locatorId = uploadDestination.GetValue <string>("locatorid"); // Find the asset to be published IAsset asset = _cloudMediaContext.Assets.Where(a => a.Id == assetId).FirstOrDefault(); if (asset == null) { throw new InvalidOperationException(string.Format("Could not find asset {0}.", assetId)); } // Set the file as the primary asset file IAssetFile assetFile = asset.AssetFiles.Where(f => f.Name == filename).FirstOrDefault(); if (assetFile == null) { throw new InvalidOperationException(string.Format("Could not find file {0} on asset {1}.", filename, assetId)); } assetFile.IsPrimary = true; await assetFile.UpdateAsync().ConfigureAwait(false); // Remove the upload locator (i.e. revoke upload access) ILocator uploadLocator = asset.Locators.Where(l => l.Id == locatorId).FirstOrDefault(); if (uploadLocator != null) { await uploadLocator.DeleteAsync().ConfigureAwait(false); } // Tell the world an upload finished await _bus.Publish(new UploadCompleted { AssetId = assetId, Filename = filename }).ConfigureAwait(false); }
/// <summary> /// Returns a <see cref="System.Threading.Tasks.Task<IAsset>"/> instance for a new <see cref="IAsset"/> with the file in <paramref name="sourceBlob"/>. /// </summary> /// <param name="assets">The <see cref="AssetBaseCollection"/> instance.</param> /// <param name="sourceBlob">The <see cref="Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob"/> instance that contains the file to copy.</param> /// <param name="storageCredentials">The <see cref="Microsoft.WindowsAzure.Storage.Auth.StorageCredentials"/> instance for the new asset to create.</param> /// <param name="options">The <see cref="AssetCreationOptions"/>.</param> /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> instance used for cancellation.</param> /// <returns>A <see cref="System.Threading.Tasks.Task<IAsset>"/> instance for a new <see cref="IAsset"/> with the file in <paramref name="sourceBlob"/>.</returns> public static async Task <IAsset> CreateFromBlobAsync(this AssetBaseCollection assets, CloudBlockBlob sourceBlob, StorageCredentials storageCredentials, AssetCreationOptions options, CancellationToken cancellationToken) { if (assets == null) { throw new ArgumentNullException("assets", "The assets collection cannot be null."); } if (sourceBlob == null) { throw new ArgumentNullException("sourceBlob", "The source blob cannot be null."); } if (storageCredentials == null) { throw new ArgumentNullException("storageCredentials", "The destination storage credentials cannot be null."); } if (storageCredentials.IsAnonymous || storageCredentials.IsSAS) { throw new ArgumentException("The destination storage credentials must contain the account key credentials.", "destinationStorageCredentials"); } MediaContextBase context = assets.MediaContext; IAsset asset = await assets.CreateAsync(sourceBlob.Name, storageCredentials.AccountName, options, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); IRetryPolicy retryPolicy = context.MediaServicesClassFactory.GetBlobStorageClientRetryPolicy().AsAzureStorageClientRetryPolicy(); BlobRequestOptions blobOptions = new BlobRequestOptions { RetryPolicy = retryPolicy }; CloudBlobContainer container = new CloudBlobContainer(asset.Uri, storageCredentials); CloudBlockBlob blob = container.GetBlockBlobReference(sourceBlob.Name); await CopyBlobHelpers.CopyBlobAsync(sourceBlob, blob, blobOptions, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); IAssetFile assetFile = await asset.AssetFiles.CreateAsync(sourceBlob.Name, cancellationToken).ConfigureAwait(false); assetFile.IsPrimary = true; if (sourceBlob.Properties != null) { assetFile.ContentFileSize = sourceBlob.Properties.Length; assetFile.MimeType = sourceBlob.Properties.ContentType; } await assetFile.UpdateAsync().ConfigureAwait(false); return(asset); }
private static void CallUpdateUploadDownloadAndDelete(IAssetFile file, string name) { file.Update(); file.UpdateAsync(); var uploadFile = Path.Combine(Path.GetTempPath(), name); try { File.CreateText(uploadFile).Close(); file.Upload(uploadFile); } finally { File.Delete(uploadFile); } file.Download(Path.GetTempFileName()); file.Delete(); }