Implements library management update library

This commit is contained in:
BinaryAlley committed 2024-11-17 21:02:10 +02:00
1 parent d0d53fd471
commit 291d1fa329
68 files changed
+1003 -110

No files matched your search

@@ -5,7 +5,6 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -40,7 +39,7 @@ public class DirectoriesController : Controller
public async Task<IActionResult> GetDirectories([FromQuery] string path, [FromQuery] bool includeHiddenElements, CancellationToken cancellationToken)
{
IEnumerable<DirectoryModel> response = await _apiHttpClient.GetAsync<DirectoryModel[]>(
$"directories/get-directories?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken: cancellationToken);
$"directories/get-directories?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken);
return Json(new { success = true, data = response });
}
@@ -54,7 +53,7 @@ public class DirectoriesController : Controller
public async Task<IActionResult> GetTreeDirectories([FromQuery] string path, [FromQuery] bool includeHiddenElements, CancellationToken cancellationToken)
{
IEnumerable<FileSystemTreeNodeModel> response = await _apiHttpClient.GetAsync<FileSystemTreeNodeModel[]>(
$"directories/get-tree-directories?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken: cancellationToken);
$"directories/get-tree-directories?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken);
return Json(new { success = true, data = response });
}
@@ -67,7 +66,7 @@ public class DirectoriesController : Controller
[HttpGet("get-directory-tree")]
public async Task<IActionResult> GetDirectoryTree([FromQuery] string path, [FromQuery] bool includeFiles, [FromQuery] bool includeHiddenElements, CancellationToken cancellationToken)
{
FileSystemTreeNodeModel[] response = await _apiHttpClient.GetAsync<FileSystemTreeNodeModel[]>($"directories/get-directory-tree?path={Uri.EscapeDataString(path)}&includeFiles={includeFiles}&includeHiddenElements={includeHiddenElements}", cancellationToken: cancellationToken);
FileSystemTreeNodeModel[] response = await _apiHttpClient.GetAsync<FileSystemTreeNodeModel[]>($"directories/get-directory-tree?path={Uri.EscapeDataString(path)}&includeFiles={includeFiles}&includeHiddenElements={includeHiddenElements}", cancellationToken);
return Json(new { success = true, data = response });
}
}
@@ -34,7 +34,7 @@ public class DrivesController : Controller
[HttpGet("get-drives")]
public async Task<IActionResult> GetDrives(CancellationToken cancellationToken)
{
FileSystemTreeNodeModel[] response = await _apiHttpClient.GetAsync<FileSystemTreeNodeModel[]>($"drives/get-drives", cancellationToken: cancellationToken);
FileSystemTreeNodeModel[] response = await _apiHttpClient.GetAsync<FileSystemTreeNodeModel[]>($"drives/get-drives", cancellationToken);
return Json(new { success = true, data = new { drives = response } });
}
}
@@ -34,7 +34,7 @@ public class FileSystemController : Controller
[HttpGet("get-type")]
public async Task<IActionResult> GetType(CancellationToken cancellationToken)
{
FileSystemTypeModel response = await _apiHttpClient.GetAsync<FileSystemTypeModel>($"file-system/get-type", cancellationToken: cancellationToken);
FileSystemTypeModel response = await _apiHttpClient.GetAsync<FileSystemTypeModel>($"file-system/get-type", cancellationToken);
return Json(new { success = true, data = new { platformType = response.PlatformType.ToString() } });
}
}
@@ -5,7 +5,6 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -40,7 +39,7 @@ public class FilesController : Controller
public async Task<IActionResult> GetTreeFiles([FromQuery] string path, [FromQuery] bool includeHiddenElements, CancellationToken cancellationToken)
{
IEnumerable<FileSystemTreeNodeModel> response = await _apiHttpClient.GetAsync<FileSystemTreeNodeModel[]>(
$"files/get-tree-files?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken: cancellationToken);
$"files/get-tree-files?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken);
return Json(new { success = true, data = response });
}
@@ -54,7 +53,7 @@ public class FilesController : Controller
public async Task<IActionResult> GetFiles([FromQuery] string path, [FromQuery] bool includeHiddenElements, CancellationToken cancellationToken)
{
IEnumerable<FileModel> response = await _apiHttpClient.GetAsync<FileModel[]>(
$"files/get-files?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken: cancellationToken);
$"files/get-files?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken);
return Json(new { success = true, data = response });
}
}
@@ -36,7 +36,7 @@ public class PathController : Controller
[HttpGet("get-path-root")]
public async Task<IActionResult> GetPathRoot([FromQuery] string path, CancellationToken cancellationToken)
{
PathSegmentModel response = await _apiHttpClient.GetAsync<PathSegmentModel>($"path/get-path-root?path={Uri.EscapeDataString(path)}", cancellationToken: cancellationToken);
PathSegmentModel response = await _apiHttpClient.GetAsync<PathSegmentModel>($"path/get-path-root?path={Uri.EscapeDataString(path)}", cancellationToken);
return Json(new { success = true, data = new { root = response } });
}
@@ -47,7 +47,7 @@ public class PathController : Controller
[HttpGet("get-path-separator")]
public async Task<IActionResult> GetPathSeparator(CancellationToken cancellationToken)
{
PathSeparatorModel response = await _apiHttpClient.GetAsync<PathSeparatorModel>($"path/get-path-separator", cancellationToken: cancellationToken);
PathSeparatorModel response = await _apiHttpClient.GetAsync<PathSeparatorModel>($"path/get-path-separator", cancellationToken);
return Json(new { success = true, data = new { pathSeparator = response.Separator } });
}
@@ -59,7 +59,7 @@ public class PathController : Controller
[HttpGet("get-path-parent")]
public async Task<IActionResult> GetPathParent([FromQuery] string path, CancellationToken cancellationToken)
{
PathSegmentModel[] response = await _apiHttpClient.GetAsync<PathSegmentModel[]>($"path/get-path-parent?path={Uri.EscapeDataString(path)}", cancellationToken: cancellationToken);
PathSegmentModel[] response = await _apiHttpClient.GetAsync<PathSegmentModel[]>($"path/get-path-parent?path={Uri.EscapeDataString(path)}", cancellationToken);
return Json(new { success = true, data = new { pathSegments = response } });
}
@@ -71,7 +71,7 @@ public class PathController : Controller
[HttpGet("split")]
public async Task<IActionResult> SplitPath([FromQuery] string path, CancellationToken cancellationToken)
{
PathSegmentModel[] response = await _apiHttpClient.GetAsync<PathSegmentModel[]>($"path/split?path={Uri.EscapeDataString(path)}", cancellationToken: cancellationToken);
PathSegmentModel[] response = await _apiHttpClient.GetAsync<PathSegmentModel[]>($"path/split?path={Uri.EscapeDataString(path)}", cancellationToken);
return Json(new { success = true, data = new { pathSegments = response } });
}
@@ -83,7 +83,7 @@ public class PathController : Controller
[HttpGet("validate")]
public async Task<IActionResult> ValidatePath([FromQuery] string path, CancellationToken cancellationToken)
{
PathValidModel response = await _apiHttpClient.GetAsync<PathValidModel>($"path/validate?path={Uri.EscapeDataString(path)}", cancellationToken: cancellationToken);
PathValidModel response = await _apiHttpClient.GetAsync<PathValidModel>($"path/validate?path={Uri.EscapeDataString(path)}", cancellationToken);
return Json(new { success = true, data = new { isValid = response.IsValid } });
}
@@ -96,7 +96,7 @@ public class PathController : Controller
[HttpGet("check-path-exists")]
public async Task<IActionResult> CheckPathExists([FromQuery] string path, [FromQuery] bool includeHiddenElements, CancellationToken cancellationToken)
{
PathExistsModel response = await _apiHttpClient.GetAsync<PathExistsModel>($"path/check-path-exists?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken: cancellationToken);
PathExistsModel response = await _apiHttpClient.GetAsync<PathExistsModel>($"path/check-path-exists?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken);
return Json(new { success = true, data = new { exists = response.Exists } });
}
}
@@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using System;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -30,24 +31,27 @@ public class LibraryManagementController : Controller
}
[HttpGet("item/{id?}")]
public IActionResult EditLibrary(Guid? id = null)
public async Task<IActionResult> EditLibrary(Guid? id = null, CancellationToken cancellationToken = default)
{
LibraryModel libraryModel = new()
{
Id = id,
LibraryType = LibraryType.Book,
ContentLocations = ["a", "b", "c"],
Title = ""
};
LibraryModel libraryModel = new();
if (id is not null)
libraryModel = await _apiHttpClient.GetAsync<LibraryModel>($"libraries/{id}", cancellationToken);
return View("/Views/Library/Management/Item.cshtml", libraryModel);
}
/// <summary>
/// Adds a new media library, or updates an existing one.
/// </summary>
/// <param name="data">The model containing the new library data.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
[ValidateAntiForgeryToken]
[HttpPost("item")]
public async Task<IActionResult> SaveLibraryAsync([FromBody] LibraryModel model)
public async Task<IActionResult> SaveLibrary([FromBody] LibraryModel data, CancellationToken cancellationToken)
{
LibraryModel response = model.Id.HasValue
? await _apiHttpClient.PutAsync<LibraryModel, LibraryModel>($"libraries/{model.Id}", model)
: await _apiHttpClient.PostAsync<LibraryModel, LibraryModel>("libraries", model);
return Json(new { success = true });
// call different API endpoints based on whether this is a new library or an existing one
LibraryModel response = data.Id.HasValue
? await _apiHttpClient.PutAsync<LibraryModel, LibraryModel>($"libraries/{data.Id}", data, cancellationToken)
: await _apiHttpClient.PostAsync<LibraryModel, LibraryModel>("libraries", data, cancellationToken);
return Json(new { success = true, data = response });
}
}
@@ -14,6 +14,7 @@ using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -113,15 +114,16 @@ public class AuthController : Controller
/// Registers a new account.
/// </summary>
/// <param name="data">User credentials used for registration.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
[AllowAnonymous]
[ValidateAntiForgeryToken]
[HttpPost("register")]
public async Task<IActionResult> Register([FromBody] RegisterRequestModel data)
public async Task<IActionResult> Register([FromBody] RegisterRequestModel data, CancellationToken cancellationToken)
{
// call different endpoints based on the view hidden field - registration for normal users, or initial application admin account setup
string endpoint = data.RegistrationType == "Admin" ? "initialization" : "auth/register";
// attempt API registration
RegisterResponseModel response = await _apiHttpClient.PostAsync<RegisterResponseModel, RegisterRequestModel>(endpoint, data);
RegisterResponseModel response = await _apiHttpClient.PostAsync<RegisterResponseModel, RegisterRequestModel>(endpoint, data, cancellationToken);
return Json(new { success = true, data = response });
}
@@ -129,15 +131,16 @@ public class AuthController : Controller
/// Logs in an account.
/// </summary>
/// <param name="data">User credentials used for login.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
[AllowAnonymous]
[ValidateAntiForgeryToken]
[HttpPost("login")]
public async Task<IActionResult> Login([FromBody] LoginRequestModel data)
public async Task<IActionResult> Login([FromBody] LoginRequestModel data, CancellationToken cancellationToken)
{
try
{
// attempt API registration
LoginResponseModel response = await _apiHttpClient.PostAsync<LoginResponseModel, LoginRequestModel>("auth/login", data);
LoginResponseModel response = await _apiHttpClient.PostAsync<LoginResponseModel, LoginRequestModel>("auth/login", data, cancellationToken);
// store the received token in a secure cookie
Response.Cookies.Delete("Token");
Response.Cookies.Append("Token", _cryptographyService.Encrypt(response.Token!), new CookieOptions
@@ -193,12 +196,13 @@ public class AuthController : Controller
/// Recovers the password of an account.
/// </summary>
/// <param name="data">User credentials used for password recovery.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
[AllowAnonymous]
[ValidateAntiForgeryToken]
[HttpPost("recover-password")]
public async Task<IActionResult> RecoverPassword([FromBody] RecoverPasswordRequestModel data)
public async Task<IActionResult> RecoverPassword([FromBody] RecoverPasswordRequestModel data, CancellationToken cancellationToken)
{
RecoverPasswordResponseModel response = await _apiHttpClient.PostAsync<RecoverPasswordResponseModel, RecoverPasswordRequestModel>("auth/recover-password", data);
RecoverPasswordResponseModel response = await _apiHttpClient.PostAsync<RecoverPasswordResponseModel, RecoverPasswordRequestModel>("auth/recover-password", data, cancellationToken);
return Json(new { success = true, data = response });
}
@@ -206,12 +210,13 @@ public class AuthController : Controller
/// Changes the password of an account.
/// </summary>
/// <param name="data">User credentials used for password change.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
[ValidateAntiForgeryToken]
[HttpPost("change-password")]
public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordRequestModel data)
public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordRequestModel data, CancellationToken cancellationToken)
{
data = data with { Username = User?.Identity?.Name }; // assign the currently logged in user as the user for which to change the password
ChangePasswordResponseModel response = await _apiHttpClient.PostAsync<ChangePasswordResponseModel, ChangePasswordRequestModel>("auth/change-password", data);
ChangePasswordResponseModel response = await _apiHttpClient.PostAsync<ChangePasswordResponseModel, ChangePasswordRequestModel>("auth/change-password", data, cancellationToken);
return Json(new { success = true, data = response });
}
}