예제 #1
0
        public async Task <byte[]> ExecuteAsync(HttpContext context, IWebOptimizerOptions options)
        {
            var env    = (IHostingEnvironment)context.RequestServices.GetService(typeof(IHostingEnvironment));
            var config = new AssetContext(context, this, options);

            IEnumerable <string> files = ExpandGlobs(this, env);

            DateTime lastModified = DateTime.MinValue;

            // Read file content into memory
            foreach (string file in files)
            {
                if (!config.Content.ContainsKey(file))
                {
                    DateTime dateChanged = await LoadFileContentAsync(this.GetFileProvider(env), config, file);

                    if (dateChanged > lastModified)
                    {
                        lastModified = dateChanged;
                    }
                }
            }

            if (lastModified != DateTime.MinValue)
            {
                context.Response.Headers[HeaderNames.LastModified] = lastModified.ToString("R");
            }

            // Attach the processors
            foreach (IProcessor processor in Processors)
            {
                await processor.ExecuteAsync(config).ConfigureAwait(false);
            }

            return(config.Content.FirstOrDefault().Value);
        }
예제 #2
0
        private static async Task <DateTime> LoadFileContentAsync(IFileProvider fileProvider, AssetContext config, string sourceFile)
        {
            IFileInfo file = fileProvider.GetFileInfo(sourceFile);

            using (Stream fs = File.OpenRead(file.PhysicalPath))
            {
                byte[] bytes = await fs.AsBytesAsync();

                if (bytes.Length > 2 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF)
                {
                    bytes = bytes.Skip(3).ToArray();
                }

                config.Content.Add(sourceFile, bytes);
            }

            return(file.LastModified.UtcDateTime);
        }