public string GetAddr(CdnResource res) { if (res == null) { return(null); } return(res.GetAddr(this.UseCdn, this.UseDev)); }
public void AddResource(string name, CdnResource res, string reqs) { if (!string.IsNullOrWhiteSpace(reqs)) // "data-req" { res.Requires = new List <CdnResource>(); foreach (string dep in reqs.Split()) { var resDep = FindResource(dep, false); Debug.Assert(resDep != null); res.Requires.Add(resDep); } } Debug.Assert(!Resources.ContainsKey(name)); Resources.Add(name, res); if (name != res.fallback_src) { Debug.Assert(!Resources.ContainsKey(res.fallback_src)); Resources.Add(res.fallback_src, res); } // Add the Cdn Hosts to our list. if (res.CdnPath1 != null) { // add CDN Host info. string host = UrlUtil.GetHostName(res.CdnPath1); CdnHost host1; if (!Hosts.TryGetValue(host, out host1)) { host1 = new CdnHost(host); Hosts.Add(host, host1); } res.CdnHost1 = host1; } }
public async Task <int> InitCdnAsync(string cdnAllFilePath, string outDir) { // Make sure all my (local copy) CDN based resources are up to date. // Called ONCE at startup to read cdnAllFilePath. // 1. Read the HTML/XML cdnAllFilePath file. // 2. Pull all files from the CDN that we want locally as backups/fallback. // 3. Write out the local stuff to outDir. e.g. "wwwroot/cdn" if (!File.Exists(cdnAllFilePath)) // get my list from here. { return(0); } int downloadCount = 0; int fileCount = 0; XDocument doc = XDocument.Load(cdnAllFilePath); // TODO: Use HTML agility pack to deal with proper HTML (Not XML) encoding?? var tasks = new List <Task <CdnRet> >(); // allow background/parallel loading. // pull all 'a', 'link' and 'script' elements foreach (XNode node in doc.DescendantNodes()) { if (node.NodeType != XmlNodeType.Element) { continue; } var xl = node as XElement; if (!CdnResource.IsUsedElement(xl)) { continue; } var res = new CdnResource(xl); AddResource(res.name, res, xl.Attribute("data-req")?.Value); tasks.Add(res.SyncElement(outDir)); fileCount++; if (tasks.Count < kMaxConcurrentUpdates) { continue; } await Task.WhenAny(tasks.ToArray()); // Do the work. for (int i = 0; i < tasks.Count; i++) { var task = tasks[i]; if (task.IsCompleted) { if (task.Result == CdnRet.Updated) { downloadCount++; } tasks.RemoveAt(i); // done. i--; } } Debug.Assert(tasks.Count < kMaxConcurrentUpdates); } await Task.WhenAll(tasks.ToArray()); // do this in parallel. foreach (var task in tasks) { Debug.Assert(task.IsCompleted); if (task.Result == CdnRet.Updated) { downloadCount++; } } return(downloadCount); }