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;
8
10{
15 {
24 : base(new ScriptNode[] { FileName }, argumentTypes1Scalar, Start, Length, Expression)
25 {
26 }
27
36 public LoadFile(ScriptNode FileName, ScriptNode ContentType, int Start, int Length, Expression Expression)
37 : base(new ScriptNode[] { FileName, ContentType }, argumentTypes2Scalar, Start, Length, Expression)
38 {
39 }
40
44 public override string FunctionName => nameof(LoadFile);
45
49 public override string[] DefaultArgumentNames => new string[] { "FileName", "ContentType" };
50
55 public override bool IsAsynchronous => true;
56
64 {
65 return this.EvaluateAsync(Arguments, Variables).Result;
66 }
67
74 public override async Task<IElement> EvaluateAsync(IElement[] Arguments, Variables Variables)
75 {
76 string FileName = Arguments[0].AssociatedObjectValue?.ToString();
77 string ContentType;
78
79 FileName = Path.Combine(Directory.GetCurrentDirectory(), FileName);
80
81 if (Arguments.Length > 1)
82 ContentType = Arguments[1].AssociatedObjectValue?.ToString();
83 else
84 ContentType = InternetContent.GetContentType(Path.GetExtension(FileName));
85
86 byte[] Bin;
87
88 try
89 {
90 using (FileStream fs = File.OpenRead(FileName))
91 {
92 Bin = await this.LoadBin(fs);
93 }
94 }
95 catch (IOException)
96 {
97 string TempFileName = Path.GetTempFileName();
98 File.Copy(FileName, TempFileName, true);
99
100 try
101 {
102 using (FileStream fs = File.OpenRead(TempFileName))
103 {
104 Bin = await this.LoadBin(fs);
105 }
106 }
107 finally
108 {
109 File.Delete(TempFileName);
110 }
111 }
112
113 object Decoded = await InternetContent.DecodeAsync(ContentType, Bin, new Uri(FileName));
114
115 return Expression.Encapsulate(Decoded);
116 }
117
118 private async Task<byte[]> LoadBin(FileStream fs)
119 {
120 long l = fs.Length;
121 if (l > int.MaxValue)
122 throw new ScriptRuntimeException("File too large.", this);
123
124 int Len = (int)l;
125 byte[] Bin = new byte[Len];
126 int Pos = 0;
127
128 while (Pos < Len)
129 {
130 int i = await fs.ReadAsync(Bin, Pos, Len - Pos);
131 if (i < 0)
132 throw new ScriptRuntimeException("Unexpected end of file.", this);
133
134 Pos += i;
135 }
136
137 return Bin;
138 }
139 }
140}
Static class managing encoding and decoding of internet content.
static Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
static string GetContentType(string FileExtension)
Gets the content type of an item, given its file extension. It uses the TryGetContentType to see if a...
LoadFile(ScriptNode FileName, int Start, int Length, Expression Expression)
LoadFile(FileName)
Definition: LoadFile.cs:23
override string FunctionName
Name of the function
Definition: LoadFile.cs:44
override async Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: LoadFile.cs:74
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: LoadFile.cs:55
override string[] DefaultArgumentNames
Default Argument names
Definition: LoadFile.cs:49
LoadFile(ScriptNode FileName, ScriptNode ContentType, int Start, int Length, Expression Expression)
LoadFile(FileName, ContentType)
Definition: LoadFile.cs:36
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: LoadFile.cs:63
Class managing a script expression.
Definition: Expression.cs:39
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:4955
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:20