示例#1
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        public virtual void ProcessRequestCore(HttpContextBase context)
        {
            var requestedNode = RequestedNode;
            var propertyName  = PropertyName;

            if (string.IsNullOrEmpty(propertyName) || requestedNode == null)
            {
                context.Response.StatusCode = 404;
                return;
            }

            // Get the stream through our provider to let 3rd party developers serve custom data
            string         contentType;
            BinaryFileName fileName;

            using (var binaryStream = DocumentBinaryProvider.Current.GetStream(requestedNode, propertyName, out contentType, out fileName))
            {
                if (binaryStream == null)
                {
                    return;
                }

                using (var resizedStream = GetResizedOrOriginalStream(binaryStream, contentType))
                {
                    // We need to Flush the headers before we start to stream the actual binary.
                    context.Response.ContentType = contentType;
                    context.Response.AppendHeader("Content-Length", resizedStream.Length.ToString());

                    HttpHeaderTools.SetContentDispositionHeader(fileName);

                    HttpHeaderTools.SetCacheControlHeaders(lastModified: requestedNode.ModificationDate);
                    if (this.MaxAge.HasValue)
                    {
                        HttpHeaderTools.SetCacheControlHeaders(maxAge: this.MaxAge);
                    }

                    context.Response.StatusCode = 200;
                    context.Response.Flush();

                    resizedStream.Position = 0;

                    // Let ASP.NET handle sending bytes to the client.
                    resizedStream.CopyTo(context.Response.OutputStream);
                }
            }

            // Let the client code log file downloads
            var file = requestedNode as ContentRepository.File;

            if (file != null)
            {
                ContentRepository.File.Downloaded(file.Id);
            }
        }
示例#2
0
        internal void ProcessRequest(HttpContext context, bool calledFromTest)
        {
            // Get actors.
            var webResponse   = context.Response;
            var portalContext = (PortalContext)context.Items[PortalContext.CONTEXT_ITEM_KEY];
            var wopiResponse  = GetResponse(portalContext);

            // Set content type if it is known.
            if (!string.IsNullOrEmpty(wopiResponse.ContentType))
            {
                webResponse.ContentType = wopiResponse.ContentType;
            }

            // Set response headers if any (works well only in IIS evironment).
            if (!calledFromTest)
            {
                foreach (var item in wopiResponse.Headers)
                {
                    webResponse.Headers.Add(item.Key, item.Value);
                }
            }

            // Set HTTP Status code.
            webResponse.StatusCode = (int)wopiResponse.StatusCode;

            // Write binary content
            if (wopiResponse is IWopiBinaryResponse wopiBinaryResponse)
            {
                var stream = wopiBinaryResponse.GetResponseStream();
                if (!calledFromTest)
                {
                    HttpHeaderTools.SetContentDispositionHeader(wopiBinaryResponse.FileName);
                    context.Response.AppendHeader("Content-Length", stream.Length.ToString());
                }
                stream.CopyTo(context.Response.OutputStream);
                return;
            }

            // Write JSON body
            if (wopiResponse is IWopiObjectResponse)
            {
                var settings = new JsonSerializerSettings {
                    Formatting = Formatting.Indented
                };
                var serializer = JsonSerializer.Create(settings);
                serializer.Serialize(webResponse.Output, wopiResponse);
                webResponse.Flush();
            }
        }
示例#3
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        public virtual void ProcessRequestCore(HttpContextBase context)
        {
            var propertyName  = PropertyName;
            var requestedNode = RequestedNode;

            if (string.IsNullOrEmpty(propertyName) || requestedNode == null)
            {
                context.Response.StatusCode = 404;
                return;
            }

            // Get the stream through our provider to let 3rd party developers serve custom data
            string         contentType;
            BinaryFileName fileName;
            var            binaryStream = DocumentBinaryProvider.Current.GetStream(requestedNode, propertyName, out contentType, out fileName);

            if (binaryStream == null)
            {
                return;
            }

            Stream resizedStream;

            // If this is an image and we need to resize it
            if (Width.HasValue && Height.HasValue)
            {
                resizedStream = Image.CreateResizedImageFile(binaryStream, string.Empty, Width.Value, Height.Value, 0, contentType);
            }
            else
            {
                resizedStream = binaryStream;
            }

            // We need to Flush the headers before
            // We start to stream the actual binary.

            context.Response.ContentType = contentType;
            context.Response.AppendHeader("Content-Length", resizedStream.Length.ToString());

            HttpHeaderTools.SetContentDispositionHeader(fileName);

            HttpHeaderTools.SetCacheControlHeaders(lastModified: requestedNode.ModificationDate);
            if (this.MaxAge.HasValue)
            {
                HttpHeaderTools.SetCacheControlHeaders(maxAge: this.MaxAge);
            }

            context.Response.StatusCode = 200;
            context.Response.Flush();

            resizedStream.Position = 0;

            var buffer = new byte[Math.Min(resizedStream.Length, RepositoryConfiguration.BinaryChunkSize)];
            int bytesRead;

            //while (bytesRead > 0)
            while ((bytesRead = resizedStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                context.Response.OutputStream.Write(buffer, 0, bytesRead);
                context.Response.Flush();
            }

            // Let the client code log file downloads
            var file = requestedNode as ContentRepository.File;

            if (file != null)
            {
                ContentRepository.File.Downloaded(file.Id);
            }
        }