Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LoadFile.cs
1using System;
2using System.IO;
3using System.Threading.Tasks;
4using Waher.Content;
9
11{
16 {
25 : base(new ScriptNode[] { FileName }, argumentTypes1Scalar, Start, Length, Expression)
26 {
27 }
28
37 public LoadFile(ScriptNode FileName, ScriptNode ContentType, int Start, int Length, Expression Expression)
38 : base(new ScriptNode[] { FileName, ContentType }, argumentTypes2Scalar, Start, Length, Expression)
39 {
40 }
41
45 public override string FunctionName => nameof(LoadFile);
46
50 public override string[] DefaultArgumentNames => new string[] { "FileName", "ContentType" };
51
56 public override bool IsAsynchronous => true;
57
65 {
66 return this.EvaluateAsync(Arguments, Variables).Result;
67 }
68
75 public override async Task<IElement> EvaluateAsync(IElement[] Arguments, Variables Variables)
76 {
77 string FileName = ToString(Arguments[0]);
78 string ContentType;
79
80 FileName = Path.Combine(Directory.GetCurrentDirectory(), FileName);
81
82 if (Arguments.Length > 1)
83 ContentType = ToString(Arguments[1]);
84 else
85 ContentType = InternetContent.GetContentType(Path.GetExtension(FileName));
86
87 byte[] Bin;
88
89 try
90 {
91 using (FileStream fs = File.OpenRead(FileName))
92 {
93 Bin = await fs.ReadAllAsync();
94 }
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 {
105 Bin = await fs.ReadAllAsync();
106 }
107 }
108 finally
109 {
110 File.Delete(TempFileName);
111 }
112 }
113
114 ContentResponse Content = await InternetContent.DecodeAsync(ContentType, Bin, new Uri(FileName));
115
116 if (Content.HasError)
117 throw new ScriptRuntimeException(Content.Error.Message, this, Content.Error);
118
119 return Expression.Encapsulate(Content.Decoded);
120
121 }
122 }
123}
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.
LoadFile(ScriptNode FileName, int Start, int Length, Expression Expression)
LoadFile(FileName)
Definition: LoadFile.cs:24
override string FunctionName
Name of the function
Definition: LoadFile.cs:45
override async Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: LoadFile.cs:75
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: LoadFile.cs:56
override string[] DefaultArgumentNames
Default Argument names
Definition: LoadFile.cs:50
LoadFile(ScriptNode FileName, ScriptNode ContentType, int Start, int Length, Expression Expression)
LoadFile(FileName, ContentType)
Definition: LoadFile.cs:37
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: LoadFile.cs:64
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