예제 #1
0
        /// <summary>
        /// This will return the cache file path for a given IWebFile depending on if it's being watched
        /// </summary>
        /// <param name="file"></param>
        /// <param name="fileWatchEnabled"></param>
        /// <param name="extension"></param>
        /// <param name="cacheBuster"></param>
        /// <param name="fileInfo">
        /// A getter to the underlying IFileInfo, this is lazy because when file watching is not enabled we do not want to resolve
        /// this if the cache file already exists
        /// </param>
        /// <returns></returns>
        public string GetCacheFilePath(IWebFile file, bool fileWatchEnabled, string extension, ICacheBuster cacheBuster, out Lazy <IFileInfo> fileInfo)
        {
            string cacheDir;
            string cacheFile;

            if (fileWatchEnabled)
            {
                //When file watching, the file path will be different since we'll hash twice:
                // * Hash normally, since this will be a static hash of the file name
                // * Hash with timestamp since this will be how we change it
                // This allows us to lookup the file's folder to store it's timestamped processed files

                var fi = GetFileInfo(file);

                //get the file hash without the extension
                var fileHash        = GetFileHash(file, string.Empty);
                var timestampedHash = GetFileHash(file, fi, extension);

                cacheDir  = Path.Combine(CurrentCacheFolder, cacheBuster.GetValue(), fileHash);
                cacheFile = Path.Combine(cacheDir, timestampedHash);
                fileInfo  = new Lazy <IFileInfo>(() => fi, LazyThreadSafetyMode.None);
            }
            else
            {
                var fileHash = GetFileHash(file, extension);

                cacheDir  = Path.Combine(CurrentCacheFolder, cacheBuster.GetValue());
                cacheFile = Path.Combine(cacheDir, fileHash);
                fileInfo  = new Lazy <IFileInfo>(() => GetFileInfo(file), LazyThreadSafetyMode.None);
            }

            //ensure the folder exists
            Directory.CreateDirectory(cacheDir);

            return(cacheFile);
        }
예제 #2
0
        public string GetUrl(string bundleName, string fileExtension, bool debug, ICacheBuster cacheBuster)
        {
            if (cacheBuster == null)
            {
                throw new ArgumentNullException(nameof(cacheBuster));
            }

            const string handler = "~/{0}/{1}{2}.{3}{4}";

            return(_requestHelper.Content(
                       string.Format(
                           handler,
                           _options.BundleFilePath,
                           Uri.EscapeUriString(bundleName),
                           fileExtension,
                           debug ? 'd' : 'v',
                           cacheBuster.GetValue())));
        }
예제 #3
0
 /// <summary>
 /// Returns the cache folder for composite files for the current compression supported
 /// </summary>
 /// <returns></returns>
 public string GetCurrentCompositeFolder(ICacheBuster cacheBuster, CompressionType type)
 {
     return(Path.Combine(CurrentCacheFolder, cacheBuster.GetValue(), type.ToString()));
 }
예제 #4
0
        public IEnumerable <FileSetUrl> GetUrls(IEnumerable <IWebFile> dependencies, string fileExtension, ICacheBuster cacheBuster)
        {
            if (cacheBuster == null)
            {
                throw new ArgumentNullException(nameof(cacheBuster));
            }

            var files            = new List <FileSetUrl>();
            var currBuilder      = new StringBuilder();
            var delimitedBuilder = new StringBuilder();
            var builderCount     = 1;

            var remaining = new Queue <IWebFile>(dependencies);

            while (remaining.Any())
            {
                var current = remaining.Peek();

                //add the normal file path (generally this would already be hashed)
                delimitedBuilder.Append(current.FilePath.TrimExtension(fileExtension).EnsureEndsWith('.'));

                //test if the current string exceeds the max length, if so we need to split
                if ((delimitedBuilder.Length
                     + _options.CompositeFilePath.Length
                     + fileExtension.Length
                     + cacheBuster.GetValue().Length
                     //this number deals with slashes, etc...
                     + 10)
                    >= (_options.MaxUrlLength))
                {
                    //we need to do a check here, this is the first one and it's already exceeded the max length we cannot continue
                    if (currBuilder.Length == 0)
                    {
                        throw new InvalidOperationException("The path for the single dependency: '" + current.FilePath.TrimExtension(fileExtension) + "' exceeds the max length (" + _options.MaxUrlLength + "), either reduce the single dependency's path length or increase the MaxHandlerUrlLength value");
                    }

                    //flush the current output to the array
                    var output = currBuilder.ToString().TrimEnd('.');
                    files.Add(new FileSetUrl
                    {
                        Key = _hasher.Hash(output),
                        Url = GetCompositeUrl(output, fileExtension, cacheBuster)
                    });
                    //create some new output
                    currBuilder      = new StringBuilder();
                    delimitedBuilder = new StringBuilder();
                    builderCount++;
                }
                else
                {
                    //update the normal builder
                    currBuilder.Append(current.FilePath.TrimExtension(fileExtension).EnsureEndsWith('.'));
                    //remove from the queue
                    remaining.Dequeue();
                }
            }

            if (builderCount > files.Count)
            {
                //flush the remaining output to the array
                var output = currBuilder.ToString().TrimEnd('.');
                files.Add(new FileSetUrl
                {
                    Key = _hasher.Hash(output),
                    Url = GetCompositeUrl(output, fileExtension, cacheBuster)
                });
            }

            return(files.ToArray());
        }