public HttpResponseMessage Item(string region, string key) { var cache = ServiceHelper.Cache as FileCache; if (cache == null) { throw new InvalidOperationException("FileCache must be used for this operation"); } Uri itemUri = cache.GetItemExternalResource(key, region, false); string fullPath = itemUri.LocalPath; if (!File.Exists(fullPath)) { throw new ServiceException("File not found", HttpStatusCode.NotFound); } var response = new HttpResponseMessage(); // For "Save to Google Drive" access, we must have the appropriate CORS headers. // See https://developers.google.com/drive/v3/web/savetodrive response.Headers.Remove("Access-Control-Allow-Headers"); response.Headers.Add("Access-Control-Allow-Headers", "Range"); response.Headers.Remove("Access-Control-Expose-Headers"); response.Headers.Add("Access-Control-Expose-Headers", "Cache-Control, Content-Encoding, Content-Range"); try { string contentType = "application/octet-stream"; try { var extension = Path.GetExtension(fullPath); // ZIP is not handled by RasterCodecs, so check it here if (!string.IsNullOrEmpty(extension) && !extension.EndsWith("zip", StringComparison.OrdinalIgnoreCase)) { // Use RasterCodecs helper method to get it var extensionContentType = RasterCodecs.GetExtensionMimeType(extension); if (!string.IsNullOrWhiteSpace(extensionContentType)) { contentType = extensionContentType; } } } catch { } Action <Stream, HttpContent, TransportContext> write = (stream, content, context) => { try { using (var fs = File.OpenRead(fullPath)) ServiceHelper.CopyStream(fs, stream); } finally { stream.Close(); } }; response.StatusCode = HttpStatusCode.OK; response.Content = new PushStreamContent(write, new MediaTypeHeaderValue(contentType)); ServiceHelper.SetResponseViewFileName(response, key, contentType); return(response); } catch (IOException) { throw new ServiceException("File could not be streamed", HttpStatusCode.InternalServerError); } }
[HttpPost, HttpGet] // Support GET only for testing public LoadFromUriResponse LoadFromUri(LoadFromUriRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (request.Uri == null) { throw new ArgumentException("uri must be specified"); } ServiceHelper.CheckUriScheme(request.Uri); if (request.Options != null && request.Options.AnnotationsUri != null) { ServiceHelper.CheckUriScheme(request.Options.AnnotationsUri); } if (request.Resolution < 0) { throw new ArgumentException("Resolution must be a value greater than or equal to zero"); } var cache = ServiceHelper.Cache; var loadOptions = new LoadDocumentOptions(); loadOptions.Cache = cache; loadOptions.UseCache = cache != null; loadOptions.CachePolicy = ServiceHelper.CreatePolicy(); loadOptions.WebClient = null; // Use default if (request.Options != null) { loadOptions.DocumentId = request.Options.DocumentId; loadOptions.AnnotationsUri = request.Options.AnnotationsUri; loadOptions.Name = request.Options.Name; loadOptions.Password = request.Options.Password; loadOptions.LoadEmbeddedAnnotations = request.Options.LoadEmbeddedAnnotations; loadOptions.MaximumImagePixelSize = request.Options.MaximumImagePixelSize; loadOptions.FirstPageNumber = request.Options.FirstPageNumber; loadOptions.LastPageNumber = request.Options.LastPageNumber; } // Get the document name var documentName = request.Uri.ToString(); // Check if this document was uploaded, then hope the user has set LoadDocumentOptions.Name to the original file name if (DocumentFactory.IsUploadDocumentUri(request.Uri) && !string.IsNullOrEmpty(loadOptions.Name)) { // Use that instead documentName = loadOptions.Name; } // Most image file formats have a signature that can be used to detect to detect the type of the file. // However, some formats supported by LEADTOOLS do not, such as plain text files (TXT) or DXF CAD format or // For these, we detect the MIME type from the file extension if available and set it in the load document options and the // documents library will use this value if it fails to detect the file format from the data. if (!string.IsNullOrEmpty(documentName)) { loadOptions.MimeType = RasterCodecs.GetExtensionMimeType(documentName); } LEADDocument document = null; try { // first, check if this is pre-cached if (PreCacheHelper.PreCacheExists) { string documentId = PreCacheHelper.CheckDocument(request.Uri, loadOptions.MaximumImagePixelSize); if (documentId != null) { var precachedDocument = DocumentFactory.LoadFromCache(cache, documentId); // Instead of returning the same pre-cached document, we'll return a cloned version. // This allows the user to make changes (get/set annotations) without affecting the pre-cached version. document = precachedDocument.Clone(cache, new CloneDocumentOptions() { CachePolicy = ServiceHelper.CreatePolicy() }); } } // else, load normally if (document == null) { document = DocumentFactory.LoadFromUri(request.Uri, loadOptions); if (document == null) { // This document was rejected due to its mimeType. throw new InvalidOperationException("Document at URI '" + request.Uri + "' uses a blocked mimeType"); } } CacheController.TrySetCacheUri(document); if (ServiceHelper.AutoUpdateHistory) { document.History.AutoUpdate = true; } ServiceHelper.SetRasterCodecsOptions(document.RasterCodecs, request.Resolution); document.AutoDeleteFromCache = false; document.AutoDisposeDocuments = true; document.AutoSaveToCache = false; document.SaveToCache(); /* * NOTE: Use the line below to add this new document * to the pre-cache. By doing so, everyone loading a document from * that URL will get a copy of the same document from the cache/pre-cache. * * if (!isInPrecache) * PreCacheHelper.AddExistingDocument(request.Uri, document); */ return(new LoadFromUriResponse { Document = document }); } finally { if (document != null) { document.Dispose(); } } }