/// <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)); } }
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; } }
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; } }
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; } }
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 using (var fileStream = fi.OpenRead()) { WriteContentToStream(provider, sw, fileStream, 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; } }
/// <summary> /// The write to stream. /// </summary> /// <param name="provider"> /// The provider. /// </param> /// <param name="sw"> /// The sw. /// </param> /// <param name="vf"> /// The vf. /// </param> /// <param name="type"> /// The type. /// </param> /// <param name="origUrl"> /// The orig url. /// </param> /// <param name="http"> /// The http. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> 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; } }
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; } }
/// <summary> /// Writes the virtual file to a stream for serving. /// </summary> /// <param name="provider">The file processing provider.</param> /// <param name="streamWriter">The <see cref="StreamWriter"/>.</param> /// <param name="virtualFile">The <see cref="IVirtualFile"/> to write.</param> /// <param name="type">The <see cref="ClientDependencyType"/> the virtual file matches.</param> /// <param name="originalUrl">The original url to the virtual file.</param> /// <param name="context">The <see cref="HttpContextBase"/>.</param> /// <returns> /// The <see cref="bool"/>. /// </returns> public bool WriteToStream( BaseCompositeFileProcessingProvider provider, StreamWriter streamWriter, IVirtualFile virtualFile, ClientDependencyType type, string originalUrl, HttpContextBase context) { try { using (Stream readStream = virtualFile.Open()) using (StreamReader 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; } }