Implement CreateDirectory API

This commit is contained in:
Cyberboss
2018-08-20 13:26:32 -04:00
parent f58c71adc7
commit d1d78ba3a7
6 changed files with 79 additions and 0 deletions
+4
View File
@@ -396,6 +396,10 @@ If the path is empty, the root directory will be retrieved. The @ref Tgstation.S
If you do not have access to list the requested directory, a 403 response will be returned. If the path actually represents a file or the directory no longer exists a 410 response will be returned.
To create an empty config directory use the following request
I POST "/Config/List/<path to directory>" => @ref Tgstation.Server.Api.Models.ConfigurationFile
To get the content of a static file use the following method
I GET "/Config/File/<path to file>" => @ref Tgstation.Server.Api.Models.ConfigurationFile
@@ -347,6 +347,22 @@ namespace Tgstation.Server.Host.Components.StaticFiles
return result;
}
/// <inheritdoc />
public async Task<bool> CreateDirectory(string configurationRelativePath, ISystemIdentity systemIdentity, CancellationToken cancellationToken)
{
await EnsureDirectories(cancellationToken).ConfigureAwait(false);
var path = ValidateConfigRelativePath(configurationRelativePath);
bool? result = null;
void DoCreate() => result = synchronousIOManager.CreateDirectory(path, cancellationToken);
if (systemIdentity == null)
await Task.Factory.StartNew(DoCreate, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current).ConfigureAwait(false);
else
await systemIdentity.RunImpersonated(DoCreate, cancellationToken).ConfigureAwait(false);
return result.Value;
}
/// <inheritdoc />
public Task StartAsync(CancellationToken cancellationToken) => EnsureDirectories(cancellationToken);
@@ -48,6 +48,15 @@ namespace Tgstation.Server.Host.Components.StaticFiles
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="Api.Models.ConfigurationFile"/> of the file</returns>
Task<ConfigurationFile> Read(string configurationRelativePath, ISystemIdentity systemIdentity, CancellationToken cancellationToken);
/// <summary>
/// Create an empty directory at <paramref name="configurationRelativePath"/>
/// </summary>
/// <param name="configurationRelativePath">The relative path in the Configuration directory</param>
/// <param name="systemIdentity">The <see cref="ISystemIdentity"/> for the operation. If <see langword="null"/>, the operation will be performed as the user of the <see cref="Core.Application"/></param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation. Usage may result in partial writes</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in <see langword="true"/> if the directory already existed, <see langword="false"/> otherwise</returns>
Task<bool> CreateDirectory(string configurationRelativePath, ISystemIdentity systemIdentity, CancellationToken cancellationToken);
/// <summary>
/// Writes to a given <paramref name="configurationRelativePath"/>
/// </summary>
@@ -127,5 +127,37 @@ namespace Tgstation.Server.Host.Controllers
/// <inheritdoc />
[TgsAuthorize(ConfigurationRights.List)]
public override Task<IActionResult> List(CancellationToken cancellationToken) => Directory(null, cancellationToken);
/// <summary>
/// Create an empty directory at a <paramref name="directoryPath"/>
/// </summary>
/// <param name="directoryPath">The path of the directory to get</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> for the operation</returns>
[HttpPut("List/{*directoryPath}")]
[TgsAuthorize(ConfigurationRights.List)]
public async Task<IActionResult> CreateDirectory(string directoryPath, CancellationToken cancellationToken)
{
if (ForbidDueToModeConflicts())
return Forbid();
try
{
var result = new ConfigurationFile
{
IsDirectory = true,
Path = directoryPath
};
return await instanceManager.GetInstance(Instance).Configuration.CreateDirectory(directoryPath, AuthenticationContext.SystemIdentity, cancellationToken).ConfigureAwait(false) ? (IActionResult)Json(result) : StatusCode((int)HttpStatusCode.Created);
}
catch (NotImplementedException)
{
return StatusCode((int)HttpStatusCode.NotImplemented);
}
catch (UnauthorizedAccessException)
{
return Forbid();
}
}
}
}
@@ -24,6 +24,14 @@ namespace Tgstation.Server.Host.IO
/// <returns>A <see cref="IEnumerable{T}"/> of directory names in <paramref name="path"/></returns>
IEnumerable<string> GetDirectories(string path, CancellationToken cancellationToken);
/// <summary>
/// Create an empty directory at <paramref name="path"/>
/// </summary>
/// <param name="path">The path to create</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation. Usage may result in partial writes</param>
/// <returns><see langword="true"/> if the directory already existed, <see langword="false"/> otherwise</returns>
bool CreateDirectory(string path, CancellationToken cancellationToken);
/// <summary>
/// Read the <see cref="byte"/>s of a file at a given <paramref name="path"/>
/// </summary>
@@ -11,6 +11,16 @@ namespace Tgstation.Server.Host.IO
/// <inheritdoc />
sealed class SynchronousIOManager : ISynchronousIOManager
{
/// <inheritdoc />
public bool CreateDirectory(string path, CancellationToken cancellationToken)
{
if (IsDirectory(path))
return true;
cancellationToken.ThrowIfCancellationRequested();
Directory.CreateDirectory(path);
return false;
}
/// <inheritdoc />
public IEnumerable<string> GetDirectories(string path, CancellationToken cancellationToken)
{