private byte[] GetFileBytes(HttpContext context, string virtualPath, Encoding encoding) { if (virtualPath.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase) || virtualPath.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase)) { using (var client = new WebClient()) { return client.DownloadData(virtualPath); } } else { virtualPath = CombinerHelper.GetUrlFromFilename(_path, virtualPath); string physicalPath = HttpContext.Current.Server.MapPath(virtualPath); string extension = Path.GetExtension(virtualPath); if (!_allowedFileExtension.Contains(extension)) throw new SecurityException("Combiner couldn't find a valid parser for file " + virtualPath); byte[] bytes = new byte[1024]; // Don't compress if it already is compressed if (!(physicalPath.Contains(".min.") || physicalPath.Contains("-min."))) { // Use minified file if one exists string newPhysicalPath = physicalPath.Replace(extension, ""); newPhysicalPath = newPhysicalPath + ".min" + extension; if (File.Exists(newPhysicalPath)) { physicalPath = newPhysicalPath; bytes = File.ReadAllBytes(physicalPath); } else { if (File.Exists(physicalPath)) { // Compress on-the-fly using YUI string uncompresed = File.ReadAllText(physicalPath); string compressed = ""; try { if (extension == ".css") { compressed = new CssCompressor().Compress(uncompresed); // Fix for media-queries, please remove once its no longer needed compressed = compressed.Replace(" screen and(", " screen and ("); } else if (extension == ".js") { compressed = new JavaScriptCompressor().Compress(uncompresed); } } catch (Exception ex) { // Something went wrong... let's use the uncompressed version //Logger.Fatal("Error compressing " + virtualPath, ex); compressed = "/* Error compressing */\n" + uncompresed; } bytes = encoding.GetBytes(compressed); } else { // File does not exist throw new Exception(physicalPath + " does not exist"); } } } else { // File already compressed. Lets let it string alreadycompresed = File.ReadAllText(physicalPath); bytes = encoding.GetBytes(alreadycompresed); } // TODO: Convert unicode files to specified encoding. For now, assuming // files are either ASCII or UTF8 return bytes; } }