private static void ProcessQueue() { for (int i = _cache.Count - 1; i >= 0; i--) { var entry = _cache.ElementAt(i); // The file should be 1 second old before we start processing if (entry.Value > DateTime.Now.AddSeconds(-1)) { continue; } if (!_store.HasChangedOrIsNew(entry.Key)) { _cache.Remove(entry.Key); continue; } try { Minify(entry.Key); _store.Save(entry.Key); _cache.Remove(entry.Key); } catch (IOException) { // Do nothing, let's try again next time } catch { _cache.Remove(entry.Key); } } }
private static void WriteToLog(object sender, CompressionResult e) { ThreadPool.QueueUserWorkItem((o) => { _store.Save(e.OriginalFileName); if (e == null || e.ResultFileSize == 0) { return; } string name = new Uri(_folder).MakeRelativeUri(new Uri(e.OriginalFileName)).ToString(); _log.Write(DateTime.Now, name, e.OriginalFileSize, Math.Min(e.ResultFileSize, e.OriginalFileSize)); }); }
private static void WriteToLog(object sender, CompressionResult e) { ThreadPool.QueueUserWorkItem((o) => { _store.Save(e.OriginalFileName); if (e == null || e.ResultFileSize == 0) { return; } if (cmdLineOptions == null || !cmdLineOptions.SuppressCsvReport) { string name = new Uri(_directoryToOptimize).MakeRelativeUri(new Uri(e.OriginalFileName)).ToString(); var logItem = new LogItem { FileName = name, OriginalSizeBytes = e.OriginalFileSize, NewSizeBytes = e.ResultFileSize }; _logger.Write(logItem); } }); }
public void Minify(string path) { try { //if not changed - do nothing if (!FileHashStore.HasChangedOrIsNew(path)) { return; } long before; long after; using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var content = ReadFully(fs); before = content.LongLength; } if (path.EndsWith(".html", StringComparison.InvariantCultureIgnoreCase)) { MinifyHtml(path); } else if (path.EndsWith(".css", StringComparison.InvariantCultureIgnoreCase)) { MinifyCSS(path); } else if (path.EndsWith(".js", StringComparison.InvariantCultureIgnoreCase)) { MinifyJs(path); } else if (path.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase)) { MinifyImage(path); } else if (path.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase)) { MinifyImage(path); } else if (path.EndsWith(".jpeg", StringComparison.InvariantCultureIgnoreCase)) { MinifyImage(path); } else if (path.EndsWith(".gif", StringComparison.InvariantCultureIgnoreCase)) { MinifyImage(path); } var bytes = File.ReadAllBytes(path); after = bytes.LongLength; _logger.Write(new LogItem { FileName = path, OriginalSizeBytes = before, NewSizeBytes = after }); FileHashStore.Save(path); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine($"Minify:: Exception! {ex}"); } }