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;
2using System.Collections.Generic;
3using System.IO;
4using System.Threading.Tasks;
5using Waher.Content;
8
10{
15 {
20 : base("/UpdateBackupFolderSettings")
21 {
22 }
23
27 public override bool HandlesSubPaths => false;
28
32 public override bool UserSessions => true;
33
37 public bool AllowsPOST => true;
38
45 public async Task POST(HttpRequest Request, HttpResponse Response)
46 {
47 Gateway.AssertUserAuthenticated(Request, "Admin.Data.Backup");
48
49 if (!Request.HasData)
50 throw new BadRequestException();
51
52 object Obj = await Request.DecodeDataAsync();
53
54 if (!(Obj is Dictionary<string, object> Form) ||
55 !Form.TryGetValue("ExportFolder", out Obj) || !(Obj is string ExportFolder) ||
56 !Form.TryGetValue("KeyFolder", out Obj) || !(Obj is string KeyFolder) ||
57 !Form.TryGetValue("BackupHosts", out Obj) || !(Obj is string BackupHostsStr) ||
58 !Form.TryGetValue("KeyHosts", out Obj) || !(Obj is string KeyHostsStr))
59 {
60 throw new BadRequestException();
61 }
62
63
64 ExportFolder = ExportFolder.Trim();
65 KeyFolder = KeyFolder.Trim();
66
67 string[] BackupHosts = BackupHostsStr.Split(',');
68 string[] KeyHosts = KeyHostsStr.Split(',');
69 int Len;
70
71 Trim(BackupHosts);
72 Trim(KeyHosts);
73
74 Len = ExportFolder.Length;
75 if (Len > 0 && (ExportFolder[Len - 1] == '/' || ExportFolder[Len - 1] == '\\'))
76 ExportFolder = ExportFolder.Substring(0, Len - 1);
77
78 Len = KeyFolder.Length;
79 if (Len > 0 && (KeyFolder[Len - 1] == '/' || KeyFolder[Len - 1] == '\\'))
80 KeyFolder = KeyFolder.Substring(0, Len - 1);
81
82 if (!string.IsNullOrEmpty(ExportFolder))
83 {
84 if (!Path.IsPathRooted(ExportFolder))
85 {
86 Response.StatusCode = 400;
87 Response.StatusMessage = "Bad Request";
88 Response.ContentType = PlainTextCodec.DefaultContentType;
89 await Response.Write("Export folder must be rooted. Relative paths are not accepted.");
90 await Response.SendResponse();
91 return;
92 }
93
94 if (!Directory.Exists(ExportFolder))
95 {
96 Response.StatusCode = 400;
97 Response.StatusMessage = "Bad Request";
98 Response.ContentType = PlainTextCodec.DefaultContentType;
99 await Response.Write("Export folder does not exist, or cannot be accessed or reached.");
100 await Response.SendResponse();
101 return;
102 }
103
104 try
105 {
106 string s = ExportFolder + Path.DirectorySeparatorChar + "test.txt";
107 await Resources.WriteAllTextAsync(s, "test");
108 File.Delete(s);
109 }
110 catch (Exception)
111 {
112 Response.StatusCode = 400;
113 Response.StatusMessage = "Bad Request";
114 Response.ContentType = PlainTextCodec.DefaultContentType;
115 await Response.Write("Not allowed to write data to the export folder.");
116 await Response.SendResponse();
117 return;
118 }
119 }
120
121 if (!string.IsNullOrEmpty(KeyFolder))
122 {
123 if (!Path.IsPathRooted(KeyFolder))
124 {
125 Response.StatusCode = 400;
126 Response.StatusMessage = "Bad Request";
127 Response.ContentType = PlainTextCodec.DefaultContentType;
128 await Response.Write("Key folder must be rooted. Relative paths are not accepted.");
129 await Response.SendResponse();
130 return;
131 }
132
133 if (!Directory.Exists(KeyFolder))
134 {
135 Response.StatusCode = 400;
136 Response.StatusMessage = "Bad Request";
137 Response.ContentType = PlainTextCodec.DefaultContentType;
138 await Response.Write("Key folder does not exist, or cannot be accessed or reached.");
139 await Response.SendResponse();
140 return;
141 }
142
143 try
144 {
145 string s = KeyFolder + Path.DirectorySeparatorChar + "test.txt";
146 await Resources.WriteAllTextAsync(s, "test");
147 File.Delete(s);
148 }
149 catch (Exception)
150 {
151 Response.StatusCode = 400;
152 Response.StatusMessage = "Bad Request";
153 Response.ContentType = PlainTextCodec.DefaultContentType;
154 await Response.Write("Not allowed to write data to the key folder.");
155 await Response.SendResponse();
156 return;
157 }
158 }
159
160 await Export.SetExportFolderAsync(ExportFolder);
161 await Export.SetExportKeyFolderAsync(KeyFolder);
162 await Export.SetBackupHostsAsync(BackupHosts);
163 await Export.SetKeyHostsAsync(KeyHosts);
164
165 Response.StatusCode = 200;
166 Response.StatusMessage = "OK";
167 await Response.SendResponse();
168 }
169
170 private static void Trim(string[] Array)
171 {
172 int i, c = Array.Length;
173
174 for (i = 0; i < c; i++)
175 Array[i] = Array[i].Trim();
176 }
177 }
178}
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static Task WriteAllTextAsync(string FileName, string Text)
Creates a text file asynchronously.
Definition: Resources.cs:267
Plain text encoder/decoder.
const string DefaultContentType
text/plain
Static class managing data export.
Definition: Export.cs:19
static async Task SetKeyHostsAsync(string[] Value)
Secondary key hosts.
Definition: Export.cs:620
static async Task SetBackupHostsAsync(string[] Value)
Secondary backup hosts.
Definition: Export.cs:594
static async Task SetExportFolderAsync(string Value)
Export folder.
Definition: Export.cs:159
static async Task SetExportKeyFolderAsync(string Value)
Key folder
Definition: Export.cs:195
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
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:3041
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:18
bool HasData
If the request has data.
Definition: HttpRequest.cs:74
async Task< object > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:95
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
async 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...
POST Interface for HTTP resources.