Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SaveFile.cs
2using System.IO;
3using System.Threading.Tasks;
4using Waher.Content;
11
13{
18 {
27 public SaveFile(ScriptNode Object, ScriptNode FileName, int Start, int Length, Expression Expression)
28 : base(Object, FileName, Start, Length, Expression)
29 {
30 }
31
35 public override string FunctionName => nameof(SaveFile);
36
40 public override string[] DefaultArgumentNames => new string[] { "Object", "FileName" };
41
46 public override bool IsAsynchronous => true;
47
56 {
57 return this.EvaluateAsync(Argument1, Argument2, Variables).Result;
58 }
59
67 public override async Task<IElement> EvaluateAsync(IElement Argument1, IElement Argument2, Variables Variables)
68 {
69 object Obj = Argument1.AssociatedObjectValue;
70 string FileName = ToString(Argument2);
71
72 if (Obj is Graph G)
73 Obj = G.CreatePixels();
74
75 ContentResponse Content;
76
77 if (InternetContent.TryGetContentType(Path.GetExtension(FileName), out string ContentType) &&
78 InternetContent.Encodes(Obj, out Grade _, out IContentEncoder Encoder, ContentType))
79 {
80 Content = await Encoder.EncodeAsync(Obj, System.Text.Encoding.UTF8, null, ContentType);
81 }
82 else
83 Content = await InternetContent.EncodeAsync(Obj, System.Text.Encoding.UTF8);
84
85 if (Content.HasError)
86 throw new ScriptRuntimeException(Content.Error.Message, this, Content.Error);
87
88 using (FileStream fs = File.Create(FileName))
89 {
90 await fs.WriteAsync(Content.Encoded, 0, Content.Encoded.Length);
91 }
92
93 return new StringValue(ContentType);
94 }
95 }
96}
Contains information about a response to a content request.
byte[] Encoded
Encoded object.
bool HasError
If an error occurred.
Exception Error
Error response.
Static class managing encoding and decoding of internet content.
static bool Encodes(object Object, out Grade Grade, out IContentEncoder Encoder, params string[] AcceptedContentTypes)
If a given object can be encoded.
static Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
static bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
override string FunctionName
Name of the function
Definition: SaveFile.cs:35
override IElement Evaluate(IElement Argument1, IElement Argument2, Variables Variables)
Evaluates the function.
Definition: SaveFile.cs:55
SaveFile(ScriptNode Object, ScriptNode FileName, int Start, int Length, Expression Expression)
SaveFile(Object,FileName)
Definition: SaveFile.cs:27
override string[] DefaultArgumentNames
Default Argument names
Definition: SaveFile.cs:40
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: SaveFile.cs:46
override async Task< IElement > EvaluateAsync(IElement Argument1, IElement Argument2, Variables Variables)
Evaluates the function.
Definition: SaveFile.cs:67
Class managing a script expression.
Definition: Expression.cs:41
Base class for graphs.
Definition: Graph.cs:88
Base class for funcions of one variable.
ScriptNode Argument2
Function argument 2.
ScriptNode Argument1
Function argument 1.
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
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Collection of variables.
Definition: Variables.cs:25
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Basic interface for all types of elements.
Definition: IElement.cs:21
Definition: ImplTypes.g.cs:58
Grade
Grade enumeration
Definition: Grade.cs:7