Skip to content

Commit

Permalink
Fix: compatibility with new V2 REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
rzander committed Jan 27, 2019
1 parent 122a074 commit efc4a7d
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 151 deletions.
6 changes: 3 additions & 3 deletions RZ.OneGetProvider/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Zander Tools")]
[assembly: AssemblyProduct("RuckZuck OneGet provider")]
[assembly: AssemblyCopyright("Copyright © 2018 by Roger Zander")]
[assembly: AssemblyCopyright("Copyright © 2019 by Roger Zander")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -49,5 +49,5 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyVersion("1.6.2.11")]
[assembly: AssemblyFileVersion("1.6.2.11")]
[assembly: AssemblyVersion("1.6.2.13")]
[assembly: AssemblyFileVersion("1.6.2.13")]
71 changes: 54 additions & 17 deletions RZ.OneGetProvider/RZRestAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public static string sURL
{
if (sWebSVC.StartsWith("http", StringComparison.CurrentCultureIgnoreCase))
{
RZRestAPI._sURL = sWebSVC;
RZRestAPI._sURL = sWebSVC.TrimEnd('/');
}
}

if (_sURL == "UDP" & !DisableBroadcast)
if (_sURL == "UDP" && !DisableBroadcast)
{
try
{
Expand Down Expand Up @@ -147,7 +147,7 @@ public static List<GetSoftware> SWGet(string PackageName, string PackageVersion)
{
try
{
return SWResults("").Where(t => t.ProductName == PackageName & t.ProductVersion == PackageVersion).ToList();
return SWResults("").Where(t => t.ProductName == PackageName && t.ProductVersion == PackageVersion).ToList();
}
catch { }

Expand All @@ -158,7 +158,7 @@ public static List<GetSoftware> SWGet(string PackageName, string Manufacturer, s
{
try
{
return SWResults("").Where(t => t.ProductName == PackageName & t.ProductVersion == PackageVersion & t.Manufacturer == Manufacturer).ToList();
return SWResults("").Where(t => t.ProductName == PackageName && t.ProductVersion == PackageVersion && t.Manufacturer == Manufacturer).ToList();
}
catch { }

Expand Down Expand Up @@ -267,7 +267,7 @@ public static List<AddSoftware> CheckForUpdate(List<AddSoftware> lSoftware)
if (contentType == "application/json")
{
var response = oClient.PostAsync(sURL + "/rest/CheckForUpdate", oCont);
response.Wait(15000);
response.Wait(60000);
if (response.IsCompleted)
{
List<AddSoftware> lRes = ser.Deserialize<List<AddSoftware>>(response.Result.Content.ReadAsStringAsync().Result);
Expand Down Expand Up @@ -309,15 +309,20 @@ public static bool UploadSWEntry(AddSoftware lSoftware)


//vNext 5.9.2017
public static async void TrackDownloads2(long SWId, string Architecture)
public static async void TrackDownloads2(long SWId, string Architecture, string Shortname = "")
{
try
{
await oClient.GetStringAsync(sURL + "/rest/TrackDownloadsNew?SWId=" + SWId.ToString() + "&arch=" + WebUtility.UrlEncode(Architecture));
string sID = SWId.ToString();
if (SWId == 0)
sID = "";

await oClient.GetStringAsync(sURL + "/rest/TrackDownloadsNew?SWId=" + sID + "&arch=" + WebUtility.UrlEncode(Architecture) + "&shortname=" + WebUtility.UrlEncode(Shortname));
}
catch { }
}


public static List<string> GetCategories(List<GetSoftware> oSWList)
{
List<string> lResult = new List<string>();
Expand All @@ -339,7 +344,8 @@ public static byte[] GetIcon(long SWId)
using (MemoryStream ms = new MemoryStream())
{
response.Result.CopyTo(ms);
return ms.ToArray();
byte[] bRes = ms.ToArray();
return bRes;
}
}

Expand Down Expand Up @@ -394,6 +400,10 @@ public class GetSoftware

public long IconId { get; set; }

public long SWId { get; set; }

public string IconHash { get; set; }

public bool isInstalled { get; set; }

//public string XMLFile { get; set; }
Expand All @@ -404,14 +414,25 @@ public string IconURL
{
get
{
if (IconId > 0)
//Support new V2 REST API
if (!string.IsNullOrEmpty(IconHash))
{
return RZRestAPI.sURL + "/rest/GetIcon?id=" + IconId.ToString();
return RZRestAPI.sURL + "/rest/v2/GetIcon?iconhash=" + IconHash;
}
else

if (SWId > 0)
{
return ""; // "File://" + IconFile;
return RZRestAPI.sURL + "/rest/GetIcon?id=" + SWId.ToString();
}

if (IconId > 0)
{
SWId = IconId;
return RZRestAPI.sURL + "/rest/GetIcon?id=" + SWId.ToString();
}

return "";

//return "https://ruckzuck.azurewebsites.net/wcf/RZService.svc/rest/GetIcon?id=" + IconId.ToString();
}
}
Expand Down Expand Up @@ -464,6 +485,9 @@ public class AddSoftware
//public long SWId { get { return IconId; } set { IconId = value; } }
public long SWId { get; set; }

public long IconId { get; set; }

public string IconHash { get; set; }
//remove if SWId is in place 5.9.2017
//public long IconId { get; set; }

Expand All @@ -476,6 +500,19 @@ public string IconURL
string sURL = RZRestAPI.sURL + "/rest/GetIcon?id=" + SWId.ToString();
return sURL;
}

//Support new V2 REST API
if (!string.IsNullOrEmpty(IconHash))
{
return RZRestAPI.sURL + "/rest/v2/GetIcon?iconhash=" + IconHash;
}

if (IconId > 0)
{
SWId = IconId;
string sURL = RZRestAPI.sURL + "/rest/GetIcon?id=" + SWId.ToString();
return sURL;
}
return "";
}
}
Expand Down Expand Up @@ -524,17 +561,17 @@ public string Status
{
if (string.IsNullOrEmpty(_status))
{
if (Installing & !Error)
if (Installing && !Error)
return "Installing";
if (Downloading & !Error)
if (Downloading && !Error)
return "Downloading";
if (Installed & !Error)
if (Installed && !Error)
return "Installed";
if (UnInstalled & !Error)
if (UnInstalled && !Error)
return "Uninstalled";
if (WaitingForDependency)
return "Installing dependencies";
if (PercentDownloaded == 100 & !Error)
if (PercentDownloaded == 100 && !Error)
return "Downloaded";
if (Error)
return ErrorMessage;
Expand Down
43 changes: 25 additions & 18 deletions RZ.OneGetProvider/RZScan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public RZScan(bool RunScan, bool CheckForUpdates)
{
bRunScan = true;
//SWScan();
GetSWRepository().ConfigureAwait(false);
GetSWRepository().ConfigureAwait(false); //Scan Runs when Repo is loaded
}

//Check every 60s
Expand Down Expand Up @@ -165,17 +165,18 @@ public async Task<bool> GetSWRepository()
{
SoftwareRepository = oDB.Select(item => new GetSoftware()
{
Categories = item.Categories.ToList(),
Categories = item.Categories ?? new List<string>(),
Description = item.Description,
Downloads = item.Downloads,
IconId = item.IconId,
SWId = item.SWId,
Image = item.Image,
Manufacturer = item.Manufacturer,
ProductName = item.ProductName,
ProductURL = item.ProductURL,
ProductVersion = item.ProductVersion,
Quality = item.Quality,
Shortname = item.Shortname
Shortname = item.Shortname,
IconHash = item.IconHash
}).ToList();
}
}
Expand Down Expand Up @@ -291,9 +292,13 @@ internal void _CheckUpdates(List<AddSoftware> aSWCheck)
var vSWCheck = aSWCheck.Select(t => new AddSoftware() { ProductName = t.ProductName, ProductVersion = t.ProductVersion, Manufacturer = t.Manufacturer }).ToList();

//we do not have to check for updates if it's in the Catalog
List<AddSoftware> tRes = vSWCheck.Where(t => SoftwareRepository.FirstOrDefault(r => r.ProductName == t.ProductName && r.ProductVersion == t.ProductVersion && r.Manufacturer == t.Manufacturer) == null).ToList();
//List<AddSoftware> tRes = vSWCheck.Where(t => SoftwareRepository.Count(r => r.ProductName.ToLower().Trim() == t.ProductName.ToLower().Trim() && r.ProductVersion.ToLower().Trim() == t.ProductVersion.ToLower().Trim() && r.Manufacturer.ToLower().Trim() == t.Manufacturer.ToLower().Trim()) == 0).ToList();
foreach (var oSW in SoftwareRepository)
{
vSWCheck.RemoveAll(t => t.ProductName.ToLower().Trim() == oSW.ProductName.ToLower().Trim() && t.Manufacturer.ToLower().Trim() == oSW.Manufacturer.ToLower().Trim() && t.ProductVersion.ToLower().Trim() == oSW.ProductVersion.ToLower().Trim());
}

List<AddSoftware> lCheckResult = RZRestAPI.CheckForUpdate(tRes).ToList();
List<AddSoftware> lCheckResult = RZRestAPI.CheckForUpdate(vSWCheck).ToList();

var lResult = lCheckResult.Select(item => new AddSoftware()
{
Expand All @@ -307,7 +312,9 @@ internal void _CheckUpdates(List<AddSoftware> aSWCheck)
ProductVersion = item.ProductVersion,
MSIProductID = item.MSIProductID,
Shortname = item.Shortname,
SWId = item.SWId
SWId = item.SWId,
IconId = item.IconId,
IconHash = item.IconHash
}).ToList();

//Only take updated Versions
Expand Down Expand Up @@ -655,17 +662,17 @@ internal static Bitmap GetImageFromExe(string Filename)

try
{
/*TsudaKageyu.IconExtractor iE = new TsudaKageyu.IconExtractor(Filename);
if (iE.FileName != null)
{
List<Icon> lIcons = TsudaKageyu.IconUtil.Split(iE.GetIcon(0)).ToList();
//Max Size 128px...
var ico = lIcons.Where(t => t.Height <= 128 && t.ToBitmap().PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb).OrderByDescending(t => t.Height).FirstOrDefault();
if (ico != null)
return ico.ToBitmap();
else
return bResult;
}*/
//TsudaKageyu.IconExtractor iE = new TsudaKageyu.IconExtractor(Filename);
//if (iE.FileName != null)
//{
// List<Icon> lIcons = TsudaKageyu.IconUtil.Split(iE.GetIcon(0)).ToList();
// //Max Size 128px...
// var ico = lIcons.Where(t => t.Height <= 128 && t.ToBitmap().PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb).OrderByDescending(t => t.Height).FirstOrDefault();
// if (ico != null)
// return ico.ToBitmap();
// else
// return bResult;
//}
}
catch { }

Expand Down
55 changes: 35 additions & 20 deletions RZ.OneGetProvider/RZUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,20 @@ internal static AddSoftware ParseJSON(string sFile)
try
{
JavaScriptSerializer ser = new JavaScriptSerializer();
AddSoftware lRes = ser.Deserialize<AddSoftware>(File.ReadAllText(sFile));
string sJson = File.ReadAllText(sFile);
AddSoftware lRes;

//Check if it's an Arrya (new in V2)
if (sJson.TrimStart().StartsWith("["))
{
List<AddSoftware> lItems = ser.Deserialize<List<AddSoftware>>(sJson);
lRes = lItems[0];
}
else
{
lRes = ser.Deserialize<AddSoftware>(sJson);
}

if (lRes.PreRequisites != null)
{
lRes.PreRequisites = lRes.PreRequisites.Where(x => !string.IsNullOrEmpty(x)).ToArray();
Expand Down Expand Up @@ -542,6 +555,7 @@ private bool _Download(bool Enforce, string DLPath)
{
foreach (var vFile in SW.Files)
{
bool bDLSuccess = false;
try
{
if (string.IsNullOrEmpty(vFile.URL))
Expand Down Expand Up @@ -629,20 +643,7 @@ private bool _Download(bool Enforce, string DLPath)
}
else
{

if (SendFeedback)
{
if (SW.SWId > 0)
{
RZRestAPI.TrackDownloads2(SW.SWId, SW.Architecture);
}
else
{
//Depreciated
//RZRestAPI.TrackDownloads(SW.ContentID);
}
}

bDLSuccess = true;
}

//Sleep 1s to complete
Expand Down Expand Up @@ -775,7 +776,14 @@ private bool _Download(bool Enforce, string DLPath)
Console.WriteLine("ERROR: " + ex.Message);
bError = true;
}

if (SendFeedback && bDLSuccess)
{
RZRestAPI.TrackDownloads2(SW.SWId, SW.Architecture, SW.Shortname);
}
}


}
else
{
Expand Down Expand Up @@ -865,10 +873,17 @@ public async Task<bool> Download(bool Enforce, string DLPath)
bool bAutoInstall = downloadTask.AutoInstall;
downloadTask = new DLTask() { ProductName = SW.ProductName, ProductVersion = SW.ProductVersion, Manufacturer = SW.Manufacturer, Shortname = SW.Shortname, Image = SW.Image, Files = SW.Files };

if (SW.PreRequisites.Length > 0)
if (SW.PreRequisites != null)
{
downloadTask.WaitingForDependency = true;
downloadTask.AutoInstall = false;
if (SW.PreRequisites.Length > 0)
{
downloadTask.WaitingForDependency = true;
downloadTask.AutoInstall = false;
}
else
{
downloadTask.AutoInstall = bAutoInstall;
}
}
else
{
Expand Down Expand Up @@ -971,7 +986,7 @@ private bool _Install(bool Force = false)
downloadTask.Installing = true;
ProgressDetails(this.downloadTask, EventArgs.Empty);

var oResult = _RunPS(psPath + SW.PSPreInstall + ";" + SW.PSInstall + ";" + SW.PSPostInstall + ";$ExitCode", "", new TimeSpan(0, 30, 0));
var oResult = _RunPS(psPath + SW.PSPreInstall + ";" + SW.PSInstall + ";" + SW.PSPostInstall + ";$ExitCode", "", new TimeSpan(0, 60, 0));

try
{
Expand Down Expand Up @@ -1631,7 +1646,7 @@ private bool _checkFileX509(string FilePath, string X509)
/// <returns></returns>
public static PSDataCollection<PSObject> _RunPS(string PSScript, string WorkingDir = "", TimeSpan? Timeout = null)
{
TimeSpan timeout = new TimeSpan(0, 5, 0); //default timeout = 5min
TimeSpan timeout = new TimeSpan(0, 15, 0); //default timeout = 15min

if (Timeout != null)
timeout = (TimeSpan)Timeout;
Expand Down
4 changes: 2 additions & 2 deletions RZUpdate/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Zander Tools")]
[assembly: AssemblyProduct("RZUpdate")]
[assembly: AssemblyCopyright("Copyright © 2018 by Roger Zander")]
[assembly: AssemblyCopyright("Copyright © 2019 by Roger Zander")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -33,4 +33,4 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.6.2.*")]
[assembly: AssemblyFileVersion("1.6.2.11")]
[assembly: AssemblyFileVersion("1.6.2.13")]
Loading

0 comments on commit efc4a7d

Please sign in to comment.