public static void OptimizeSmartDoc(this SmartDoc doc, bool isMinimum)
        {
            if (!isMinimum)
            {
                return;
            }

            doc.CognitivePipelineActions = null;
            doc.DocName     = "";
            doc.DocUrl      = "";
            doc.IconSizeUrl = "";
            doc.TileSizeUrl = "";

            //Type specific optimization:
            if (doc is ShelfCompliance)
            {
                var temp = doc as ShelfCompliance;
                //Remove all matched classification result.
                temp.Classification = null;
            }
            else if (doc is EmployeeId)
            {
                var temp = doc as EmployeeId;
                //No further optmization needed for EmployeeId type
            }
            else if (doc is FaceAuthCard)
            {
                var temp = doc as FaceAuthCard;
                //Remove all face details. Just final authentication results will be part of the response.
                temp.FaceDetails = null;
            }
        }
        public static T MapDocument <T>(SmartDoc b) where T : SmartDoc, new()
        {
            T clone = new T();

            var members = b.GetType().GetMembers(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance);

            for (int i = 0; i < members.Length; i++)
            {
                if (members[i].MemberType == MemberTypes.Property)
                {
                    clone
                    .GetType()
                    .GetProperty(members[i].Name)
                    .SetValue(clone, b.GetType().GetProperty(members[i].Name).GetValue(b, null), null);
                }
            }
            return(clone);
        }
        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 #4
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));
        }