Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LoadResourceFile.cs
1using System;
2using System.IO;
3using System.Threading.Tasks;
4using Waher.Content;
6using Waher.Script;
10
12{
17 {
26 : base(new ScriptNode[] { LocalResource }, argumentTypes1Scalar, Start, Length, Expression)
27 {
28 }
29
38 public LoadResourceFile(ScriptNode LocalResource, ScriptNode ContentType, int Start, int Length, Expression Expression)
39 : base(new ScriptNode[] { LocalResource, ContentType }, argumentTypes2Scalar, Start, Length, Expression)
40 {
41 }
42
46 public override string FunctionName => nameof(LoadResourceFile);
47
51 public override string[] DefaultArgumentNames => new string[] { "LocalResource", "ContentType" };
52
57 public override bool IsAsynchronous => true;
58
66 {
67 return this.EvaluateAsync(Arguments, Variables).Result;
68 }
69
76 public override async Task<IElement> EvaluateAsync(IElement[] Arguments, Variables Variables)
77 {
78 string LocalResource = ToString(Arguments[0]);
79 string ContentType;
80
81 if (!Gateway.HttpServer.TryGetFileName(LocalResource, true, out string FileName))
82 throw new ScriptRuntimeException("Unable to convert local resource to file name.", this);
83
84 if (Arguments.Length > 1)
85 ContentType = ToString(Arguments[1]);
86 else
87 ContentType = InternetContent.GetContentType(Path.GetExtension(FileName));
88
89 byte[] Bin;
90
91 try
92 {
93 using FileStream fs = File.OpenRead(FileName);
94 Bin = await fs.ReadAllAsync();
95 }
96 catch (IOException)
97 {
98 string TempFileName = Path.GetTempFileName();
99 File.Copy(FileName, TempFileName, true);
100
101 try
102 {
103 using FileStream fs = File.OpenRead(TempFileName);
104 Bin = await fs.ReadAllAsync();
105 }
106 finally
107 {
108 File.Delete(TempFileName);
109 }
110 }
111
112 ContentResponse Content = await InternetContent.DecodeAsync(ContentType, Bin, new Uri(Gateway.GetUrl(LocalResource)));
113
114 if (Content.HasError)
115 throw new ScriptRuntimeException(Content.Error.Message, this, Content.Error);
116
117 return Expression.Encapsulate(Content.Decoded);
118 }
119 }
120}
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Exception Error
Error response.
Static class managing encoding and decoding of internet content.
static string GetContentType(string FileExtension)
Gets the content type of an item, given its file extension. It uses the TryGetContentType to see if a...
static Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static HttpServer HttpServer
HTTP Server
Definition: Gateway.cs:4118
static string GetUrl(string LocalResource)
Gets a URL for a resource.
Definition: Gateway.cs:5079
override async Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the function.
override string[] DefaultArgumentNames
Default Argument names
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
LoadResourceFile(ScriptNode LocalResource, int Start, int Length, Expression Expression)
LoadResourceFile(LocalResource)
LoadResourceFile(ScriptNode LocalResource, ScriptNode ContentType, int Start, int Length, Expression Expression)
LoadResourceFile(LocalResource, ContentType)
Class managing a script expression.
Definition: Expression.cs:41
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:5241
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes2Scalar
Two scalar parameters.
static readonly ArgumentType[] argumentTypes1Scalar
One scalar parameter.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
override string ToString()
Definition: ScriptNode.cs:359
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21