Esempio n. 1
0
        public bool WriteToStream(BaseCompositeFileProcessingProvider provider, StreamWriter sw, FileInfo fi, ClientDependencyType type, string origUrl, HttpContextBase http)
        {
            try
            {
                //if it is a file based dependency then read it
                var fileContents = File.ReadAllText(fi.FullName, Encoding.UTF8); //read as utf 8

                //for our custom file reader to work we just need to put the origUrl into the httpcontext items so
                //we can retreive it in the reader to then figure out the 'real' requested path.
                http.Items["Cdf_LessWriter_origUrl"] = origUrl;
                //get the default less config
                var config = DotlessConfiguration.GetDefaultWeb();
                //set the file reader to our custom file reader
                config.LessSource = typeof(CdfFileReader);
                //disable cache for this engine since we are already caching our own, plus enabling this will cause errors because
                // the import paths aren't resolved properly.
                config.CacheEnabled = false;
                //get the engine based on the custom config with our custom file reader
                var lessEngine = LessWeb.GetEngine(config);

                var output = lessEngine.TransformToCss(fileContents, origUrl);

                DefaultFileWriter.WriteContentToStream(provider, sw, output, type, http, origUrl);

                return(true);
            }
            catch (Exception ex)
            {
                ClientDependencySettings.Instance.Logger.Error(string.Format("Could not write file {0} contents to stream. EXCEPTION: {1}", fi.FullName, ex.Message), ex);
                return(false);
            }
        }
        /// <inheritdoc/>
        public bool WriteToStream(
            BaseCompositeFileProcessingProvider provider,
            StreamWriter streamWriter,
            IVirtualFile virtualFile,
            ClientDependencyType type,
            string originalUrl,
            HttpContextBase context)
        {
            StreamReader streamReader = null;

            try
            {
                Stream readStream = virtualFile.Open();
                streamReader = new StreamReader(readStream);
                string output = streamReader.ReadToEnd();

                DefaultFileWriter.WriteContentToStream(provider, streamWriter, output, type, context, originalUrl);

                return(true);
            }
            catch (Exception)
            {
                // The file must have failed to open
                return(false);
            }
            finally
            {
                // readStream is disposed by the streamReader.
                streamReader?.Dispose();
            }
        }
        /// <summary>
        /// Writes the actual contents of a file or request result to the stream and ensures the contents are minified if necessary
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="sw"></param>
        /// <param name="content"></param>
        /// <param name="type"></param>
        /// <param name="context"></param>
        /// <param name="originalUrl">The original Url that the content is related to</param>
        internal static void WriteContentToStream(BaseCompositeFileProcessingProvider provider, StreamWriter sw, string content, ClientDependencyType type, HttpContextBase context, string originalUrl)
        {
            if (type == ClientDependencyType.Css)
            {
                IEnumerable <string> importedPaths;
                var removedImports = CssHelper.ParseImportStatements(content, out importedPaths);

                //need to write the imported sheets first since these theoretically should *always* be at the top for browser to support them
                foreach (var importPath in importedPaths)
                {
                    var uri = new Uri(originalUrl, UriKind.RelativeOrAbsolute)
                              .MakeAbsoluteUri(context);
                    var absolute = uri.ToAbsolutePath(importPath);
                    provider.WritePathToStream(ClientDependencyType.Css, absolute, context, sw);
                }

                //ensure the Urls in the css are changed to absolute
                var parsedUrls = CssHelper.ReplaceUrlsWithAbsolutePaths(removedImports, originalUrl, context);

                //then we write the css with the removed import statements
                sw.WriteLine(provider.MinifyFile(parsedUrls, ClientDependencyType.Css));
            }
            else
            {
                sw.WriteLine(provider.MinifyFile(content, type));
            }
        }
        private static void WriteContentToStream(BaseCompositeFileProcessingProvider provider, StreamWriter sw, FileInfo inputFile, Stream stream,
                                                 ClientDependencyType type, HttpContextBase context, string originalUrl)
        {
            if (type == ClientDependencyType.Css)
            {
                IEnumerable <string> importedPaths;
                string externalImports;
                CssHelper.ParseImportStatements(stream, out importedPaths, out externalImports);

                //we can write the external imports found at the top
                sw.WriteLine(externalImports);

                //need to write the imported sheets first since these theoretically should *always* be at the top for browser to support them
                foreach (var importPath in importedPaths)
                {
                    var uri = new Uri(originalUrl, UriKind.RelativeOrAbsolute)
                              .MakeAbsoluteUri(context);
                    var absolute = uri.ToAbsolutePath(importPath);
                    provider.WritePathToStream(ClientDependencyType.Css, absolute, context, sw);
                }

                var minified = GetMinifiedOutput(provider, type, inputFile, stream);

                //ensure the Urls in the css are changed to absolute
                var parsedUrls = CssHelper.ReplaceUrlsWithAbsolutePaths(minified, originalUrl, context);

                //then we write the css with the removed import statements
                sw.WriteLine(parsedUrls);
            }
            else
            {
                sw.WriteLine(GetMinifiedOutput(provider, type, inputFile, stream));
            }
        }
