Exemplo n.º 1
0
        public static JArray CheckForUpdates(JArray Softwares)
        {
            JArray jResult = new JArray();

            foreach (JObject jObj in Softwares)
            {
                bool bFound = false;
                try
                {
                    string manufacturer   = Base.clean(jObj["Manufacturer"].ToString().ToLower());
                    string productname    = Base.clean(jObj["ProductName"].ToString().ToLower());
                    string productversion = Base.clean(jObj["ProductVersion"].ToString().ToLower());

                    string sID = Hash.CalculateMD5HashString((manufacturer + productname + productversion).Trim());

                    JObject oRes = new JObject();
                    string  sRes = "";
                    //Try to get value from Memory
                    if (_cache.TryGetValue("noupd-" + sID, out sRes))
                    {
                        continue;
                    }

                    //Try to get value from Memory
                    if (_cache.TryGetValue("upd-" + sID, out oRes))
                    {
                        jResult.Add(oRes);
                        continue;
                    }

                    string shortname = GetShortname(productname, productversion, manufacturer);

                    #region compare versions
                    if (!string.IsNullOrEmpty(shortname))
                    {
                        JArray oCat = GetCatalog("", false);

                        var jobj = oCat.SelectTokens("[*].ShortName").Where(t => t.ToString().ToLower() == shortname.ToLower());
                        if (jobj.FirstOrDefault() == null)
                        {
                            var cacheEntryOptions2 = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(900)); //cache result for 15min
                            _cache.Set("noupd-" + sID, "no", cacheEntryOptions2);
                            continue;
                        }

                        JObject jSW = jobj.FirstOrDefault().Parent.Parent as JObject;

                        try
                        {
                            string sRZVersion = jSW["ProductVersion"].ToString();

                            if (!string.IsNullOrEmpty(sRZVersion))
                            {
                                if (productversion == sRZVersion) //same version...
                                {
                                    continue;
                                }
                            }

                            try
                            {
                                if (Version.Parse(productversion) > Version.Parse(sRZVersion)) //version is newer
                                {
                                    Base.SetShortname(jSW["ProductName"].Value <string>(), productversion, jSW["Manufacturer"].Value <string>(), jSW["ShortName"].Value <string>());
                                    Base.StoreFeedback(jSW["ProductName"].Value <string>(), productversion, jSW["Manufacturer"].Value <string>(), jSW["ShortName"].Value <string>(), "NEW Version ?!", "RZ", true);
                                    continue;
                                }
                                if (Version.Parse(productversion) == Version.Parse(sRZVersion)) //version is  same
                                {
                                    continue;
                                }
                            }
                            catch
                            {
                                try
                                {
                                    if (string.Compare(productversion, sRZVersion, true) >= 1)
                                    {
                                        Base.SetShortname(jSW["ProductName"].Value <string>(), productversion, jSW["Manufacturer"].Value <string>(), jSW["ShortName"].Value <string>());
                                        Base.StoreFeedback(jSW["ProductName"].Value <string>(), productversion, jSW["Manufacturer"].Value <string>(), jSW["ShortName"].Value <string>(), "NEW Version ?!", "RZ", true);
                                        continue;
                                    }
                                }
                                catch { }
                            }



                            JObject oCatItem = new JObject();
                            oCatItem.Add("ShortName", jSW["ShortName"]);
                            oCatItem.Add("Description", jSW["Description"]);
                            oCatItem.Add("Manufacturer", jSW["Manufacturer"]);
                            oCatItem.Add("ProductName", jSW["ProductName"]);
                            oCatItem.Add("ProductVersion", jSW["ProductVersion"]);
                            oCatItem.Add("ProductURL", jSW["ProductURL"]);
                            oCatItem.Add("MSIProductID", productversion); //to show the old version in RuckZuck.exe


                            if (jSW["Downloads"] == null)
                            {
                                oCatItem.Add("Downloads", 0);
                            }
                            else
                            {
                                oCatItem.Add("Downloads", jSW["Downloads"]);
                            }

                            if (jSW["SWId"] != null)
                            {
                                if (!string.IsNullOrEmpty(jSW["SWId"].ToString()))
                                {
                                    oCatItem.Add("IconId", jSW["SWId"].Value <Int32>()); //for old older Versions only...
                                    oCatItem.Add("SWId", jSW["SWId"].Value <Int32>());
                                }
                            }

                            if (!string.IsNullOrEmpty(jSW["IconHash"].ToString()))
                            {
                                oCatItem.Add("IconHash", jSW["IconHash"].ToString());
                            }

                            //Cache result
                            var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(930)); //cache result for 15min
                            _cache.Set("upd-" + sID, oCatItem, cacheEntryOptions);

                            jResult.Add(oCatItem);
                            bFound = true;
                            continue;
                        }
                        catch { }

                        //JArray jItems = GetSoftwares(shortname);
                        //foreach (JObject jSW in jItems)
                        //{

                        //}
                    }
                    else
                    {
                        if (shortname == null) //if shortname = ""; it's in SWLookup but without a shortname so we dont need to store it...
                        {
                            ThreadPool.QueueUserWorkItem(s =>
                            {
                                //Add Item to Catalog
                                SetShortname(productname, productversion, manufacturer, ""); //No Shortname
                            });
                        }
                    }
                    #endregion

                    if (!bFound)
                    {
                        var cacheEntryOptions2 = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(900)); //cache result for 15min
                        _cache.Set("noupd-" + sID, "no", cacheEntryOptions2);
                    }
                }
                catch (Exception ex)
                {
                    jObj.ToString();
                    ex.Message.ToString();
                }
            }

            return(jResult);
        }