Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ExportFormat.cs
1using System;
2using System.IO;
3using System.Text;
4using System.Threading.Tasks;
5using Waher.Content;
7
9{
13 public abstract class ExportFormat : IExportFormat, IDisposable
14 {
15 private DateTime lastUpdate = DateTime.Now;
16 private readonly DateTime created;
17 private FileStream fs;
18 private readonly string fileName;
19 private readonly bool onlySelectedCollections;
20 private readonly Array selectedCollections;
21
30 public ExportFormat(string FileName, DateTime Created, FileStream File, bool OnlySelectedCollections, Array SelectedCollections)
31 {
32 this.fileName = FileName;
33 this.created = Created;
34 this.fs = File;
35 this.onlySelectedCollections = OnlySelectedCollections;
36 this.selectedCollections = SelectedCollections;
37 }
38
42 public string[] CollectionNames
43 {
44 get
45 {
46 if (this.onlySelectedCollections)
47 {
48 int i, c = this.selectedCollections.Length;
49 string[] Result = new string[c];
50
51 for (i = 0; i < c; i++)
52 Result[i] = this.selectedCollections.GetValue(i).ToString();
53
54 return Result;
55 }
56 else
57 return null;
58 }
59 }
60
64 public string FileName => this.fileName;
65
69 public DateTime Created => this.created;
70
74 public virtual void Dispose()
75 {
76 if (!(this.fs is null))
77 {
78 if (this.fs.CanWrite)
79 this.fs.Flush();
80
81 this.fs.Dispose();
82 this.fs = null;
83 }
84 }
85
91 protected bool ExportCollection(string CollectionName)
92 {
93 return !this.onlySelectedCollections || Array.IndexOf(this.selectedCollections, CollectionName) >= 0;
94 }
95
100 public abstract Task<bool> Start();
101
106 public abstract Task<bool> End();
107
112 public abstract Task<bool> StartDatabase();
113
118 public abstract Task<bool> EndDatabase();
119
124 public abstract Task<bool> StartLedger();
125
130 public abstract Task<bool> EndLedger();
131
137 public abstract Task<bool> StartCollection(string CollectionName);
138
143 public abstract Task<bool> EndCollection();
144
149 public abstract Task<bool> StartIndex();
150
155 public abstract Task<bool> EndIndex();
156
163 public abstract Task<bool> ReportIndexField(string FieldName, bool Ascending);
164
170 public abstract Task<bool> StartBlock(string BlockID);
171
176 public abstract Task<bool> EndBlock();
177
184 public abstract Task<bool> BlockMetaData(string Key, object Value);
185
194 public abstract Task<bool> StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp);
195
200 public abstract Task<bool> EndEntry();
201
207 public virtual async Task<bool> CollectionCleared(DateTimeOffset EntryTimestamp)
208 {
209 return
210 await this.StartEntry(string.Empty, string.Empty, EntryType.Clear, EntryTimestamp) &&
211 await this.EndEntry();
212 }
213
220 public abstract Task<string> StartObject(string ObjectId, string TypeName);
221
226 public abstract Task<bool> EndObject();
227
234 public abstract Task<bool> ReportProperty(string PropertyName, object PropertyValue);
235
241 public abstract Task<bool> ReportError(string Message);
242
248 public abstract Task<bool> ReportException(Exception Exception);
249
254 public abstract Task<bool> StartFiles();
255
260 public abstract Task<bool> EndFiles();
261
268 public abstract Task<bool> ExportFile(string FileName, Stream File);
269
275 public Task<bool> UpdateClient(bool ForceUpdate)
276 {
277 DateTime TP = DateTime.Now;
278
279 if (ForceUpdate || (TP - this.lastUpdate).TotalSeconds >= 1)
280 {
281 this.lastUpdate = TP;
282 UpdateClientsFileUpdated(this.fileName, this.fs.Length, this.created);
283 }
284
285 return Task.FromResult(true);
286 }
287
294 public static void UpdateClientsFileUpdated(string FileName, long Length, DateTime Created)
295 {
296 StringBuilder sb = new StringBuilder();
297
298 sb.Append("{\"fileName\":\"");
300 sb.Append("\", \"size\": \"");
301 if (Length >= 0)
303 sb.Append("\", \"created\": \"");
304 sb.Append(CommonTypes.JsonStringEncode(Created.ToString()));
305 sb.Append("\", \"button\": \"");
306 sb.Append(CommonTypes.JsonStringEncode("<button class=\"negButtonSm\" onclick=\"DeleteExport('" + FileName + "');\">Delete</button>"));
307 sb.Append("\", \"isKey\": ");
308 sb.Append(FileName.EndsWith(".key", StringComparison.CurrentCultureIgnoreCase) ? "true" : "false");
309 sb.Append("}");
310
311 string[] TabIDs = ClientEvents.GetTabIDsForLocation("/Settings/Backup.md");
312
313 Task _ = ClientEvents.PushEvent(TabIDs, "UpdateExport", sb.ToString(), true, "User");
314 }
315
320 public static void UpdateClientsFileDeleted(string FileName)
321 {
322 StringBuilder sb = new StringBuilder();
323
324 sb.Append("{\"fileName\":\"");
326 sb.Append("\"}");
327
328 string[] TabIDs = ClientEvents.GetTabIDsForLocation("/Settings/Backup.md");
329
330 Task _ = ClientEvents.PushEvent(TabIDs, "FileDeleted", sb.ToString(), true, "User");
331 }
332 }
333}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static string JsonStringEncode(string s)
Encodes a string for inclusion in JSON.
Definition: CommonTypes.cs:803
The ClientEvents class allows applications to push information asynchronously to web clients connecte...
Definition: ClientEvents.cs:51
static string[] GetTabIDsForLocation(string Location)
Gets the Tab IDs of all tabs that display a particular resource.
static Task< int > PushEvent(string[] TabIDs, string Type, object Data)
Puses an event to a set of Tabs, given their Tab IDs.
Static class managing data export.
Definition: Export.cs:19
static string FormatBytes(double Bytes)
Formats a file size using appropriate unit.
Definition: Export.cs:126
Abstract base class for export formats.
Definition: ExportFormat.cs:14
abstract Task< bool > StartIndex()
Is called when an index in a collection is started.
abstract Task< bool > StartFiles()
Starts export of files.
string[] CollectionNames
Optional array of collection nmes to export. If null, all collections will be exported.
Definition: ExportFormat.cs:43
ExportFormat(string FileName, DateTime Created, FileStream File, bool OnlySelectedCollections, Array SelectedCollections)
Abstract base class for export formats.
Definition: ExportFormat.cs:30
static void UpdateClientsFileUpdated(string FileName, long Length, DateTime Created)
Updates the status of a file on all pages viewing backup files
abstract Task< bool > ReportException(Exception Exception)
Is called when an exception has occurred.
abstract Task< bool > ReportProperty(string PropertyName, object PropertyValue)
Is called when a property is reported.
abstract Task< bool > StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
Is called when an entry is started.
abstract Task< bool > EndEntry()
Is called when an entry is finished.
abstract Task< bool > EndObject()
Is called when an object is finished.
abstract Task< bool > EndDatabase()
Is called when export of database is finished.
abstract Task< bool > EndCollection()
Is called when a collection is finished.
Task< bool > UpdateClient(bool ForceUpdate)
If any clients should be updated about export status.
abstract Task< bool > EndIndex()
Is called when an index in a collection is finished.
abstract Task< bool > ReportError(string Message)
Is called when an error is reported.
abstract Task< bool > EndFiles()
Ends export of files.
abstract Task< string > StartObject(string ObjectId, string TypeName)
Is called when an object is started.
abstract Task< bool > StartLedger()
Is called when export of ledger is started.
abstract Task< bool > ReportIndexField(string FieldName, bool Ascending)
Is called when a field in an index is reported.
abstract Task< bool > BlockMetaData(string Key, object Value)
Reports block meta-data.
virtual async Task< bool > CollectionCleared(DateTimeOffset EntryTimestamp)
Is called when a collection has been cleared.
bool ExportCollection(string CollectionName)
Checks if a collection is to be exported or not.
Definition: ExportFormat.cs:91
abstract Task< bool > StartBlock(string BlockID)
Is called when a block in a collection is started.
abstract Task< bool > ExportFile(string FileName, Stream File)
Export file.
abstract Task< bool > StartDatabase()
Is called when export of database is started.
abstract Task< bool > EndLedger()
Is called when export of ledger is finished.
static void UpdateClientsFileDeleted(string FileName)
Removes a file from all pages viewing backup files
abstract Task< bool > EndBlock()
Is called when a block in a collection is finished.
abstract Task< bool > Start()
Starts export
abstract Task< bool > StartCollection(string CollectionName)
Is called when a collection is started.
EntryType
Ledger entry type.
Definition: ILedgerEntry.cs:9