示例#1
0
        public async Task <GetDocumentStreamQueryResult> RetrieveAsync(GetDocumentStreamQuery query, CancellationToken cancellationToken = default)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                _logger.LogInformation($"{nameof(GetDocumentStreamQueryHandler)}.{nameof(RetrieveAsync)} was cancelled before execution");
                cancellationToken.ThrowIfCancellationRequested();
            }

            using (var context = _projectionDbContextFactory.Create())
            {
                var document = await context.Set <Document>().FindAsync(new object[] { query.Subject }, cancellationToken);

                if (document is null)
                {
                    throw new ArgumentNullException(nameof(document));
                }

                if (!_contentTypeProvider.TryGetContentType(document.FileName, out var contentType))
                {
                    contentType = "application/octet-stream";
                }


                var result = new GetDocumentStreamQueryResult(new MemoryStream(), contentType, document.FileName);

                await _fileClient.DownloadAsync($"{query.Subject}{Path.GetExtension(document.FileName)}", query.Actor, result.Stream, cancellationToken);

                result.Stream.Position = 0;
                return(result);
            }
        }
示例#2
0
        public async Task <FileResult> DownloadAsync(string fileName, string userId)
        {
            lock (_downloadLockObj)
            {
                if (_downloadTask == null)
                {
                    _downloadTask = Task.Run(async() => await _fileClient.DownloadAsync(fileName, userId), _cts.Token);
                }
            }

            return(await _downloadTask);
        }
示例#3
0
        private async Task QueueText(string text, int index, AWSSpeechRequest request, AWSSpeechResult result)
        {
            var synthesizeSpeechRequest = new StartSpeechSynthesisTaskRequest
            {
                OutputFormat       = request.OutputFormat,
                VoiceId            = request.VoiceId,
                Text               = text,
                OutputS3BucketName = _translationOptions.Bucket,
                OutputS3KeyPrefix  = Guid.NewGuid().ToString()
            };

            var response = await _pollyClient.StartSpeechSynthesisTaskAsync(synthesizeSpeechRequest);

            using (var waitHandle = new AutoResetEvent(false))
                using (new Timer(
                           callback: async(e) => { await CheckSynthesisTask(response.SynthesisTask.TaskId, waitHandle); },
                           state: null,
                           dueTime: TimeSpan.Zero,
                           period: TimeSpan.FromSeconds(_translationOptions.PollRate)
                           )
                       )
                {
                    waitHandle.WaitOne();
                }

            var task = await _pollyClient.GetSpeechSynthesisTaskAsync(new GetSpeechSynthesisTaskRequest { TaskId = response.SynthesisTask.TaskId });

            if (task.SynthesisTask.TaskStatus == Amazon.Polly.TaskStatus.Failed)
            {
                throw new InvalidOperationException();
            }

            var key        = task.SynthesisTask.OutputUri.Split('/').Last();
            var fileResult = await _fileClient.DownloadAsync(key, "");

            using (var stream = await fileResult.OpenStreamAsync())
            {
                await result.DigestBytes(index, stream);
            }

            if (request.CallBack != null)
            {
                await request.CallBack(text.Length);
            }

            await _fileClient.DeleteAsync(key, "");
        }
        public async Task StartAsync(TranslationRequest request)
        {
            int version = request.Version + 1;

            var fileResult = await _fileClient.DownloadAsync(
                fileName : $"{request.CorrelationId}{Path.GetExtension(request.FileName)}",
                userId : request.UserId
                );

            string text;

            using (var fileStream = await fileResult.OpenStreamAsync())
                using (var textExtractor = TextExtractor.Open(fileStream, fileResult.ContentType))
                {
                    text = await textExtractor.ExtractAsync();
                }

            var textChunks = text.ChunkWithDelimeters(5000, '.', '!', '?', ')', '"', '}', ']').ToArray();

            await _eventPublisher.PublishAsync(new TranslationStarted(
                                                   aggregateId : request.AggregateId,
                                                   version : version,
                                                   correlationId : request.CorrelationId,
                                                   causationId : request.CausationId,
                                                   characterCount : textChunks.Sum(tc => tc.Length),
                                                   userId : request.UserId
                                                   ));

            try
            {
                for (int i = 0; i < textChunks.Count(); i++)
                {
                    version++;
                    await _eventPublisher.PublishAsync(new TranslationCharactersProcessed(
                                                           aggregateId : request.AggregateId,
                                                           version : version,
                                                           correlationId : request.CorrelationId,
                                                           causationId : request.CausationId,
                                                           charactersProcessed : textChunks[i].Length,
                                                           userId : request.UserId
                                                           ));
                }

                using (var stream = new MemoryStream())
                    using (TextWriter tw = new StreamWriter(stream))
                    {
                        await tw.WriteAsync("test");

                        await tw.FlushAsync();

                        stream.Position = 0;
                        await _fileClient.UploadAsync($"{request.AggregateId}.mp3", request.UserId, stream);

                        await _eventPublisher.PublishAsync(new TranslationSucceded(
                                                               aggregateId : request.AggregateId,
                                                               version : ++version,
                                                               correlationId : request.CorrelationId,
                                                               causationId : request.CausationId,
                                                               userId : request.UserId
                                                               ));
                    }
            }
            catch (Exception e)
            {
                await _eventPublisher.PublishAsync(new TranslationFailed(
                                                       aggregateId : request.AggregateId,
                                                       version : ++version,
                                                       correlationId : request.CorrelationId,
                                                       causationId : request.CausationId,
                                                       error : e.Message,
                                                       userId : request.UserId
                                                       ));
            }
        }
        public async Task StartAsync(TranslationRequest request)
        {
            int version = request.Version + 1;

            var fileResult = await _fileClient.DownloadAsync(
                fileName : $"{request.CorrelationId}{Path.GetExtension(request.FileName)}",
                userId : request.UserId
                );

            string text;

            using (var fileStream = await fileResult.OpenStreamAsync())
                using (var textExtractor = TextExtractor.Open(fileStream, fileResult.ContentType))
                {
                    text = await textExtractor.ExtractAsync();
                }

            var textChunks = text.ChunkWithDelimeters(5000, '.', '!', '?', ')', '"', '}', ']');

            await _eventPublisher.PublishAsync(new TranslationStarted(
                                                   aggregateId : request.AggregateId,
                                                   version : version,
                                                   correlationId : request.CorrelationId,
                                                   causationId : request.CausationId,
                                                   characterCount : textChunks.Sum(tc => tc.Length),
                                                   userId : request.UserId
                                                   ));

            try
            {
                var result = await _speechSynthesizer.TranslateTextAsync(new GoogleSpeechRequest(
                                                                             voiceSelection : new VoiceSelectionParams
                {
                    Name = request.TranslationSubject
                },
                                                                             audioConfig : new AudioConfig
                {
                    AudioEncoding = AudioEncoding.Mp3
                },
                                                                             content : textChunks,
                                                                             callback : async length =>
                {
                    Interlocked.Increment(ref version);
                    await _semaphoreSlim.WaitAsync();

                    try
                    {
                        await _eventPublisher.PublishAsync(new TranslationCharactersProcessed(
                                                               aggregateId: request.AggregateId,
                                                               version: version,
                                                               correlationId: request.CorrelationId,
                                                               causationId: request.CausationId,
                                                               charactersProcessed: length,
                                                               userId: request.UserId
                                                               ));
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        _semaphoreSlim.Release();
                    }
                }
                                                                             ));


                using (var stream = await result.OpenStreamAsync())
                {
                    await _fileClient.UploadAsync($"{request.AggregateId}.mp3", request.UserId, stream);

                    await _eventPublisher.PublishAsync(new TranslationSucceded(
                                                           aggregateId : request.AggregateId,
                                                           version : ++version,
                                                           correlationId : request.CorrelationId,
                                                           causationId : request.CausationId,
                                                           userId : request.UserId
                                                           ));
                }
            }
            catch (Exception e)
            {
                await _eventPublisher.PublishAsync(new TranslationFailed(
                                                       aggregateId : request.AggregateId,
                                                       version : ++version,
                                                       correlationId : request.CorrelationId,
                                                       causationId : request.CausationId,
                                                       error : e.Message,
                                                       userId : request.UserId
                                                       ));
            }
        }
