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
113 public abstract Task<bool> StartDatabase(IDatabaseProvider Provider);
114
119 public abstract Task<bool> EndDatabase();
120
126 public abstract Task<bool> StartLedger(ILedgerProvider Provider);
127
132 public abstract Task<bool> EndLedger();
133
139 public abstract Task<bool> StartCollection(string CollectionName);
140
145 public abstract Task<bool> EndCollection();
146
151 public abstract Task<bool> StartIndex();
152
157 public abstract Task<bool> EndIndex();
158
165 public abstract Task<bool> ReportIndexField(string FieldName, bool Ascending);
166
172 public abstract Task<bool> StartBlock(string BlockID);
173
178 public abstract Task<bool> EndBlock();
179
186 public abstract Task<bool> BlockMetaData(string Key, object Value);
187
196 public abstract Task<bool> StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp);
197
202 public abstract Task<bool> EndEntry();
203
209 public virtual async Task<bool> CollectionCleared(DateTimeOffset EntryTimestamp)
210 {
211 return
212 await this.StartEntry(string.Empty, string.Empty, EntryType.Clear, EntryTimestamp) &&
213 await this.EndEntry();
214 }
215
222 public abstract Task<string> StartObject(string ObjectId, string TypeName);
223
228 public abstract Task<bool> EndObject();
229
236 public abstract Task<bool> ReportProperty(string PropertyName, object PropertyValue);
237
243 public abstract Task<bool> ReportError(string Message);
244
250 public abstract Task<bool> ReportException(Exception Exception);
251
256 public abstract Task<bool> StartFiles();
257
262 public abstract Task<bool> EndFiles();
263
270 public abstract Task<bool> ExportFile(string FileName, Stream File);
271
277 public Task<bool> UpdateClient(bool ForceUpdate)
278 {
279 DateTime TP = DateTime.Now;
280
281 if (ForceUpdate || (TP - this.lastUpdate).TotalSeconds >= 1)
282 {
283 this.lastUpdate = TP;
284 UpdateClientsFileUpdated(this.fileName, this.fs.Length, this.created);
285 }
286
287 return Task.FromResult(true);
288 }
289
296 public static void UpdateClientsFileUpdated(string FileName, long Length, DateTime Created)
297 {
298 StringBuilder sb = new StringBuilder();
299
300 sb.Append("{\"fileName\":\"");
302 sb.Append("\", \"size\": \"");
303 if (Length >= 0)
305 sb.Append("\", \"created\": \"");
306 sb.Append(CommonTypes.JsonStringEncode(Created.ToString()));
307 sb.Append("\", \"button\": \"");
308 sb.Append(CommonTypes.JsonStringEncode("<button class=\"negButtonSm\" onclick=\"DeleteExport('" + FileName + "');\">Delete</button>"));
309 sb.Append("\", \"isKey\": ");
310 sb.Append(FileName.EndsWith(".key", StringComparison.CurrentCultureIgnoreCase) ? "true" : "false");
311 sb.Append("}");
312
313 string[] TabIDs = ClientEvents.GetTabIDsForLocation("/Settings/Backup.md");
314
315 Task _ = ClientEvents.PushEvent(TabIDs, "UpdateExport", sb.ToString(), true, "User");
316 }
317
322 public static void UpdateClientsFileDeleted(string FileName)
323 {
324 StringBuilder sb = new StringBuilder();
325
326 sb.Append("{\"fileName\":\"");
328 sb.Append("\"}");
329
330 string[] TabIDs = ClientEvents.GetTabIDsForLocation("/Settings/Backup.md");
331
332 Task _ = ClientEvents.PushEvent(TabIDs, "FileDeleted", sb.ToString(), true, "User");
333 }
334 }
335}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:15
static string JsonStringEncode(string s)
Encodes a string for inclusion in JSON.
Definition: CommonTypes.cs:805
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:18
static string FormatBytes(double Bytes)
Formats a file size using appropriate unit.
Definition: Export.cs:125
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 > StartDatabase(IDatabaseProvider Provider)
Is called when export of database is started.
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 > 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 > 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 > StartLedger(ILedgerProvider Provider)
Is called when export of ledger is started.
abstract Task< bool > Start()
Starts export
abstract Task< bool > StartCollection(string CollectionName)
Is called when a collection is started.
Interface for database providers that can be plugged into the static Database class.
Interface for ledger providers that can be plugged into the static Ledger class.
Definition: ImplTypes.g.cs:58
EntryType
Ledger entry type.
Definition: ILedgerEntry.cs:9