public async Task InitalizeAsync( string appApiLicenseCodeKey, string checkoutSessionLicenseCode, Uri sourceServiceUri, Guid sourceActorId, string consumerLicenseCode, EntityDownload entityDownload) { if (!await StateManager.ContainsStateAsync(ConsumerRefIdKey)) { await StateManager.SetStateAsync(AppApiLicenseCodeKey, appApiLicenseCodeKey); await StateManager.SetStateAsync(CheckoutSessionLicenseCodeKey, checkoutSessionLicenseCode); await StateManager.SetStateAsync(ConsumerLicenseCodeKey, consumerLicenseCode); await StateManager.SetStateAsync(SourceServiceUriKey, sourceServiceUri); await StateManager.SetStateAsync(ConsumerRefIdKey, sourceActorId); await StateManager.SetStateAsync(EntityDownloadKey, entityDownload); } await RegisterReminderAsync(ReminderKey, null, TimeSpan.FromMilliseconds(1), TimeSpan.FromSeconds(5)); }
private async Task <EntitySecret> RetrieveEntityMediaAsync() { EntityDownload entityDownload = await StateManager.GetStateAsync <EntityDownload>(EntityDownloadKey); WriteTimedDebug($"Begin retrieve {entityDownload.MediaEntityType} {entityDownload.MediaSize}\n{entityDownload.SasReadUri}"); long chunkSize = entityDownload.MediaSize / 100; chunkSize = chunkSize > 5000 ? chunkSize : 5000; long read; string mediaType = null; using (HttpClient client = new HttpClient()) using (MemoryStream ms = new MemoryStream()) { long offset = 0; do { HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, new Uri(entityDownload.SasReadUri)); req.Headers.Range = new RangeHeaderValue(offset, offset + chunkSize - 1); HttpResponseMessage message = await client.SendAsync(req).ConfigureAwait(false); if (!message.IsSuccessStatusCode) { // If we've reached forbidden the token has expired. If not found, we may have already downloaded it, but were demoted. if (message.StatusCode == System.Net.HttpStatusCode.Forbidden || message.StatusCode == System.Net.HttpStatusCode.NotFound) { return(null); } throw new Exception("Error downloading media"); } byte[] buffer = await message.Content.ReadAsByteArrayAsync().ConfigureAwait(false); ms.Write(buffer, 0, buffer.Length); read = message.Content.Headers.ContentLength.Value; offset += read; mediaType = message.Content.Headers.ContentType.MediaType; }while (offset < entityDownload.MediaSize); return(await ExtractTicketSecrets(entityDownload, mediaType, ms)); } }
private async Task <EntitySecret> ExtractTicketSecrets(EntityDownload entityDownload, string mediaType, MemoryStream ticketStream) { string ticketLicenseCode = null; byte[] ticketBytes = ticketStream.ToArray(); string encodedHash = CryptographyHelper.HashSha256(ticketBytes); if (mediaType == "video/mp4") { ticketStream.Position = 0; ticketLicenseCode = await TryParseMp4(ticketStream); if (ticketLicenseCode != null) { return(new EntitySecret { MediaEntityType = entityDownload.MediaEntityType, Hash = encodedHash, ValidationLicenseCode = entityDownload.ValidationLicenseCode }); } } else { string qrCode = QrCodeHelper.ParseImage(ticketBytes); ticketLicenseCode = await ExtracTicketLicenseCodeViaTagsAsync(ticketStream).ConfigureAwait(false); if (ticketLicenseCode != qrCode) { WriteTimedDebug("Tag mismatch"); } } if (ticketLicenseCode == null) { return(null); } else { return(new EntitySecret { MediaEntityType = entityDownload.MediaEntityType, Hash = encodedHash, ValidationLicenseCode = entityDownload.ValidationLicenseCode }); } }
private async Task OnCheckStatusAsync() { try { Uri requestUri = GetFullUri("api/v2/shopping/checkout/status"); HttpRequestMessage httpreq = new HttpRequestMessage(HttpMethod.Get, requestUri); string appApiLicenseCode = await StateManager.GetStateAsync <string>(AppApiLicenseCodeKey).ConfigureAwait(false); string consumerLicenseCode = await StateManager.GetStateAsync <string>(ConsumerLicenseCodeKey).ConfigureAwait(false); string checkoutSessionLicenseCode = await StateManager.GetStateAsync <string>(CheckoutSessionLicenseCodeKey).ConfigureAwait(false); httpreq.Headers.Add("lazlo-consumerlicensecode", consumerLicenseCode); httpreq.Headers.Add("lazlo-apilicensecode", appApiLicenseCode); httpreq.Headers.Add("lazlo-txlicensecode", checkoutSessionLicenseCode); var message = await _HttpClient.SendAsync(httpreq); string responseJson = await message.Content.ReadAsStringAsync().ConfigureAwait(false); var statusResponse = JsonConvert.DeserializeObject <SmartResponse <CheckoutStatusResponse> >(responseJson); if (message.IsSuccessStatusCode) { if (statusResponse.Data.GiftCardStatusCount == statusResponse.Data.GiftCardStatuses.Count(z => z.SasUri != null)) { List <EntityDownload> downloads = new List <EntityDownload>(); foreach (var item in statusResponse.Data.GiftCardStatuses) { EntityDownload pendingDownload = new EntityDownload { MediaEntityType = Lazlo.Common.Enumerators.MediaEntityType.GiftCard, MediaSize = item.MediaSize, SasReadUri = item.SasUri, ValidationLicenseCode = item.ValidationLicenseCode }; downloads.Add(pendingDownload); ActorId downloadActorId = new ActorId(pendingDownload.ValidationLicenseCode); IConsumerEntityDownloadActor downloadActor = ActorProxy.Create <IConsumerEntityDownloadActor>(downloadActorId, EntityDownloadServiceUri); await downloadActor.InitalizeAsync(appApiLicenseCode, checkoutSessionLicenseCode, ServiceUri, Id.GetGuidId(), consumerLicenseCode, pendingDownload); } await StateManager.SetStateAsync(PendingGiftCardsKey, downloads); await _StateMachine.FireAsync(ConsumerSimulationExchangeAction.WaitForGiftCardsToDownload); } else { await _StateMachine.FireAsync(ConsumerSimulationExchangeAction.WaitForGiftCardsToRender); } } else { if (message.StatusCode == System.Net.HttpStatusCode.Processing) { await _StateMachine.FireAsync(ConsumerSimulationExchangeAction.WaitForGiftCardsToRender); } else { throw new Exception(statusResponse.Error.Message); } } } catch (Exception ex) { WriteTimedDebug(ex); await _StateMachine.FireAsync(ConsumerSimulationExchangeAction.WaitForGiftCardsToRender); } }