Exemple #1
0
        public void ProcessRequest(HttpContext context)
        {
            var request  = context.Request;
            var response = context.Response;

            VirtualFile vf       = null;
            var         filePath = request.FilePath;

            // Cross-Origin Resource Sharing (CORS)
            if (!HttpHeaderTools.IsOriginHeaderAllowed())
            {
                AuthenticationHelper.ThrowForbidden(filePath);
            }

            if (HostingEnvironment.VirtualPathProvider.FileExists(filePath))
            {
                vf = HostingEnvironment.VirtualPathProvider.GetFile(filePath);
            }

            if (vf == null)
            {
                throw new HttpException(404, "File does not exist");
            }

            response.ClearContent();

            // Set content type only if this is not a RepositoryFile, because the
            // Open method of RepositoryFile will set the content type itself.
            if (!(vf is RepositoryFile))
            {
                var extension = System.IO.Path.GetExtension(filePath);
                context.Response.ContentType = MimeTable.GetMimeType(extension);

                // add the necessary header for the css font-face rule
                if (MimeTable.IsFontType(extension))
                {
                    HttpHeaderTools.SetAccessControlHeaders();
                }
            }

            // The bytes we write into the output stream will NOT be buffered and will be sent
            // to the client immediately. This makes sure that the whole (potentially large)
            // file is not loaded into the memory on the server.
            response.BufferOutput = false;

            using (var stream = vf.Open())
            {
                response.AppendHeader("Content-Length", stream.Length.ToString());
                response.Clear();

                // Let ASP.NET handle sending bytes to the client (avoid Flush).
                stream.CopyTo(response.OutputStream);
            }
        }
Exemple #2
0
        public void ProcessRequest(HttpContext context)
        {
            var request  = context.Request;
            var response = context.Response;

            VirtualFile vf       = null;
            var         filePath = request.FilePath;

            if (HostingEnvironment.VirtualPathProvider.FileExists(filePath))
            {
                vf = HostingEnvironment.VirtualPathProvider.GetFile(filePath);
            }

            if (vf == null)
            {
                throw new HttpException(404, "File does not exist");
            }

            response.ClearContent();

            //Set content type only if this is not a RepositoryFile, because the
            //Open method of RepositoryFile will set the content type itself.
            if (!(vf is RepositoryFile))
            {
                var extension = System.IO.Path.GetExtension(filePath);
                context.Response.ContentType = MimeTable.GetMimeType(extension);

                //add the necessary header for the css font-face rule
                if (MimeTable.IsFontType(extension))
                {
                    HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", HttpContext.Current.Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.SafeUnescaped));
                }
            }

            using (var stream = vf.Open())
            {
                response.AppendHeader("Content-Length", stream.Length.ToString());

                //We need to Flush the headers before
                //we start to stream the actual binary.
                response.Flush();

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

                while ((readed = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    response.OutputStream.Write(buffer, 0, readed);
                    response.Flush();
                }
            }
        }
        private static void ProcessRequestUsingVirtualFile(HttpContext context, string repositoryPath)
        {
            byte[] buffer = ReadVirtualFile(repositoryPath);

            context.Response.ClearContent();

            context.Response.OutputStream.Write(buffer, 0, buffer.Length);

            context.Response.ContentType =
                MimeTable.GetMimeType(System.IO.Path.GetExtension(repositoryPath));

            context.Response.End();
        }
Exemple #4
0
        public void BinaryData_CreateImageByBinary()
        {
            var testFileName        = "test.jpg";
            var expectedContentType = MimeTable.GetMimeType(System.IO.Path.GetExtension(testFileName).ToLower());
            var testBinaryData      = new BinaryData();

            testBinaryData.FileName = testFileName;

            var image = Image.CreateByBinary(this.TestRoot as IFolder, testBinaryData);

            Assert.AreEqual(testFileName, image.Name);
            Assert.AreEqual(testFileName, image.Binary.FileName.ToString());
            Assert.AreEqual(expectedContentType, image.Binary.ContentType);
        }
        private static void ProcessRequestUsingFilesystemCache(HttpContext context, string repositoryPath, string fullyQualyfiedPath)
        {
            // Check the filesystem cache, create the file if not exists
            if (!System.IO.File.Exists(fullyQualyfiedPath))
            {
                byte[] buffer = ReadVirtualFile(repositoryPath);

                string directoryName = Path.GetDirectoryName(fullyQualyfiedPath);
                if (!Directory.Exists(directoryName))
                {
                    System.IO.Directory.CreateDirectory(directoryName);
                }

                using (var cachedFile = System.IO.File.Create(fullyQualyfiedPath))
                {
                    cachedFile.Write(buffer, 0, buffer.Length);
                }
            }
            else
            {
                var servedFromFileSystem = true;

                // check the date
                DateTime lastWriteTime   = System.IO.File.GetLastWriteTime(fullyQualyfiedPath);
                DateTime now             = DateTime.UtcNow;
                var      nodeDescription = NodeHead.Get(repositoryPath);

                if (lastWriteTime.AddMinutes(CacheTimeframeInMinutes) < now)
                {
                    // update if needed
                    CreateLockFile(fullyQualyfiedPath);

                    int         maxTryNum = 20;
                    IOException lastError = null;
                    while (--maxTryNum >= 0)
                    {
                        try
                        {
                            System.IO.File.SetLastWriteTime(fullyQualyfiedPath, now);
                            break;
                        }
                        catch (IOException ioex) //TODO: catch block
                        {
                            lastError = ioex;
                            System.Threading.Thread.Sleep(200);
                        }
                    }
                    if (lastError != null)
                    {
                        throw new IOException("Cannot write the file: " + fullyQualyfiedPath, lastError);
                    }

                    if (nodeDescription.ModificationDate > lastWriteTime)
                    {
                        // refresh file
                        byte[] buffer = ReadVirtualFile(repositoryPath);
                        servedFromFileSystem = false;

                        using (var cachedFile = System.IO.File.Open(fullyQualyfiedPath, FileMode.Truncate, FileAccess.Write))
                        {
                            cachedFile.Write(buffer, 0, buffer.Length);
                        }
                    }

                    DeleteLockFile(fullyQualyfiedPath);
                }

                // only log file download if it was not logged before by the virtual file provider
                if (servedFromFileSystem)
                {
                    // let the client code log file downloads
                    if (nodeDescription != null &&
                        Providers.Instance.StorageSchema.NodeTypes[nodeDescription.NodeTypeId].IsInstaceOfOrDerivedFrom("File"))
                    {
                        ContentRepository.File.Downloaded(nodeDescription.Id);
                    }
                }
            }

            string extension = System.IO.Path.GetExtension(repositoryPath);

            context.Response.ContentType = MimeTable.GetMimeType(extension);

            context.Response.TransmitFile(fullyQualyfiedPath);
        }