static Html() { Cache = new SegmentArray <Html>(8); int urlIndex = AutoCSer.Web.Config.Search.HtmlPath.Length; foreach (FileInfo htmlFile in new DirectoryInfo(AutoCSer.Web.Config.Search.HtmlPath).GetFiles("*.html", SearchOption.AllDirectories)) { Html html = getHtml(htmlFile); if (html != null) { html.Url = htmlFile.FullName.Substring(urlIndex).Replace('\\', '/'); urls.Add(html.Url.FileNameToLower(), html); html.Id = Cache.GetIndex(html); Searcher.SearchTaskQueue.Add(new Queue.Append(new DataKey { Id = html.Id, Type = DataType.HtmlTitle }, html.Title)); Searcher.SearchTaskQueue.Add(new Queue.Append(new DataKey { Id = html.Id, Type = DataType.HtmlBodyText }, html.Text)); foreach (HtmlImage image in html.Images) { image.GetIndex(html.Id); Searcher.SearchTaskQueue.Add(new Queue.Append(new DataKey { Id = image.Id, Type = DataType.HtmlImage }, image.Title)); } } } htmlWatcher = new FileSystemWatcher(AutoCSer.Web.Config.Search.HtmlPath, "*.html"); htmlWatcher.IncludeSubdirectories = false; htmlWatcher.EnableRaisingEvents = true; htmlWatcher.Created += onCreatedHtml; htmlWatcher.Deleted += onDeleteHtml; }
/// <summary> /// 获取数组索引 /// </summary> /// <param name="htmlId">HTML 标识</param> internal void GetIndex(int htmlId) { HtmlId = htmlId; Monitor.Enter(freeImageLock); if (freeImageEnd == null) { Monitor.Exit(freeImageLock); Id = Cache.GetIndex(this); } else { Id = freeImageHead.Id; if (freeImageHead.HtmlId == 0) { freeImageEnd = null; } else { freeImageHead = Cache[freeImageHead.HtmlId]; } Monitor.Exit(freeImageLock); Cache[Id] = this; } }
/// <summary> /// 新建 HTML 文件处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void onCreatedHtml(object sender, FileSystemEventArgs e) { try { FileInfo htmlFile = new FileInfo(e.FullPath); Html newHtml = getHtml(htmlFile); if (newHtml != null) { Html html; string url = htmlFile.FullName.Substring(AutoCSer.Web.Config.Search.HtmlPath.Length).Replace('\\', '/'), lowerUrl = url.FileNameToLower(); Monitor.Enter(htmlLock); if (urls.TryGetValue(lowerUrl, out html) && html != Null) { Monitor.Exit(htmlLock); if (html.Title != newHtml.Title) { Searcher.SearchTaskQueue.Add(new Queue.Update(new DataKey { Type = DataType.HtmlTitle, Id = html.Id }, newHtml.Title, html.Title)); html.Title = newHtml.Title; } if (html.Text != newHtml.Text) { Searcher.SearchTaskQueue.Add(new Queue.Update(new DataKey { Type = DataType.HtmlBodyText, Id = html.Id }, newHtml.Text, html.Text)); html.Text = newHtml.Text; } HtmlImage.Free(ref html.Images); html.Images = newHtml.Images; } else { try { urls[lowerUrl] = newHtml; } finally { Monitor.Exit(htmlLock); } newHtml.Url = url; newHtml.Id = Cache.GetIndex(newHtml); Searcher.SearchTaskQueue.Add(new Queue.Append(new DataKey { Id = newHtml.Id, Type = DataType.HtmlTitle }, newHtml.Title)); Searcher.SearchTaskQueue.Add(new Queue.Append(new DataKey { Id = newHtml.Id, Type = DataType.HtmlBodyText }, newHtml.Text)); html = newHtml; } foreach (HtmlImage image in html.Images) { image.GetIndex(html.Id); Searcher.SearchTaskQueue.Add(new Queue.Append(new DataKey { Id = image.Id, Type = DataType.HtmlImage }, image.Title)); } } } catch (Exception error) { AutoCSer.Log.Pub.Log.Add(AutoCSer.Log.LogType.Error, error); } }