tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
ConfigurationController.cs
Go to the documentation of this file.
1using System;
2using System.IO;
5
8
22
24{
30 {
35
46 IDatabaseContext databaseContext,
47 IAuthenticationContext authenticationContext,
51 IApiHeadersProvider apiHeaders)
52 : base(
53 databaseContext,
54 authenticationContext,
55 logger,
57 apiHeaders)
58 {
60 }
61
70 [HttpPost]
75 {
76 ArgumentNullException.ThrowIfNull(model);
78 return Forbid();
79
80 try
81 {
83 async instance =>
84 {
85 var newFile = await instance
86 .Configuration
87 .Write(
88 model.Path!,
90 model.LastReadHash,
92
93 if (newFile == null)
94 return Conflict(new ErrorMessageResponse(ErrorCode.ConfigurationContendedAccess));
95
96 return model.LastReadHash == null ? Accepted(newFile) : Json(newFile);
97 });
98 }
99 catch (IOException e)
100 {
101 Logger.LogInformation(e, "IOException while updating file {path}!", model.Path);
102 return Conflict(new ErrorMessageResponse(ErrorCode.IOError)
103 {
104 AdditionalData = e.Message,
105 });
106 }
107 }
108
117 [HttpGet(Routes.File + "/{*filePath}")]
123 {
125 return Forbid();
126
127 try
128 {
130 async instance =>
131 {
132 var result = await instance
133 .Configuration
135
136 if (result == null)
137 return Conflict(new ErrorMessageResponse(ErrorCode.ConfigurationContendedAccess));
138
139 return Json(result);
140 });
141 }
142 catch (IOException e)
143 {
144 Logger.LogInformation(e, "IOException while reading file {path}!", filePath);
145 return Conflict(new ErrorMessageResponse(ErrorCode.IOError)
146 {
147 AdditionalData = e.Message,
148 });
149 }
150 }
151
162 [HttpGet(Routes.List + "/{*directoryPath}")]
168 string? directoryPath,
169 [FromQuery] int? page,
170 [FromQuery] int? pageSize,
171 CancellationToken cancellationToken)
173 instance => Paginated(
174 async () =>
175 {
178 Forbid());
179
180 try
181 {
182 var result = await instance
183 .Configuration
185
186 if (result == null)
188 Conflict(new ErrorMessageResponse(ErrorCode.ConfigurationContendedAccess)));
189
191 }
193 {
195 Forbid());
196 }
197 catch (IOException ex)
198 {
199 Logger.LogInformation(ex, "IOException while enumerating directory!");
201 Conflict(new ErrorMessageResponse(ErrorCode.IOError)
202 {
203 AdditionalData = ex.Message,
204 }));
205 }
206 },
207 null,
208 page,
209 pageSize,
211
223 [FromQuery] int? page,
224 [FromQuery] int? pageSize,
225 CancellationToken cancellationToken) => Directory(null, page, pageSize, cancellationToken);
226
235 [HttpPut]
241 {
242 ArgumentNullException.ThrowIfNull(model);
243
244 if (model.Path == null)
245 return BadRequest(new ErrorMessageResponse(ErrorCode.ModelValidationFailure));
246
248 return Forbid();
249
250 try
251 {
253 {
254 IsDirectory = true,
255 Path = model.Path,
256 };
257
259 async instance =>
260 {
261 var result = await instance
262 .Configuration
263 .CreateDirectory(model.Path, systemIdentity, cancellationToken);
264
265 if (!result.HasValue)
266 return Conflict(new ErrorMessageResponse(ErrorCode.ConfigurationContendedAccess));
267
268 return result.Value
269 ? Json(resultModel)
270 : this.Created(resultModel);
271 });
272 }
273 catch (IOException e)
274 {
275 Logger.LogInformation(e, "IOException while creating directory {path}!", model.Path);
276 return Conflict(new ErrorMessageResponse(ErrorCode.IOError)
277 {
278 Message = e.Message,
279 });
280 }
282 {
283 return Forbid();
284 }
285 }
286
294 [HttpDelete]
299 {
300 ArgumentNullException.ThrowIfNull(directory);
301
302 if (directory.Path == null)
303 return BadRequest(new ErrorMessageResponse(ErrorCode.ModelValidationFailure));
304
306 return Forbid();
307
308 try
309 {
311 async instance =>
312 {
313 var result = await instance
314 .Configuration
315 .DeleteDirectory(directory.Path, systemIdentity, cancellationToken);
316
317 if (!result.HasValue)
318 return Conflict(new ErrorMessageResponse(ErrorCode.ConfigurationContendedAccess));
319
320 return result.Value
321 ? NoContent()
322 : Conflict(new ErrorMessageResponse(ErrorCode.ConfigurationDirectoryNotEmpty));
323 });
324 }
326 {
327 return Forbid();
328 }
329 catch (IOException ex)
330 {
331 Logger.LogInformation(ex, "IOException while deleting directory!");
332 return Conflict(new ErrorMessageResponse(ErrorCode.IOError)
333 {
334 Message = ex.Message,
335 });
336 }
337 }
338
358 }
359}
Metadata about a server instance.
Definition Instance.cs:9
ConfigurationType? ConfigurationType
If IConfigurationFiles can be used on the Instance.
Definition Instance.cs:29
Represents a request to update a configuration file.
Represents an error message returned by the server.
Routes to a server actions.
Definition Routes.cs:9
const string List
The postfix for list operations.
Definition Routes.cs:113
const string File
To be paired with Configuration for accessing Models.IConfigurationFiles.
Definition Routes.cs:78
const string Configuration
The configuration controller.
Definition Routes.cs:73
ILogger< ApiController > Logger
The ILogger for the ApiController.
readonly IInstanceManager instanceManager
The IInstanceManager for the ComponentInterfacingController.
async ValueTask< IActionResult > WithComponentInstance(Func< IInstanceCore, ValueTask< IActionResult > > action, Models.Instance? instance=null)
Run a given action with the relevant IInstance.
async ValueTask< IActionResult > DeleteDirectory([FromBody] ConfigurationFileRequest directory, CancellationToken cancellationToken)
Deletes an empty directory .
ConfigurationController(IDatabaseContext databaseContext, IAuthenticationContext authenticationContext, ILogger< ConfigurationController > logger, IInstanceManager instanceManager, IIOManager ioManager, IApiHeadersProvider apiHeaders)
Initializes a new instance of the ConfigurationController class.
async ValueTask< IActionResult > Update([FromBody] ConfigurationFileRequest model, CancellationToken cancellationToken)
Write to a configuration file.
ValueTask< IActionResult > Directory(string? directoryPath, [FromQuery] int? page, [FromQuery] int? pageSize, CancellationToken cancellationToken)
Get the contents of a directory at a directoryPath .
readonly IIOManager ioManager
The IIOManager for the ConfigurationController.
ValueTask< IActionResult > List([FromQuery] int? page, [FromQuery] int? pageSize, CancellationToken cancellationToken)
Get the contents of the root configuration directory.
async ValueTask< IActionResult > File(string filePath, CancellationToken cancellationToken)
Get the contents of a file at a filePath .
bool ForbidDueToModeConflicts(string? path, out ISystemIdentity? systemIdentityToUse)
If a ForbidResult should be returned from actions due to conflicts with one or both of the Api....
async ValueTask< IActionResult > CreateDirectory([FromBody] ConfigurationFileRequest model, CancellationToken cancellationToken)
Create a configuration directory.
ComponentInterfacingController for operations that require an instance.
ISystemIdentity? SystemIdentity
The ISystemIdentity of User if applicable.
Interface for using filesystems.
Definition IIOManager.cs:13
bool PathContainsParentAccess(string path)
Check if a path contains the '..' parent directory accessor.
For creating and accessing authentication contexts.
Represents a user on the current global::System.Runtime.InteropServices.OSPlatform.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.
Definition ErrorCode.cs:12
ConfigurationType
The type of configuration allowed on an Instance.
ConfigurationRights
Rights for Models.IConfigurationFiles.
@ List
User may list files if the Models.Instance allows it.