Example #1
0
        private async Task <IEnumerable <string> > GenerateUrlsAsync(string bundleName, string fileExt)
        {
            var result       = new List <string>();
            var bundleExists = _bundleManager.Exists(bundleName);

            if (!bundleExists)
            {
                return(null);
            }

            if (_config.IsDebug)
            {
                var urls  = new List <string>();
                var files = _bundleManager.GetFiles(bundleName, _request);
                foreach (var d in files)
                {
                    urls.Add(d.FilePath);
                }

                foreach (var url in urls)
                {
                    result.Add(url);
                }
                return(result);
            }
            else
            {
                var compression = _request.GetClientCompression();
                var url         = _context.UrlCreator.GetUrl(bundleName, fileExt);

                //now we need to determine if these files have already been minified
                var compositeFilePath = _fileSystemHelper.GetCurrentCompositeFilePath(compression, bundleName);
                if (!File.Exists(compositeFilePath))
                {
                    var files = _bundleManager.GetFiles(bundleName, _request);
                    //we need to do the minify on the original files
                    foreach (var file in files)
                    {
                        await _fileManager.ProcessAndCacheFileAsync(file);
                    }
                }
                result.Add(url);
                return(result);
            }
        }
Example #2
0
        /// <summary>
        /// Generates the URLs for a dynamically registered set of files (non pre-defined bundle)
        /// </summary>
        /// <param name="files"></param>
        /// <param name="fileType"></param>
        /// <param name="pipeline"></param>
        /// <param name="debug"></param>
        /// <returns></returns>
        private async Task <IEnumerable <string> > GenerateUrlsAsync(
            IEnumerable <IWebFile> files,
            WebFileType fileType,
            PreProcessPipeline pipeline = null,
            bool debug = false)
        {
            var result = new List <string>();

            var orderedFiles = _fileSetGenerator.GetOrderedFileSet(files, pipeline ?? _processorFactory.CreateDefault(fileType));

            if (debug)
            {
                return(orderedFiles.Select(x => x.FilePath));
            }

            var compression = _requestHelper.GetClientCompression(_httpContextAccessor.HttpContext.Request.Headers);

            //Get the file collection used to create the composite URLs and the external requests
            var fileBatches = _fileBatcher.GetCompositeFileCollectionForUrlGeneration(orderedFiles);

            var cacheBuster = _cacheBusterResolver.GetCacheBuster(_bundleManager.GetDefaultBundleOptions(debug).GetCacheBusterType());

            foreach (var batch in fileBatches)
            {
                //if it's external, the rule is that a WebFileBatch can only contain a single external file
                // it's path will be normalized as an external url so we just use it
                if (batch.IsExternal)
                {
                    result.Add(batch.Single().Original.FilePath);
                }
                else
                {
                    //Get the URLs for the batch, this could be more than one resulting URL depending on how many
                    // files are in the batch and the max url length
                    var compositeUrls = _urlManager.GetUrls(
                        batch.Select(x => x.Hashed),
                        fileType == WebFileType.Css ? ".css" : ".js",
                        cacheBuster);

                    foreach (var u in compositeUrls)
                    {
                        //now we need to determine if these files have already been minified
                        var compositeFilePath = _fileSystemHelper.GetCurrentCompositeFilePath(cacheBuster, compression, u.Key);
                        if (!File.Exists(compositeFilePath))
                        {
                            using (var bundleContext = BundleContext.CreateEmpty())
                            {
                                //need to process/minify these files - need to use their original paths of course
                                foreach (var file in batch.Select(x => x.Original))
                                {
                                    await _preProcessManager.ProcessAndCacheFileAsync(file, null, bundleContext);
                                }
                            }
                        }
                        result.Add(u.Url);
                    }
                }
            }

            return(result);
        }