Skip to content

Commit

Permalink
Merge branch 'MIHO/masterV30_additions' of github.com:admin-shell-io/…
Browse files Browse the repository at this point in the history
…aasx-package-explorer into MIHO/masterV30_additions
  • Loading branch information
festo-i40 committed Dec 23, 2023
2 parents e6eaf85 + a9f48c2 commit 81695b0
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ sample-aasx/
/.vs/aasx-package-explorer/v16/.suo
/.vs/slnx.sqlite
/.vs/aasx-package-explorer/v17/.suo
/src/WpfXamlTool/Properties/AssemblyInfo.cs
/src/WpfMtpVisuViewer/Properties/AssemblyInfo.cs
/src/WpfMtpControl/Properties/AssemblyInfo.cs
/src/MsaglWpfControl/Properties/AssemblyInfo.cs
98 changes: 98 additions & 0 deletions src/AasxCsharpLibrary/AdminShellPackageEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ This source code may use other Open Source software components (see LICENSE.txt)

using Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using static AasCore.Aas3_0.Reporting;

namespace AdminShellNS
{
Expand Down Expand Up @@ -1155,8 +1159,102 @@ public bool IsLocalFile(string uriString)
return isLocal;
}

private static WebProxy proxy = null;

public Stream GetLocalStreamFromPackage(string uriString, FileMode mode = FileMode.Open, FileAccess access = FileAccess.Read)
{
// Check, if remote
if (uriString.ToLower().Substring(0, 4) == "http")
{
if (proxy == null)
{
string proxyAddress = "";
string username = "";
string password = "";

string proxyFile = "proxy.txt";
if (System.IO.File.Exists(proxyFile))
{
try
{ // Open the text file using a stream reader.
using (StreamReader sr = new StreamReader(proxyFile))
{
proxyFile = sr.ReadLine();
}
}
catch (IOException e)
{
Console.WriteLine("proxy.txt could not be read:");
Console.WriteLine(e.Message);
}
}

try
{
using (StreamReader sr = new StreamReader(proxyFile))
{
proxyAddress = sr.ReadLine();
username = sr.ReadLine();
password = sr.ReadLine();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(proxyFile + " not found!");
}

if (proxyAddress != "")
{
proxy = new WebProxy();
Uri newUri = new Uri(proxyAddress);
proxy.Address = newUri;
proxy.Credentials = new NetworkCredential(username, password);
Console.WriteLine("Using proxy: " + proxyAddress);
}
}

var handler = new HttpClientHandler();

if (proxy != null)
handler.Proxy = proxy;
else
handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials;
var hc = new HttpClient(handler);

var response = hc.GetAsync(uriString).GetAwaiter().GetResult();

// if you call response.EnsureSuccessStatusCode here it will throw an exception
if (response.StatusCode == HttpStatusCode.Moved
|| response.StatusCode == HttpStatusCode.Found)
{
var location = response.Headers.Location;
response = hc.GetAsync(location).GetAwaiter().GetResult();
}

response.EnsureSuccessStatusCode();
var s = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult();

if (s.Length < 500) // indirect load?
{
StreamReader reader = new StreamReader(s);
string json = reader.ReadToEnd();
var parsed = JObject.Parse(json);
try
{
string url = parsed.SelectToken("url").Value<string>();
response = hc.GetAsync(url).GetAwaiter().GetResult();
response.EnsureSuccessStatusCode();
s = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
}
catch
{
}
}

return s;
}

// access
if (_openPackage == null)
throw (new Exception(string.Format($"AASX Package {_fn} not opened. Aborting!")));
Expand Down
24 changes: 17 additions & 7 deletions src/AasxCsharpLibrary/Extensions/ExtendKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This source code may use other Open Source software components (see LICENSE.txt)
using AdminShellNS;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;

namespace Extensions
Expand All @@ -25,26 +26,32 @@ public static IKey CreateFrom(Reference r)
public static bool Matches(this IKey key,
KeyTypes type, string id, MatchMode matchMode = MatchMode.Strict)
{
key.Value = key.Value.Trim();
id = id.Trim();

if (matchMode == MatchMode.Strict)
return key.Type == type && key.Value == id;
return key.Type == type && key.Value.Replace("*01", "") == id.Replace("*01", "");

if (matchMode == MatchMode.Relaxed)
return (key.Type == type || key.Type == KeyTypes.GlobalReference || type == KeyTypes.GlobalReference)
&& key.Value == id;
&& key.Value.Replace("*01", "") == id.Replace("*01", "");

if (matchMode == MatchMode.Identification)
return key.Value == id;
return key.Value.Replace("*01", "") == id.Replace("*01", "");

return false;
}
public static bool Matches(this IKey key, IKey otherKey)
{
key.Value = key.Value.Trim();
otherKey.Value = otherKey.Value.Trim();

if (otherKey == null)
{
return false;
}

if (key.Type == otherKey.Type && key.Value.Equals(otherKey.Value))
if (key.Type == otherKey.Type && key.Value.Replace("*01", "").Equals(otherKey.Value.Replace("*01", "")))
{
return true;
}
Expand All @@ -54,15 +61,18 @@ public static bool Matches(this IKey key, IKey otherKey)

public static bool Matches(this IKey key, IKey otherKey, MatchMode matchMode = MatchMode.Strict)
{
key.Value = key.Value.Trim();
otherKey.Value = otherKey.Value.Trim();

if (matchMode == MatchMode.Strict)
return key.Type == otherKey.Type && key.Value == otherKey.Value;
return key.Type == otherKey.Type && key.Value.Replace("*01", "") == otherKey.Value.Replace("*01", "");

if (matchMode == MatchMode.Relaxed)
return (key.Type == otherKey.Type || key.Type == KeyTypes.GlobalReference || otherKey.Type == KeyTypes.GlobalReference)
&& (key.Value == otherKey.Value);
&& (key.Value.Replace("*01", "") == otherKey.Value.Replace("*01", ""));

if (matchMode == MatchMode.Identification)
return key.Value == otherKey.Value;
return key.Value.Replace("*01", "") == otherKey.Value.Replace("*01", "");

return false;
}
Expand Down
4 changes: 3 additions & 1 deletion src/AasxIntegrationBaseGdi/AnyUI/AnyUiMagickHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static AnyUiBitmapInfo MakePreviewFromPackageOrUrl(
try
{
System.IO.Stream thumbStream = null;
if (true == package?.IsLocalFile(path))
if (true /*= package?.IsLocalFile(path)*/)
thumbStream = package.GetLocalStreamFromPackage(path);
else
{
Expand All @@ -133,6 +133,7 @@ public static AnyUiBitmapInfo MakePreviewFromPackageOrUrl(
var wc = new WebClient();
thumbStream = wc.OpenRead(path);
#else
/*
// upgrade to HttpClient and follow re-directs
var hc = new HttpClient();
var response = hc.GetAsync(path).GetAwaiter().GetResult();
Expand All @@ -147,6 +148,7 @@ public static AnyUiBitmapInfo MakePreviewFromPackageOrUrl(
response.EnsureSuccessStatusCode();
thumbStream = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
*/
#endif
}

Expand Down
16 changes: 13 additions & 3 deletions src/BlazorExplorer/Controllers/ImageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This source code may use other Open Source software components (see LICENSE.txt)
*/

using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using AnyUi;
Expand Down Expand Up @@ -50,12 +51,21 @@ public ActionResult Get(string id)
.GetExecutingAssembly()
.GetManifestResourceStream("BlazorUI.Resources.sample.png"))
{
if (stream == null)
return NotFound();
// if (stream == null)
// return NotFound();

using (MemoryStream ms = new MemoryStream())
{
stream.CopyTo(ms);
if (stream!= null)
{
stream.CopyTo(ms);
}
else
{
var b = new Bitmap(1, 1);
b.SetPixel(0, 0, Color.White);
b.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
}
var bb = ms.ToArray();
return base.File(bb, "image/png");
}
Expand Down
1 change: 1 addition & 0 deletions src/BlazorExplorer/proxy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c:/dat/proxy.dat

0 comments on commit 81695b0

Please sign in to comment.