Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
UpdateBackupFolderSettings.cs
1using System;
3using System.IO;
4using System.Threading.Tasks;
5using Waher.Content;
9
11{
16 {
21 : base("/UpdateBackupFolderSettings")
22 {
23 }
24
28 public override bool HandlesSubPaths => false;
29
33 public override bool UserSessions => true;
34
38 public bool AllowsPOST => true;
39
46 public async Task POST(HttpRequest Request, HttpResponse Response)
47 {
48 Gateway.AssertUserAuthenticated(Request, "Admin.Data.Backup");
49
50 if (!Request.HasData)
51 {
52 await Response.SendResponse(new BadRequestException());
53 return;
54 }
55
56 ContentResponse Content = await Request.DecodeDataAsync();
57
58 if (Content.HasError ||
59 !(Content.Decoded is Dictionary<string, object> Form) ||
60 !Form.TryGetValue("ExportFolder", out object Obj) || !(Obj is string ExportFolder) ||
61 !Form.TryGetValue("KeyFolder", out Obj) || !(Obj is string KeyFolder) ||
62 !Form.TryGetValue("BackupHosts", out Obj) || !(Obj is string BackupHostsStr) ||
63 !Form.TryGetValue("KeyHosts", out Obj) || !(Obj is string KeyHostsStr))
64 {
65 await Response.SendResponse(new BadRequestException());
66 return;
67 }
68
69
70 ExportFolder = ExportFolder.Trim();
71 KeyFolder = KeyFolder.Trim();
72
73 string[] BackupHosts = BackupHostsStr.Split(',');
74 string[] KeyHosts = KeyHostsStr.Split(',');
75 int Len;
76
77 Trim(BackupHosts);
78 Trim(KeyHosts);
79
80 Len = ExportFolder.Length;
81 if (Len > 0 && (ExportFolder[Len - 1] == '/' || ExportFolder[Len - 1] == '\\'))
82 ExportFolder = ExportFolder[..(Len - 1)];
83
84 Len = KeyFolder.Length;
85 if (Len > 0 && (KeyFolder[Len - 1] == '/' || KeyFolder[Len - 1] == '\\'))
86 KeyFolder = KeyFolder[..(Len - 1)];
87
88 if (!string.IsNullOrEmpty(ExportFolder))
89 {
90 if (!Path.IsPathRooted(ExportFolder))
91 {
92 Response.StatusCode = 400;
93 Response.StatusMessage = "Bad Request";
94 Response.ContentType = PlainTextCodec.DefaultContentType;
95 await Response.Write("Export folder must be rooted. Relative paths are not accepted.");
96 await Response.SendResponse();
97 return;
98 }
99
100 if (!Directory.Exists(ExportFolder))
101 {
102 Response.StatusCode = 400;
103 Response.StatusMessage = "Bad Request";
104 Response.ContentType = PlainTextCodec.DefaultContentType;
105 await Response.Write("Export folder does not exist, or cannot be accessed or reached.");
106 await Response.SendResponse();
107 return;
108 }
109
110 try
111 {
112 string s = ExportFolder + Path.DirectorySeparatorChar + "test.txt";
113 await Files.WriteAllTextAsync(s, "test");
114 File.Delete(s);
115 }
116 catch (Exception)
117 {
118 Response.StatusCode = 400;
119 Response.StatusMessage = "Bad Request";
120 Response.ContentType = PlainTextCodec.DefaultContentType;
121 await Response.Write("Not allowed to write data to the export folder.");
122 await Response.SendResponse();
123 return;
124 }
125 }
126
127 if (!string.IsNullOrEmpty(KeyFolder))
128 {
129 if (!Path.IsPathRooted(KeyFolder))
130 {
131 Response.StatusCode = 400;
132 Response.StatusMessage = "Bad Request";
133 Response.ContentType = PlainTextCodec.DefaultContentType;
134 await Response.Write("Key folder must be rooted. Relative paths are not accepted.");
135 await Response.SendResponse();
136 return;
137 }
138
139 if (!Directory.Exists(KeyFolder))
140 {
141 Response.StatusCode = 400;
142 Response.StatusMessage = "Bad Request";
143 Response.ContentType = PlainTextCodec.DefaultContentType;
144 await Response.Write("Key folder does not exist, or cannot be accessed or reached.");
145 await Response.SendResponse();
146 return;
147 }
148
149 try
150 {
151 string s = KeyFolder + Path.DirectorySeparatorChar + "test.txt";
152 await Files.WriteAllTextAsync(s, "test");
153 File.Delete(s);
154 }
155 catch (Exception)
156 {
157 Response.StatusCode = 400;
158 Response.StatusMessage = "Bad Request";
159 Response.ContentType = PlainTextCodec.DefaultContentType;
160 await Response.Write("Not allowed to write data to the key folder.");
161 await Response.SendResponse();
162 return;
163 }
164 }
165
166 await Export.SetExportFolderAsync(ExportFolder);
167 await Export.SetExportKeyFolderAsync(KeyFolder);
168 await Export.SetBackupHostsAsync(BackupHosts);
169 await Export.SetKeyHostsAsync(KeyHosts);
170
171 Response.StatusCode = 200;
172 Response.StatusMessage = "OK";
173 await Response.SendResponse();
174 }
175
176 private static void Trim(string[] Array)
177 {
178 int i, c = Array.Length;
179
180 for (i = 0; i < c; i++)
181 Array[i] = Array[i].Trim();
182 }
183 }
184}
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Plain text encoder/decoder.
const string DefaultContentType
text/plain
Static class managing data export.
Definition: Export.cs:18
static async Task SetKeyHostsAsync(string[] Value)
Secondary key hosts.
Definition: Export.cs:619
static async Task SetBackupHostsAsync(string[] Value)
Secondary backup hosts.
Definition: Export.cs:593
static async Task SetExportFolderAsync(string Value)
Export folder.
Definition: Export.cs:158
static async Task SetExportKeyFolderAsync(string Value)
Key folder
Definition: Export.cs:194
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static IUser AssertUserAuthenticated(HttpRequest Request, string Privilege)
Makes sure a request is being made from a session with a successful user login.
Definition: Gateway.cs:3868
Web Resource for updating where backup files are storeed.
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
override bool HandlesSubPaths
If the resource handles sub-paths.
override bool UserSessions
If the resource uses user sessions.
UpdateBackupFolderSettings()
Web Resource for updating where backup files are storeed.
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
Represents an HTTP request.
Definition: HttpRequest.cs:22
bool HasData
If the request has data.
Definition: HttpRequest.cs:113
async Task< ContentResponse > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:139
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
Task Write(byte[] Data)
Returns binary data in the response.
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
Contains static methods
Definition: Files.cs:14
static Task WriteAllTextAsync(string FileName, string Text)
Creates a text file asynchronously.
Definition: Files.cs:95
POST Interface for HTTP resources.