Exemplo n.º 1
0
        public static async Task <IActionResult> GetSoftwares([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log)
        {
            string customerid = req.Query["customerid"];

            customerid = customerid ?? "";
            string shortname = req.Query["shortname"];

            shortname = shortname ?? "";
            string name = req.Query["name"];

            name = name ?? "";
            string ver = req.Query["ver"];

            ver = ver ?? "";
            string man = req.Query["man"];

            man = man ?? "";

            bool image = false;

            bool.TryParse(req.Query["image"], out image);

            string ClientIP = req.HttpContext.Connection.RemoteIpAddress.ToString();


            //if (ClientIP == "88.157.220.241" && string.IsNullOrEmpty(customerid))
            //    return Content("[]");

            if (string.IsNullOrEmpty(Base.localURL))
            {
                Base.localURL = req.GetEncodedUrl().ToLower().Split("/rest/v2/getsoftwares")[0];
                Base.WriteLog($"Set localURL: {Base.localURL}", ClientIP, 1000, customerid);
            }



            JArray jSW;

            if (!string.IsNullOrEmpty(shortname))
            {
                if (!Base.ValidateIP(ClientIP))
                {
                    if (Environment.GetEnvironmentVariable("EnforceGetURL") == "true")
                    {
                        return(new OkObjectResult("[]"));
                    }
                }
                else
                {
                    Base.WriteLog($"Get Definition for: {shortname}", ClientIP, 1500, customerid);
                }

                jSW = Base.GetSoftwares(shortname, customerid);
            }
            else
            {
                if (!Base.ValidateIP(ClientIP))
                {
                    if (Environment.GetEnvironmentVariable("EnforceGetURL") == "true")
                    {
                        return(new OkObjectResult("[]"));
                    }
                }
                else
                {
                    Base.WriteLog($"Get Definition for: {name}", ClientIP, 1500, customerid);
                }

                jSW = Base.GetSoftwares(name, ver, man, customerid);
            }
            //Cleanup
            foreach (JObject jObj in jSW)
            {
                try
                {
                    if (jObj["IconHash"] != null)
                    {
                        //Get SWId from Catalog if missing
                        if (string.IsNullOrEmpty(jObj["IconHash"].ToString()))
                        {
                            try
                            {
                                jObj["IconHash"] = Base.GetCatalog().SelectToken("$..[?(@.ShortName =='" + jObj["ShortName"] + "')]")["IconHash"];
                            }
                            catch { }
                        }
                    }

                    //generate IconURL if missing
                    if (jObj["IconURL"] == null)
                    {
                        if (jObj["IconHash"] != null)
                        {
                            jObj.Add("IconURL", Base.localURL + "/rest/v2/geticon?iconhash=" + jObj["IconHash"].ToString());
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(jObj["IconURL"].ToString()) && jObj["IconHash"] != null)
                        {
                            //switch to cdn for icons
                            string sBase = Base.localURL;
                            if (sBase.ToLower().StartsWith("https://ruckzuck.tools"))
                            {
                                sBase = "https://cdn.ruckzuck.tools";
                            }

                            jObj["IconURL"] = Base.localURL + "/rest/v2/geticon?iconhash=" + jObj["IconHash"].ToString();
                        }
                    }

                    if (jObj["IconId"] != null)
                    {
                        jObj.Remove("IconId"); //No IconID on V2!! only SWId
                    }

                    //rename Shortname to ShortName on V2
                    if (jObj["Shortname"] != null)
                    {
                        string sShortName = jObj["Shortname"].ToString();

                        jObj.Remove("Shortname");

                        if (jObj["ShortName"] == null)
                        {
                            jObj.Add("ShortName", sShortName);
                        }
                    }

                    if (jObj["SWId"] != null)
                    {
                        //Get SWId from Catalog if missing
                        if (jObj["SWId"].ToString() == "0")
                        {
                            try
                            {
                                jObj["SWId"] = Base.GetCatalog().SelectToken("$..[?(@.ShortName =='" + jObj["ShortName"] + "')]")["SWId"];
                            }
                            catch { }
                        }
                    }

                    //remove Image if not requested to reduce size
                    if (!image)
                    {
                        try
                        {
                            if (jObj["Image"] != null)
                            {
                                jObj.Remove("Image");
                            }
                        }
                        catch { }
                    }

                    //remove Author as there are no RuckZuck users anymore
                    if (jObj["Author"] != null)
                    {
                        jObj.Remove("Author");
                    }
                }
                catch { }
            }

            if (jSW != null)
            {
                return(new OkObjectResult(jSW));
            }
            else
            {
                return(new OkObjectResult("{[]}")); //return empty json array
            }
        }