Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CreateTextFileCommand.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Text;
5using System.Threading.Tasks;
7using Waher.Things;
10
12{
17 {
18 private readonly ProgramDataFolder folderNode;
19 private readonly string contentType;
20 private readonly string extension;
21 private readonly int nameStringId;
22 private readonly string nameDefault;
23 private readonly string commandId;
24 private readonly string sortKey;
25
36 public CreateTextFileCommand(ProgramDataFolder FolderNode, string ContentType, string Extension, int NameStringId, string NameDefault, string CommandId,
37 string SortKey)
38 {
39 this.folderNode = FolderNode;
40 this.contentType = ContentType;
41 this.extension = Extension;
42 this.nameStringId = NameStringId;
43 this.nameDefault = NameDefault;
44 this.commandId = CommandId;
45 this.sortKey = SortKey;
46
47 this.FileLocalName = Extension;
48 this.Content = new string[0];
49 this.Encoding = System.Text.Encoding.UTF8.BodyName;
50 }
51
52 [Page(7, "File System", 100)]
53 [Header(12, "File Name:")]
54 [ToolTip(13, "Local file name.")]
55 [Required]
56 public string FileLocalName { get; set; }
57
61 [Page(7, "File System", 100)]
62 [Header(30, "Text Content:")]
63 [ToolTip(31, "Text content in file.")]
64 [Required]
65 [DynamicContentType(nameof(GetContentType))]
66 public string[] Content { get; set; }
67
71 [Page(7, "File System", 100)]
72 [Header(32, "Text Encoding:")]
73 [ToolTip(33, "Encoding used to store text in file.")]
74 [Required]
75 [DynamicOptions(nameof(GetEncodingOptions))]
76 public string Encoding { get; set; }
77
83 {
84 List<OptionAttribute> Encodings = new List<OptionAttribute>();
85
86 foreach (EncodingInfo Encoding in System.Text.Encoding.GetEncodings())
87 Encodings.Add(new OptionAttribute(Encoding.Name));
88
89 return Encodings.ToArray();
90 }
91
97 {
98 return new ContentTypeAttribute(this.contentType);
99 }
100
104 public string CommandID => this.commandId;
105
109 public CommandType Type => CommandType.Parametrized;
110
114 public string SortCategory => "Edit";
115
119 public string SortKey => this.sortKey;
120
125 public Task<string> GetNameAsync(Language Language) => Language.GetStringAsync(typeof(ProgramDataSource), this.nameStringId, this.nameDefault);
126
131 public Task<string> GetConfirmationStringAsync(Language Language) => Task.FromResult(string.Empty);
132
137 public Task<string> GetFailureStringAsync(Language Language) => Task.FromResult(string.Empty);
138
143 public Task<string> GetSuccessStringAsync(Language Language) => Task.FromResult(string.Empty);
144
150 public Task<bool> CanExecuteAsync(RequestOrigin Caller) => XmppServerModule.IsAdmin(Caller.From);
151
155 public async Task ExecuteCommandAsync()
156 {
157 Encoding Encoding = System.Text.Encoding.GetEncoding(this.Encoding)
158 ?? throw new ArgumentException("Invalid encoding.", nameof(this.Encoding));
159
160 string FullPath = Path.Combine(this.folderNode.FolderName, this.FileLocalName);
161 if (!FullPath.StartsWith(this.folderNode.FolderName, StringComparison.CurrentCultureIgnoreCase))
162 throw new Exception("Invalid local file name.");
163
164 StringBuilder sb = new StringBuilder();
165 bool First = true;
166
167 foreach (string Row in this.Content)
168 {
169 if (First)
170 First = false;
171 else
172 sb.AppendLine();
173
174 sb.Append(Row);
175 }
176
177 string Text = sb.ToString();
178 byte[] Bin = Encoding.GetBytes(Text);
179 byte[] Bom = Encoding.GetPreamble();
180 bool HasBom = EditTextCommand.HasBom(Bin, Bom);
181
182 using (FileStream fs = File.Create(FullPath))
183 {
184 if (!HasBom)
185 await fs.WriteAsync(Bom, 0, Bom.Length);
186
187 await fs.WriteAsync(Bin, 0, Bin.Length);
188 await fs.FlushAsync();
189 }
190 }
191
198
203 public ICommand Copy()
204 {
205 return new CreateTextFileCommand(this.folderNode, this.contentType, this.extension, this.nameStringId, this.nameDefault,
206 this.commandId, this.sortKey);
207 }
208 }
209}
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
Task< string > GetFailureStringAsync(Language Language)
Gets a failure string, if any, of the command. If no specific failure string is available,...
Task< string > GetNameAsync(Language Language)
Gets the name of data source.
CreateTextFileCommand(ProgramDataFolder FolderNode, string ContentType, string Extension, int NameStringId, string NameDefault, string CommandId, string SortKey)
Creates a text file in a folder.
OptionAttribute[] GetEncodingOptions()
Gets encoding options when editing Encoding.
Task< bool > CanExecuteAsync(RequestOrigin Caller)
If the command can be executed by the caller.
Task< string > GetConfirmationStringAsync(Language Language)
Gets a confirmation string, if any, of the command. If no confirmation is necessary,...
Task StartQueryExecutionAsync(Query Query, Language Language)
Starts the execution of a query.
Task< string > GetSuccessStringAsync(Language Language)
Gets a success string, if any, of the command. If no specific success string is available,...
ContentTypeAttribute GetContentType()
Gets the Content-Type of the Content property.
Reference to a folder in the ProgramData folder of the broker.
Data source mirroring the ProgramData folder for the broker.
Service Module hosting the XMPP broker and its components.
Defines the Content-Type of a multi-row property. Acts as a hint to clients, on how the text can be e...
Defines an option to display when editing the parameter.
Class handling the reception of data from a query.
Definition: Query.cs:12
Tokens available in request.
Definition: RequestOrigin.cs:9
Interface for commands.
Definition: ICommand.cs:32
CommandType
Command type.
Definition: ICommand.cs:11