public static async Task <NewRequest <SmartDoc> > CreateNewRequest(
            string ownerId,
            bool isAsync,
            IFormFile doc,
            ClassificationType docType,
            IStorageRepository storageRepository,
            IDocumentDBRepository <SmartDoc> docRepository,
            IDocumentDBRepository <User> userRepository)
        {
            if (doc == null || doc.Length == 0)
            {
                throw new InvalidOperationException("No file was selected");
            }

            var owner = await userRepository.GetItemAsync(ownerId);

            if (owner == null)
            {
                throw new InvalidOperationException("Invalid request");
            }

            long size = doc.Length;

            // To hold the Url for the uploaded document
            string docName = "NA";
            string docUri  = null;

            //Upload the submitted document to Azure Storage
            if (size > 0)
            {
                using (var stream = doc.OpenReadStream())
                {
                    var docExtention = doc.FileName.Substring(doc.FileName.LastIndexOf('.'));
                    var docId        = Guid.NewGuid();
                    docName = $"{docType}-{docId}{docExtention}";
                    docUri  = await storageRepository.CreateFile(docName, stream);
                }
            }


            // process uploaded files by creating a new SmartDoc record with the details and save it to CosmosDB
            var newDoc = new SmartDoc
            {
                Id        = Guid.NewGuid().ToString(),
                OwnerId   = ownerId,
                DocType   = docType,
                CreatedAt = DateTime.UtcNow,
                IsDeleted = false,
                DocName   = docName,
                DocUrl    = docUri,
                Status    = SmartDocStatus.Created.ToString(),
                Origin    = docRepository.Origin
            };

            await docRepository.CreateItemAsync(newDoc);

            //Prepare the new request that will hold the document along with the processing instructions by the Cognitive Pipeline
            var newReq = new NewRequest <SmartDoc>
            {
                CreatedAt       = DateTime.UtcNow,
                Id              = Guid.NewGuid().ToString(),
                OwnerId         = ownerId,
                IsDeleted       = false,
                Instructions    = DocumentInstructionsProcessor.GetInstructions(docType),
                ItemReferenceId = newDoc.Id,
                RequestItem     = newDoc,
                Status          = SmartDocStatus.Created.ToString(),
                Origin          = docRepository.Origin,
                IsAsync         = isAsync
            };

            return(newReq);
        }
Beispiel #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));
        }