Пример #1
0
        private async Task <IEnumerable <string> > GenerateUrlsAsync(
            IEnumerable <IWebFile> files,
            WebFileType fileType,
            PreProcessPipeline pipeline = null)
        {
            var result = new List <string>();

            if (_config.IsDebug)
            {
                return(GenerateUrlsDebug(files));
            }
            else
            {
                if (pipeline == null)
                {
                    pipeline = _processorFactory.GetDefault(fileType);
                }

                var compression = _request.GetClientCompression();

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

                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 = _context.UrlCreator.GetUrls(batch.Select(x => x.Hashed), fileType == WebFileType.Css ? ".css" : ".js");

                        foreach (var u in compositeUrls)
                        {
                            //now we need to determine if these files have already been minified
                            var compositeFilePath = _fileSystemHelper.GetCurrentCompositeFilePath(compression, u.Key);
                            if (!File.Exists(compositeFilePath))
                            {
                                //need to process/minify these files - need to use their original paths of course
                                await ProcessWebFilesAsync(batch.Select(x => x.Original), pipeline);
                            }

                            result.Add(u.Url);
                        }
                    }
                }
            }

            return(result);
        }
Пример #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);
        }