/// <summary> /// Optimizes a list of files using a given optimizer /// </summary> /// <param name="optimizer">The optimizer to use.</param> /// <param name="file">The file to optimze.</param> /// <param name="outputPath">The path of the optimized file.</param> /// <param name="configFiles">The config files to compare against.</param> public OptimizedFile Optimize(string optimizer, IFile file, FilePath outputPath, IList <OptimizedFile> configFiles) { //Check Config OptimizedFile config = configFiles.FirstOrDefault(o => o.Path.FullPath.ToLower() == file.Path.FullPath.ToLower()); OptimizedFile optimizedFile = null; string hash = this.GetHash(file); try { if ((config == null) || config.RequiresOptimization(hash) || config.DifferentService(optimizer)) { //Optimize ImageOptimizerResult result = _OptimizerFactory.Optimize(optimizer, file.Path, outputPath); if ((result != null) && !result.HasError) { //Optimzed optimizedFile = new OptimizedFile(file.Path, result.Service, result.ModifiedDate, hash, result.SizeBefore, result.SizeAfter); _ImagesOptimized.Add(file.Path.FullPath); _SizeBefore += result.SizeBefore; _SizeAfter += result.SizeAfter; _Log.Information("Optimized: " + file.Path + " - Saved: " + result.SavedSize + " bytes (" + result.SavedPercent.ToString("N0") + "%)"); } else if ((result.ErrorMessage == "Unsupported File") || (result.ErrorMessage == "Invalid FileSize") || (result.ErrorMessage == "Matching FileSize")) { //Skipped _ImagesSkipped++; _SizeBefore += file.Length; _SizeAfter += file.Length; // Copy to destination if (file.Path.FullPath.ToLower() != outputPath.FullPath.ToLower()) { IDirectory parent = _FileSystem.GetDirectory(outputPath.GetDirectory()); if (!parent.Exists) { parent.Create(); } file.Copy(outputPath, true); } _Log.Information("Skipped: " + file.Path + " - " + result.ErrorMessage); } else { //Errored _ImagesErrored++; _Log.Information("Errored: " + file.Path + " - " + result.ErrorMessage); } } else { //Skipped _ImagesSkipped++; if (config != null) { optimizedFile = new OptimizedFile(file.Path, config.Service, config.OptimizedDate, hash, config.SizeBefore, config.SizeAfter); _SizeBefore += config.SizeBefore; _SizeAfter += config.SizeAfter; } else { _SizeBefore += file.Length; _SizeAfter += file.Length; } _Log.Information("Skipped: " + file.Path + " - Saved: " + config.SavedSize + " bytes (" + config.SavedPercent.ToString("N0") + "%)"); } } catch (Exception ex) { //Errored _ImagesErrored++; _Log.Information("Errored: " + file.Path + " - " + ex.Message); } this.OnProgress(optimizedFile); return(optimizedFile); }
/// <summary> /// Get an optimizer from the factory /// </summary> /// <param name="optimizer">The name of the optimizer.</param> /// <param name="sourcePath">The path of the file to optimize.</param> /// <param name="outputPath">The output path for the optimized image.</param> public ImageOptimizerResult Optimize(string optimizer, FilePath sourcePath, FilePath outputPath) { //Get Optimizer IImageOptimizer optim = null; if (!String.IsNullOrEmpty(optimizer)) { optim = this.GetByName(optimizer); } if (optim == null) { optim = this.GetByPath(sourcePath); } //Get Result ImageOptimizerResult result = null; if (optim != null) { try { //Optimize IFile file = _FileSystem.GetFile(sourcePath); if ((optim.FileSize == 0) || (file.Length < optim.FileSize)) { result = optim.Optimize(sourcePath); //Check Sizes if (result.SizeBefore == 0) { result.SizeBefore = file.Length; result.SizeAfter = result.SizeBefore; } //Replace File if (result.SizeAfter != result.SizeBefore) { Directory.CreateDirectory(outputPath.GetDirectory().FullPath); using (WebClient client = new WebClient()) { client.DownloadFile(result.DownloadUrl, outputPath.FullPath); } //Date Modified result.ModifiedDate = new FileInfo(file.Path.FullPath).LastWriteTime; } else { //Skipped result = new ImageOptimizerResult(optim.Name, sourcePath, "Matching FileSize"); } } else { //Skipped result = new ImageOptimizerResult(optim.Name, sourcePath, "Invalid FileSize"); } } catch (Exception ex) { //Error result = new ImageOptimizerResult(optim.Name, sourcePath, ex.Message); } } else { //Unsupported File result = new ImageOptimizerResult(optimizer, sourcePath, "Unsupported File"); } return(result); }