Exemplo n.º 1
0
        private string GetBundle(string[] inputFiles, string type)
        {
            var bundleFileProcessor = new BundleFileProcessor();
            var bundle = new BundlerMinifier.Bundle();
            //we wil store the file names with date of modification to keep track whether a new bundle should be generated or not
            //any minor modification will cause regeneration of the bundle
            //we do this because it is a performance intensive operation and the we should only do it when needed

            var distinctFiles      = inputFiles.Distinct().ToList();
            var fileNamesWithDates = new List <string>();

            foreach (var inputFile in distinctFiles)
            {
                //the input file can either be 1. theme-css 2. plugin-css 3. admin-css 4. common-css
                //check them one by one
                var file = ServerHelper.MapPath("~/Content/Themes" + inputFile);
                if (!_localFileProvider.FileExists(file))
                {
                    //plugins
                    file = ServerHelper.MapPath("~/" + inputFile);
                    if (!_localFileProvider.FileExists(file))
                    {
                        //administration & common
                        file = ServerHelper.MapPath("~/" + inputFile, true);
                        if (!_localFileProvider.FileExists(file))
                        {
                            continue; //can't do anything...the file doesn't exist
                        }
                    }
                }
                bundle.InputFiles.Add(file);
                var modDate = _localFileProvider.GetLastModifiedDateTime(file);
                fileNamesWithDates.Add($"{file}:{modDate}");
            }

            var outputFilePart1  = GetOutputFileName(fileNamesWithDates);
            var outputFilePart2  = GetOutputFileName(distinctFiles);
            var outputFile       = outputFilePart1 + "_" + outputFilePart2;
            var bundlesDirectory = ServerHelper.MapPath(ApplicationConfig.BundlesDirectory, true);
            var bundleFileName   = bundlesDirectory + "/" + outputFile + $".min.{type}";

            bundle.OutputFileName = bundleFileName;
            //do we need to generate the bundle?
            if (!_localFileProvider.FileExists(bundleFileName))
            {
                //delete the existing bundles of these files
                try
                {
                    _localFileProvider.DeleteFiles(bundlesDirectory, $"*_{outputFilePart2}.min.{type}*");
                }
                catch
                {
                    //do nothing...the file must be locked...will try next time
                }
                bundle.FileName = bundleFileName + ".json";
                BundleMinifier.ErrorMinifyingFile += BundleMinifier_ErrorMinifyingFile;
                bundleFileProcessor.Process(bundle.FileName, new List <BundlerMinifier.Bundle>()
                {
                    bundle
                });
            }
            //if operation succeeded, return the url, else null
            if (bundle.OutputFileName == null)
            {
                return(null);
            }
            //also create a gzipped version as well
            using (var bundleFileStream = File.OpenRead(bundleFileName))
                using (var compressedFileStream = File.Create(bundleFileName + ".gz"))
                {
                    using (var compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
                    {
                        bundleFileStream.CopyTo(compressionStream);
                    }
                }
            return(bundle.OutputFileName == null ? null : ApplicationEngine.MapUrl(bundleFileName));
        }