示例#6
0
        public async Task StartAsync(TranslationRequest request)
        {
            int version = request.Version + 1;

            var fileResult = await _fileClient.DownloadAsync(
                fileName : $"{request.CorrelationId.ToString()}{Path.GetExtension(request.FileName)}",
                userId : request.UserId
                );

            string text;

            using (var fileStream = await fileResult.OpenStreamAsync())
                using (var textExtractor = TextExtractor.Open(fileStream, fileResult.ContentType))
                {
                    text = await textExtractor.ExtractAsync();
                }

            var textChunks = text.ChunkWithDelimeters(100000, '.', '!', '?', ')', '"', '}', ']');

            await _eventPublisher.PublishAsync(new TranslationStarted(
                                                   aggregateId : request.AggregateId,
                                                   version : version,
                                                   correlationId : request.CorrelationId,
                                                   causationId : request.CausationId,
                                                   characterCount : text.Length,
                                                   userId : request.UserId
                                                   ));

            try
            {
                var result = await _speechSynthesizer.TranslateTextAsync(new AWSSpeechRequest {
                    OutputFormat = "",
                    Content      = textChunks,
                    VoiceId      = request.TranslationSubject,
                    CallBack     = async length =>
                    {
                        Interlocked.Increment(ref version);
                        await _semaphoreSlim.WaitAsync();

                        try
                        {
                            await _eventPublisher.PublishAsync(new TranslationCharactersProcessed(
                                                                   aggregateId: request.AggregateId,
                                                                   version: version,
                                                                   correlationId: request.CorrelationId,
                                                                   causationId: request.CausationId,
                                                                   charactersProcessed: length,
                                                                   userId: request.UserId
                                                                   ));
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                        finally
                        {
                            _semaphoreSlim.Release();
                        }
                    }
                });;


                using (var stream = await result.OpenStreamAsync())
                {
                    await _fileClient.UploadAsync($"{request.AggregateId}.mp3", request.UserId, stream);

                    await _eventPublisher.PublishAsync(new TranslationSucceded(
                                                           aggregateId : request.AggregateId,
                                                           version : version + 1,
                                                           correlationId : request.CorrelationId,
                                                           causationId : request.CausationId,
                                                           userId : request.UserId
                                                           ));
                }
            }
            catch (Exception e)
            {
                await _eventPublisher.PublishAsync(new TranslationFailed(
                                                       aggregateId : request.AggregateId,
                                                       version : version + 1,
                                                       correlationId : request.CorrelationId,
                                                       causationId : request.CausationId,
                                                       error : e.Message,
                                                       userId : request.UserId
                                                       ));
            }
        }