private async Task ProcessFile(IWebFile file, string extension) { //check if it's in cache //TODO: If we make the hash as part of the last write time of the file, then the hash will be different // which means it will be a new cached file which means we can have auto-changing of files. Versioning // will still be manual but that would just be up to the client cache, not the server cache. But, // before we do that we need to consider performance because this means that for every file that is hashed // we'd need to lookup it's last write time so that all hashes match which isn't really ideal. //var filePath = _fileSystemHelper.MapPath(file.FilePath); //var lastWrite = File.GetLastWriteTimeUtc(filePath); //var hashName = _hasher.Hash(file.FilePath + lastWrite) + extension; var hashName = _hasher.Hash(file.FilePath) + extension; var cacheDir = _fileSystemHelper.CurrentCacheFolder; var cacheFile = Path.Combine(cacheDir, hashName); Directory.CreateDirectory(cacheDir); if (!File.Exists(cacheFile)) { var filePath = _fileSystemHelper.MapPath(file.FilePath); var contents = await _fileSystemHelper.ReadContentsAsync(filePath); //process the file var processed = await file.Pipeline.ProcessAsync(new FileProcessContext(contents, file)); //save it to the cache path await _fileSystemHelper.WriteContentsAsync(cacheFile, processed); } }
/// <summary> /// If the current asset/request requires minification, this will check the cache for its existence, if it doesn't /// exist, it will process it and store the cache file. Lastly, it sets the file path for the JavaScript file. /// </summary> /// <param name="file"></param> /// <returns></returns> public async Task ProcessAndCacheFileAsync(IWebFile file) { if (file == null) throw new ArgumentNullException(nameof(file)); if (file.Pipeline == null) throw new ArgumentNullException(string.Format("{0}.Pipeline", nameof(file))); switch (file.DependencyType) { case WebFileType.Js: await ProcessJsFile(file); break; case WebFileType.Css: await ProcessCssFile(file); break; } }
private async Task ProcessFile(IWebFile file, string extension) { //If Its external throw an exception this is not allowed. if (file.FilePath.Contains(Constants.SchemeDelimiter)) { throw new InvalidOperationException("Cannot process an external file as part of a bundle"); }; //check if it's in cache //TODO: If we make the hash as part of the last write time of the file, then the hash will be different // which means it will be a new cached file which means we can have auto-changing of files. Versioning // will still be manual but that would just be up to the client cache, not the server cache. But, // before we do that we need to consider performance because this means that for every file that is hashed // we'd need to lookup it's last write time so that all hashes match which isn't really ideal. //var filePath = _fileSystemHelper.MapPath(file.FilePath); //var lastWrite = File.GetLastWriteTimeUtc(filePath); //var hashName = _hasher.Hash(file.FilePath + lastWrite) + extension; var hashName = _hasher.Hash(file.FilePath) + extension; var cacheDir = _fileSystemHelper.CurrentCacheFolder; var cacheFile = Path.Combine(cacheDir, hashName); Directory.CreateDirectory(cacheDir); if (!File.Exists(cacheFile)) { var filePath = _fileSystemHelper.MapPath(file.FilePath); //doesn't exist, throw as thsi shouldn't happen if (File.Exists(filePath) == false) throw new FileNotFoundException("No file found with path " + filePath); var contents = await _fileSystemHelper.ReadContentsAsync(filePath); //process the file var processed = await file.Pipeline.ProcessAsync(new FileProcessContext(contents, file)); //save it to the cache path await _fileSystemHelper.WriteContentsAsync(cacheFile, processed); } }
/// <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); }
/// <summary> /// Saves specified file to storage /// </summary> /// <param name="file">File to save</param> /// <param name="isRootFile">Specifies if this is root file (requested html page)</param> /// <returns>New location of file</returns> public string SaveFile(IWebFile file, bool isRootFile) { var fileUrl = file.Url.ToString(); if (file.ContentType.Contains("text")) { if (IsValidTextData(file.Text)) { if (isRootFile) { _builder.PrependTextData(file.Text, file.ContentType, fileUrl, file.TextEncoding); } else { _builder.AppendTextData(file.Text, file.ContentType, fileUrl, file.TextEncoding); } } } else { _builder.AppendBinaryData(file.Bytes, fileUrl, file.ContentType); } return(fileUrl); }
public static IWebFile Copy(this IWebFile orig, string newPath) { return(orig.Duplicate(newPath)); }
private async Task ProcessJsFile(IWebFile file) { await ProcessFile(file, ".js"); }
/// <summary> /// Initializes a new instance of the class. /// </summary> /// <param name="updatedHtml">The updated HTML.</param> /// <param name="sourceFile">The source file.</param> internal GenericWebFile(string updatedHtml, IWebFile sourceFile) { _updatedHtml = updatedHtml; _sourceFile = sourceFile; }
/// <summary> /// Returns a file's hash /// </summary> /// <param name="file"></param> /// <param name="extension"></param> /// <returns></returns> public string GetFileHash(IWebFile file, string extension) { var hashName = _hasher.Hash(file.FilePath) + extension; return(hashName); }
public FileProcessContext(string fileContent, IWebFile webFile) { FileContent = fileContent; WebFile = webFile; }
/// <inheritdoc /> public string Content(IWebFile file) => _wrappedRequestHelper.Content(file);
private void ValidateFile(IWebFile file) { if (file.Order < 0) { throw new NotSupportedException("The Order of a web file cannot be less than zero"); } }
/// <summary>Initializes a new instance of the <see cref="T:System.Object" /> class.</summary> public WatchedFile(IWebFile webFile, IFileInfo fileInfo, BundleOptions bundleOptions) { WebFile = webFile; FileInfo = fileInfo; BundleOptions = bundleOptions; }
/// <summary> /// Saves the file. /// </summary> /// <param name="file">The file.</param> /// <param name="isRootFile">if set to <c>true</c> [is root file].</param> protected virtual void SaveFile(IWebFile file, bool isRootFile) { FileStorage.SaveFile(file, isRootFile); }
public WebFilePair(IWebFile original, IWebFile hashed) { Original = original; Hashed = hashed; }
public IFileInfo GetFileInfo(IWebFile webfile) => GetFileInfo(webfile.FilePath);