Exemplo n.º 1
0
        public async Task Process(NewItemRequest newItem)
        {
            try
            {
                var documentResult = new DocumentResultResponse();
                var filePath       = _fileManager.Download(newItem.DocumentUrl);
                var numberOfPages  = _fileManager.GetNumberOfPages(filePath);

                for (var page = 1; page <= numberOfPages; page++)
                {
                    var pagePath = _fileManager.GeneratePage(filePath, page);
                    var text     = _pdfToText.Get(pagePath);
                    if (text == null)
                    {
                        var imagePath = _fileManager.GeneratePageInImage(filePath, page);
                        text = await _cognitive.Get(imagePath).ConfigureAwait(false);

                        _fileManager.Delete(imagePath);
                    }
                    documentResult.AddProcessedPage(text, page);
                    _fileManager.Delete(pagePath);
                }

                _fileManager.Delete(filePath);
                documentResult.Success = true;
                _backgroundJobs.Enqueue(() => _callback.Send(documentResult, newItem));
            }
            catch (Exception ex)
            {
                var documentResult = new DocumentResultResponse {
                    Success = false, ErrorMessage = ex.Message
                };
                _backgroundJobs.Enqueue(() => _callback.Send(documentResult, newItem));
            }
        }
        public async Task <ActionResult <ItemResponse> > SaveItem(long id, [FromBody] NewItemRequest request)
        {
            var category = await cache.TryGet(id);

            if (category == null)
            {
                category = await categoryRepository.FindById(id).ConfigureAwait(false);
            }

            if (category == null)
            {
                return(NotFound());
            }

            var newItem = request.ToNew(category);

            var savedItem = await categoryRepository.InsertItem(category.Id, newItem);

            if (savedItem != null)
            {
                await cache.Invalidate(id);

                return(Created(nameof(SaveItem), ItemResponse.Of(savedItem)));
            }
            else
            {
                return(Conflict());
            }
        }
Exemplo n.º 3
0
        public IActionResult Post(NewItemRequest dto)
        {
            _logger.LogInformation("New request");

            _backgroundJobs.Enqueue(() => _processDocument.Process(dto));

            return(Ok());
        }
Exemplo n.º 4
0
        private void AddItem()
        {
            ItemConfirmation y = new ItemConfirmation();

            NewItemRequest.Raise(y, x =>
            {
                if (x.Confirmed)
                {
                    _itemService.Add(x.Item);
                    OnItemAdded(x.Item);
                }
            });
        }
Exemplo n.º 5
0
        public async Task Send(DocumentResultResponse documentResult, NewItemRequest newItem)
        {
            using var client = new HttpClient();
            var jsonContent   = JsonConvert.SerializeObject(documentResult);
            var contentString = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            contentString.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var response = await client.PutAsync(newItem.CallbackUrl, contentString).ConfigureAwait(false);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception("Error to callback");
            }
        }