Exemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JavaScriptCruncher"/> class.
 /// </summary>
 /// <param name="options">
 /// The options containing instructions for the cruncher.
 /// </param>
 /// <param name="context">
 /// The current context.
 /// </param>
 public JavaScriptCruncher(CruncherOptions options, HttpContext context)
     : base(options)
 {
     this.context = context;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Processes the JavaScript request using cruncher and returns the result.
        /// </summary>
        /// <param name="context">
        /// The current context.
        /// </param>
        /// <param name="minify">
        /// Whether to minify the output.
        /// </param>
        /// <param name="paths">
        /// The paths to the resources to crunch.
        /// </param>
        /// <returns>
        /// The <see cref="string"/> representing the processed result.
        /// </returns>
        public async Task <string> ProcessJavascriptCrunchAsync(HttpContext context, bool minify, params string[] paths)
        {
            string combinedJavaScript = string.Empty;

            if (paths != null)
            {
                string key = string.Join(string.Empty, paths).ToMd5Fingerprint();

                using (await Locker.LockAsync(key))
                {
                    combinedJavaScript = (string)CacheManager.GetItem(key);

                    if (string.IsNullOrWhiteSpace(combinedJavaScript))
                    {
                        StringBuilder stringBuilder = new StringBuilder();

                        CruncherOptions cruncherOptions = new CruncherOptions
                        {
                            MinifyCacheKey     = key,
                            Minify             = minify,
                            CacheFiles         = true,
                            AllowRemoteFiles   = CruncherConfiguration.Instance.AllowRemoteDownloads,
                            RemoteFileMaxBytes = CruncherConfiguration.Instance.MaxBytes,
                            RemoteFileTimeout  = CruncherConfiguration.Instance.Timeout
                        };

                        JavaScriptCruncher javaScriptCruncher = new JavaScriptCruncher(cruncherOptions, context);

                        // Loop through and process each file.
                        foreach (string path in paths)
                        {
                            // Local files.
                            if (PreprocessorManager.Instance.AllowedExtensionsRegex.IsMatch(path))
                            {
                                List <string> files = new List <string>();

                                // Try to get the file using absolute/relative path
                                if (!ResourceHelper.IsResourceFilenameOnly(path))
                                {
                                    string javaScriptFilePath = ResourceHelper.GetFilePath(
                                        path,
                                        cruncherOptions.RootFolder,
                                        context);

                                    if (File.Exists(javaScriptFilePath))
                                    {
                                        files.Add(javaScriptFilePath);
                                    }
                                }
                                else
                                {
                                    // Get the path from the server.
                                    // Loop through each possible directory.
                                    foreach (string javaScriptFolder in CruncherConfiguration.Instance.JavaScriptPaths)
                                    {
                                        if (!string.IsNullOrWhiteSpace(javaScriptFolder) &&
                                            javaScriptFolder.Trim().StartsWith("~/"))
                                        {
                                            DirectoryInfo directoryInfo =
                                                new DirectoryInfo(context.Server.MapPath(javaScriptFolder));

                                            if (directoryInfo.Exists)
                                            {
                                                files.AddRange(
                                                    Directory.GetFiles(
                                                        directoryInfo.FullName,
                                                        path,
                                                        SearchOption.AllDirectories));
                                            }
                                        }
                                    }
                                }

                                if (files.Any())
                                {
                                    // We only want the first file.
                                    string first = files.FirstOrDefault();
                                    cruncherOptions.RootFolder = Path.GetDirectoryName(first);
                                    stringBuilder.Append(await javaScriptCruncher.CrunchAsync(first));
                                }
                            }
                            else
                            {
                                // Remote files.
                                string remoteFile = this.GetUrlFromToken(path).ToString();
                                stringBuilder.Append(await javaScriptCruncher.CrunchAsync(remoteFile));
                            }
                        }

                        combinedJavaScript = stringBuilder.ToString();

                        if (minify)
                        {
                            // Minify.
                            combinedJavaScript = javaScriptCruncher.Minify(combinedJavaScript);
                        }

                        this.AddItemToCache(key, combinedJavaScript, javaScriptCruncher.FileMonitors);
                    }
                }
            }

            return(combinedJavaScript);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CssCruncher"/> class.
 /// </summary>
 /// <param name="options">The options containing instructions for the cruncher.</param>
 public CssCruncher(CruncherOptions options)
     : base(options)
 {
 }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CssCruncher"/> class.
 /// </summary>
 /// <param name="options">
 /// The options containing instructions for the cruncher.
 /// </param>
 /// <param name="context">
 /// The current context.
 /// </param>
 public CssCruncher(CruncherOptions options, HttpContext context)
     : base(options)
 {
     this.context = context;
 }
Exemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CruncherBase"/> class.
 /// </summary>
 /// <param name="options">The options containing instructions for the cruncher.</param>
 protected CruncherBase(CruncherOptions options)
 {
     this.Options      = options;
     this.FileMonitors = new ConcurrentBag <string>();
 }
Exemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JavaScriptCruncher"/> class.
 /// </summary>
 /// <param name="options">The options containing instructions for the cruncher.</param>
 public JavaScriptCruncher(CruncherOptions options)
     : base(options)
 {
 }
Exemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CruncherBase"/> class.
 /// </summary>
 /// <param name="options">The options containing instructions for the cruncher.</param>
 protected CruncherBase(CruncherOptions options)
 {
     this.Options      = options;
     this.FileMonitors = new List <string>();
 }