Ejemplo n.º 1
0
            public async Task <Result <PreCacheDocumentResponse> > PreCacheDocument(PreCacheDocumentRequest request)
            {
                const string api      = "api/Factory/PreCacheDocument";
                var          response = await _owner.PostAsync <PreCacheDocumentRequest, PreCacheDocumentResponse>(api, request);

                return(response);
            }
        public PreCacheDocumentResponse PreCacheDocument(PreCacheDocumentRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            string passToCheck = ServiceHelper.GetSettingValue(ServiceHelper.Key_Access_Passcode);

            if (!string.IsNullOrWhiteSpace(passToCheck) && request.Passcode != passToCheck)
            {
                throw new ServiceException("Document cannot be pre-cached - passcode is incorrect", HttpStatusCode.Unauthorized);
            }

            if (request.Uri == null)
            {
                throw new ArgumentException("uri must be specified");
            }

            ServiceHelper.CheckUriScheme(request.Uri);

            if (!PreCacheHelper.PreCacheExists)
            {
                // Return an empty item
                return(new PreCacheDocumentResponse());
            }

            var cache = ServiceHelper.Cache;

            // Get the cache options, if none, use All (means if the user did not pass a value, we will cache everything in the document)
            if (request.CacheOptions == DocumentCacheOptions.None)
            {
                request.CacheOptions = DocumentCacheOptions.All;
            }

            return(PreCacheHelper.AddDocument(cache, request));
        }
Ejemplo n.º 3
0
        public static PreCacheDocumentResponse AddDocument(ObjectCache cache, PreCacheDocumentRequest request)
        {
            var loadOptions = new LoadDocumentOptions();

            loadOptions.Cache    = cache;
            loadOptions.UseCache = true;

            // Get the expiry policy
            CacheItemPolicy cachePolicy;

            if (request.ExpiryDate == new DateTime())
            {
                cachePolicy = ServiceHelper.CreateForeverPolicy();
            }
            else
            {
                cachePolicy = new CacheItemPolicy()
                {
                    AbsoluteExpiration = request.ExpiryDate
                };
            }

            loadOptions.CachePolicy = cachePolicy;
            // Get the maximum pixel size, if the user did not pass one, use the default values of 4096 and 2048 (used by the DocumentViewerDemo)
            var maximumImagePixelSizes = request.MaximumImagePixelSizes;

            if (maximumImagePixelSizes == null || maximumImagePixelSizes.Length == 0)
            {
                maximumImagePixelSizes = DefaultSizes;
            }

            // Sort the maximum image pixel size from largest to smallest
            // We will re-use the values from largest to set the smaller images text and SVG since they do
            // not change
            Array.Sort(
                maximumImagePixelSizes,
                new Comparison <int>((x, y) => y.CompareTo(x)));

            // Get the safe hash name from the uri
            var regionName = GetRegionName(request.Uri);

            // The PreCacheEntries are what will be cached, based on this map of sizes to documentId values.
            var sizeIdDictionary = new Dictionary <int, string>();

            // The PreCacheResponseItems are what we will return as a confirmation.
            var responseItems = new PreCacheResponseSizeItem[maximumImagePixelSizes.Length];

            var ocrEngine = ServiceHelper.GetOCREngine();

            // Largest document (to re-use)
            LEADDocument largestDocument = null;

            try
            {
                // Now load the document and cache it
                for (var index = 0; index < maximumImagePixelSizes.Length; index++)
                {
                    // No duplicates
                    if (index > 0 && maximumImagePixelSizes[index] == maximumImagePixelSizes[index - 1])
                    {
                        continue;
                    }

                    var size = maximumImagePixelSizes[index];

                    // If it's in the cache, delete it (deletes from PreCache also)
                    string documentId = InternalCheckDocument(regionName, GetKeyName(size));
                    if (documentId != null)
                    {
                        DocumentHelper.DeleteDocument(documentId, false, false);
                    }

                    // keep track for logging purposes
                    var start = DateTime.Now;

                    // re-use the load options, just change the size
                    loadOptions.MaximumImagePixelSize = size;

                    // Cache the Document
                    var document = AddDocumentToCache(largestDocument, ocrEngine, request.Uri, loadOptions, request.CacheOptions);
                    try
                    {
                        var stop = DateTime.Now;
                        documentId = document.DocumentId;

                        responseItems[index] = new PreCacheResponseSizeItem()
                        {
                            Seconds               = Math.Round((stop - start).TotalSeconds, 4),
                            DocumentId            = documentId,
                            MaximumImagePixelSize = size,
                        };

                        // add to our dictionary for updating the pre-cache all at once
                        sizeIdDictionary.Add(size, documentId);
                    }
                    finally
                    {
                        if (largestDocument == null)
                        {
                            largestDocument = document;
                        }
                        else
                        {
                            document.Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (largestDocument != null)
                {
                    largestDocument.Dispose();
                }
            }

            // Add all the info to the PreCache
            AddDocumentToPreCache(regionName, request.Uri, sizeIdDictionary);

            return(new PreCacheDocumentResponse()
            {
                Item = new PreCacheResponseItem()
                {
                    Uri = request.Uri.ToString(),
                    RegionHash = regionName,
                    Items = responseItems,
                }
            });
        }