Esempio n. 5
0
        public bool WriteToStream(
            BaseCompositeFileProcessingProvider provider,
            StreamWriter sw,
            IVirtualFile vf,
            ClientDependencyType type,
            string origUrl,
            HttpContextBase http)
        {
            try
            {
                using (var readStream = vf.Open())
                    using (var streamReader = new StreamReader(readStream))
                    {
                        var output = streamReader.ReadToEnd();
                        DefaultFileWriter.WriteContentToStream(provider, sw, output, type, http, origUrl);
                        return(true);
                    }
            }
            catch (Exception exception)
            {
                LogHelper.Warn(typeof(EmbeddedResourceVirtualFileWriter), exception.Message);

                return(false);
            }
        }
 public static void WriteContentToStream(BaseCompositeFileProcessingProvider provider, StreamWriter sw, FileInfo inputFile, ClientDependencyType type, HttpContextBase context, string originalUrl)
 {
     using (var fileStream = inputFile.OpenRead())
     {
         WriteContentToStream(provider, sw, inputFile, fileStream, type, context, originalUrl);
     }
 }
 public bool WriteToStream(BaseCompositeFileProcessingProvider provider, StreamWriter sw, FileInfo fi, ClientDependencyType type, string origUrl, HttpContextBase http)
 {
     try
     {
         WriteContentToStream(provider, sw, fi, type, http, origUrl);
         return(true);
     }
     catch (Exception ex)
     {
         ClientDependencySettings.Instance.Logger.Error($"Could not write file {fi.FullName} contents to stream. EXCEPTION: {ex.Message}", ex);
         return(false);
     }
 }
 public bool WriteToStream(BaseCompositeFileProcessingProvider provider, StreamWriter sw, FileInfo fi, ClientDependencyType type, string origUrl, HttpContextBase http)
 {
     try
     {
         //if it is a file based dependency then read it
         var fileContents = File.ReadAllText(fi.FullName, Encoding.UTF8); //read as utf 8
         WriteContentToStream(provider, sw, fileContents, type, http, origUrl);
         return(true);
     }
     catch (Exception ex)
     {
         ClientDependencySettings.Instance.Logger.Error(string.Format("Could not write file {0} contents to stream. EXCEPTION: {1}", fi.FullName, ex.Message), ex);
         return(false);
     }
 }
Esempio n. 9
0
        public bool WriteToStream(BaseCompositeFileProcessingProvider provider, StreamWriter sw, FileInfo fi, ClientDependencyType type, string origUrl, HttpContextBase http)
        {
            try
            {
                //NOTE: We don't want this compressed since CDF will do that ourselves
                var output = Compiler.Compile(fi.FullName, false, new List <string>());

                DefaultFileWriter.WriteContentToStream(provider, sw, output, type, http, origUrl);

                return(true);
            }
            catch (Exception ex)
            {
                ClientDependencySettings.Instance.Logger.Error(string.Format("Could not write file {0} contents to stream. EXCEPTION: {1}", fi.FullName, ex.Message), ex);
                return(false);
            }
        }
Esempio n. 10
0
 public bool WriteToStream(BaseCompositeFileProcessingProvider provider, StreamWriter sw, IVirtualFile vf, ClientDependencyType type, string origUrl, HttpContextBase http)
 {
     try
     {
         using (var readStream = vf.Open())
             using (var streamReader = new StreamReader(readStream))
             {
                 var output = streamReader.ReadToEnd();
                 DefaultFileWriter.WriteContentToStream(provider, sw, output, type, http, origUrl);
                 return(true);
             }
     }
     catch (Exception)
     {
         // the file must have failed to open
         return(false);
     }
 }
Esempio n. 11
0
        public bool WriteToStream(BaseCompositeFileProcessingProvider provider, StreamWriter sw, FileInfo fi, ClientDependencyType type, string origUrl, HttpContextBase http)
        {
            try
            {
                //if it is a file based dependency then read it
                var fileContents = File.ReadAllText(fi.FullName, Encoding.UTF8); //read as utf 8

                //NOTE: passing in null will automatically for the web configuration section to be loaded in!
                var output = LessWeb.Parse(fileContents, null);

                DefaultFileWriter.WriteContentToStream(provider, sw, output, type, http, origUrl);

                return(true);
            }
            catch (Exception ex)
            {
                ClientDependencySettings.Instance.Logger.Error(string.Format("Could not write file {0} contents to stream. EXCEPTION: {1}", fi.FullName, ex.Message), ex);
                return(false);
            }
        }
Esempio n. 12
0
 public static void WriteContentToStream(BaseCompositeFileProcessingProvider provider, StreamWriter sw, Stream stream, ClientDependencyType type, HttpContextBase context, string originalUrl)
 {
     WriteContentToStream(provider, sw, null, stream, type, context, originalUrl);
 }
Esempio n. 13
0
 private static string GetMinifiedOutput(BaseCompositeFileProcessingProvider provider, ClientDependencyType type, FileInfo inputFile, Stream inputStream)
 {
     return(ShouldMinify(inputFile)
         ? provider.MinifyFile(inputStream, type)
         : BaseCompositeFileProcessingProvider.StreamToString(inputStream));
 }