public async Task <IActionResult> SubmitDoc(string ownerId, [FromForm] IFormFile doc, [FromHeader] bool isAsync = false, [FromHeader] bool isMinimum = true)
        {
            NewRequest <SmartDoc> newReq = null;
            string result = null;

            try
            {
                newReq = await ClassificationRequestHelper.CreateNewRequest(
                    ownerId, isAsync, doc, docType, storageRepository, docRepository, userRepository);

                result = await newReqService.SendNewRequest(newReq, newReq.RequestItem.Id, isAsync);
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(ex.Message));
            }

            //Reduce the size of the payload if isMinimum = true
            if (isMinimum)
            {
                var output = JsonConvert.DeserializeObject <EmployeeId>(result);
                output.OptimizeSmartDoc(isMinimum);
                result = JsonConvert.SerializeObject(output);
            }

            return(Ok(result));
        }
예제 #2
0
        public async Task <IActionResult> SubmitDoc(string ownerId, string docType, bool isAsync, IFormFile doc)
        {
            // TODO: Introduce another parameter to identify the needed classification services (like classification, OCR,...)

            if (doc == null || doc.Length == 0)
            {
                return(BadRequest("file not selected"));
            }

            var owner = await userRepository.GetItemAsync(ownerId);

            if (owner == null)
            {
                return(BadRequest("Invalid request"));
            }

            var proposedDocType = ClassificationType.Unidentified;
            var isValidType     = Enum.TryParse <ClassificationType>(docType, out proposedDocType);

            if (!isValidType)
            {
                return(BadRequest("Invalid document type"));
            }

            long size = doc.Length;

            // full path to file in temp location
            string docName = "NA";
            string docUri  = null;

            if (size > 0)
            {
                using (var stream = doc.OpenReadStream())
                {
                    var docExtention = doc.FileName.Substring(doc.FileName.LastIndexOf('.'));
                    var docId        = Guid.NewGuid();
                    docName = $"{proposedDocType}-{docId}{docExtention}";
                    docUri  = await storageRepository.CreateFile(docName, stream);
                }

                //Sample to upload to local temp storage
                //var tempFolder = Path.GetTempPath();
                //var fileName = Path.GetRandomFileName();
                //var filePath = Path.Combine(tempFolder, fileName);
                //using (var stream = new FileStream(filePath, FileMode.Create))
                //{
                //    await doc.CopyToAsync(stream);
                //}
            }


            // process uploaded files
            // Don't rely on or trust the FileName property without validation.
            var newDoc = new SmartDoc
            {
                Id        = Guid.NewGuid().ToString(),
                OwnerId   = ownerId,
                DocType   = proposedDocType,
                CreatedAt = DateTime.UtcNow,
                IsDeleted = false,
                DocName   = docName,
                DocUrl    = docUri,
                Status    = SmartDocStatus.Created.ToString(),
                Origin    = docRepository.Origin
            };

            await docRepository.CreateItemAsync(newDoc);

            //Call NewReq function background service to start processing the new doc
            var newReq = new NewRequest <SmartDoc>
            {
                CreatedAt       = DateTime.UtcNow,
                Id              = Guid.NewGuid().ToString(),
                OwnerId         = ownerId,
                IsDeleted       = false,
                Instructions    = DocumentInstructionsProcessor.GetInstructions(proposedDocType),
                ItemReferenceId = newDoc.Id,
                RequestItem     = newDoc,
                Status          = SmartDocStatus.Created.ToString(),
                Origin          = docRepository.Origin,
                IsAsync         = isAsync
            };

            var result = await newReqService.SendNewRequest(newReq, newDoc.Id, isAsync);

            return(Ok(result));
        }