public async Task Test_UploadVideo() { UploadVideoModel model = new UploadVideoModel() { CallbackUrl = this.Configuration["TestUploadVideoCallbackUrl"], Name = "Automated Test Video", SendSuccessEmail = true, VideoUrl = this.Configuration["TestUploadVideoUrl"] }; var result = await this.ServerClient .PostAsJsonAsync <UploadVideoModel>("/VideoIndexer/UploadVideo", model); if (result.IsSuccessStatusCode) { Assert.IsTrue(result.IsSuccessStatusCode, result.ReasonPhrase); } else { string errorContent = string.Empty; if (result.Content.Headers.ContentLength > 0) { errorContent = await result.Content.ReadAsStringAsync(); Assert.Fail($"Reason: {result.ReasonPhrase} - Details: {errorContent}"); } } }
public async Task <IActionResult> UploadVideo(UploadVideoModel model) { HttpResponseMessage result = await AnalyzeVideo(model); if (result.IsSuccessStatusCode) { return(Ok()); } else { string content = string.Empty; if (result.Content.Headers.ContentLength > 0) { content = await result.Content.ReadAsStringAsync(); } return(Problem(detail: content, title: result.ReasonPhrase)); } }
private async Task <HttpResponseMessage> AnalyzeVideo(UploadVideoModel model) { AzureVideoIndexerHelper helper = new AzureVideoIndexerHelper(this.AzureConfiguration, this.CreateAuthorizedHttpClient()); string accountAccesstoken = await helper.GetAccountAccessTokenString(true); string requestUrl = $"https://api.videoindexer.ai/" + $"{this.AzureConfiguration.VideoIndexerConfiguration.Location}" + $"/Accounts/{this.AzureConfiguration.VideoIndexerConfiguration.AccountId}" + $"/Videos" + $"?name={EncodeUrl(model.Name)}" + //$"[&privacy]" + //$"[&priority]" + //$"[&description]" + //$"[&partition]" + //$"[&externalId]" + //$"[&externalUrl]" + //$"[&metadata]" + //$"[&language]" + $"&videoUrl={EncodeUrl(model.VideoUrl)}" + //$"[&fileName]" + //$"[&indexingPreset]" + //$"[&streamingPreset]" + //$"[&linguisticModelId]" + //$"[&personModelId]" + //$"[&animationModelId]" + $"&sendSuccessEmail={model.SendSuccessEmail}" + //$"[&assetId]" + //$"[&brandsCategories]" + $"&accessToken={accountAccesstoken}"; if (!string.IsNullOrWhiteSpace(model.CallbackUrl)) { requestUrl += $"&callbackUrl={EncodeUrl(model.CallbackUrl)}"; } var client = this.CreateAuthorizedHttpClient(); var result = await client.PostAsync(requestUrl, null); return(result); }
public async Task UploadVideoAsync(UploadVideoModel uploadVideoModel) { var authorizedHttpClient = this.HttpClientService.CreateAuthorizedClient(); authorizedHttpClient.Timeout = TimeSpan.FromMinutes(15); var response = await authorizedHttpClient.PostAsJsonAsync(ApiRoutes.VideoController.UploadVideo, uploadVideoModel); if (!response.IsSuccessStatusCode) { ProblemHttpResponse problemHttpResponse = await response.Content.ReadFromJsonAsync <ProblemHttpResponse>(); if (problemHttpResponse != null) { throw new CustomValidationException(problemHttpResponse.Detail); } else { throw new CustomValidationException(response.ReasonPhrase); } } }
[RequestSizeLimit(1073741824)] //1GB public async Task <IActionResult> UploadVideo(UploadVideoModel uploadVideoModel, CancellationToken cancellationToken) { if (uploadVideoModel.UseSourceUrl && String.IsNullOrWhiteSpace(uploadVideoModel.SourceUrl)) { throw new CustomValidationException("You muse specify a Source Url"); } if (!uploadVideoModel.UseSourceUrl && String.IsNullOrWhiteSpace(uploadVideoModel.StoredFileName)) { throw new CustomValidationException("You muse upload a file"); } if (await this.VideoService.HasReachedWeeklyVideoUploadLimitAsync(cancellationToken: cancellationToken)) { throw new CustomValidationException("You are not allowed to upload videos. You have reached your subscription's weekly limit"); } if (await this.VideoService.UploadVideoAsync(uploadVideoModel, cancellationToken: cancellationToken)) { return(Ok()); } else { throw new CustomValidationException("An error occurred trying to upload your video"); } }
public async Task <bool> UploadVideoAsync(UploadVideoModel uploadVideoModel, CancellationToken cancellationToken) { var fileExtension = string.Empty; var userAzueAdB2cObjectId = this.CurrentUserProvider.GetObjectId(); MemoryStream stream = null; if (!uploadVideoModel.UseSourceUrl) { string fileRelativePath = $"User/{userAzueAdB2cObjectId}/{uploadVideoModel.StoredFileName}"; stream = new MemoryStream(); var response = await this.AzureBlobStorageService .GetFileStreamAsync(this.DataStorageConfiguration.UntrustedUploadsContainerName, fileRelativePath, stream, cancellationToken); fileExtension = Path.GetExtension(uploadVideoModel.StoredFileName); } else { fileExtension = Path.GetExtension(uploadVideoModel.SourceUrl); if (String.IsNullOrWhiteSpace(fileExtension)) { throw new CustomValidationException("Please make sure the source file has a valid extension like .mp4"); } } var existentVideoName = await this.FairplaytubeDatabaseContext.VideoInfo .SingleOrDefaultAsync(p => p.AccountId.ToString() == this.AzureVideoIndexerConfiguration.AccountId && p.Location == this.AzureVideoIndexerConfiguration.Location && p.Name == uploadVideoModel.Name, cancellationToken : cancellationToken); if (existentVideoName != null) { throw new CustomValidationException($"Unable to use the Name: {uploadVideoModel.Name}. Please use another name and try again"); } if (uploadVideoModel.UseSourceUrl) { byte[] fileBytes = null; if (!String.IsNullOrWhiteSpace(uploadVideoModel.SourceUrl)) { fileBytes = await this.CustomHttpClient .GetByteArrayAsync(uploadVideoModel.SourceUrl, cancellationToken); } stream = new MemoryStream(fileBytes); } cancellationToken.ThrowIfCancellationRequested(); stream.Position = 0; cancellationToken.ThrowIfCancellationRequested(); var newFileName = $"{uploadVideoModel.Name}{fileExtension}"; await this.AzureBlobStorageService.UploadFileAsync(this.DataStorageConfiguration.ContainerName, $"{userAzueAdB2cObjectId}/{newFileName}", stream, overwrite : false, cancellationToken : cancellationToken); string fileUrl = $"https://{this.DataStorageConfiguration.AccountName}.blob.core.windows.net" + $"/{this.DataStorageConfiguration.ContainerName}/{userAzueAdB2cObjectId}/{newFileName}"; cancellationToken.ThrowIfCancellationRequested(); var user = await this.FairplaytubeDatabaseContext.ApplicationUser .SingleAsync(p => p.AzureAdB2cobjectId.ToString() == userAzueAdB2cObjectId, cancellationToken : cancellationToken); cancellationToken.ThrowIfCancellationRequested(); await this.FairplaytubeDatabaseContext.VideoInfo.AddAsync(new DataAccess.Models.VideoInfo() { ApplicationUserId = user.ApplicationUserId, Description = uploadVideoModel.Description, Location = this.AzureVideoIndexerConfiguration.Location, Name = uploadVideoModel.Name, Price = uploadVideoModel.Price, //VideoId = indexVideoResponse.id, VideoBloblUrl = fileUrl, //IndexedVideoUrl = $"https://www.videoindexer.ai/embed/player/{this.AzureVideoIndexerConfiguration.AccountId}" + //$"/{indexVideoResponse.id}/" + //$"?&locale=en&location={this.AzureVideoIndexerConfiguration.Location}", AccountId = Guid.Parse(this.AzureVideoIndexerConfiguration.AccountId), FileName = newFileName, VideoIndexStatusId = (short)Common.Global.Enums.VideoIndexStatus.Pending, VideoLanguageCode = uploadVideoModel.Language, VideoVisibilityId = (short)uploadVideoModel.VideoVisibility }, cancellationToken : cancellationToken); cancellationToken.ThrowIfCancellationRequested(); await this.FairplaytubeDatabaseContext.SaveChangesAsync(cancellationToken : cancellationToken); return(true); }