RemoteItemMover, Startup/Exit handler refactor

This commit is contained in:
2023-07-26 13:51:17 +02:00
parent 3de71cbdbe
commit 0b36fb939c
23 changed files with 210 additions and 110 deletions

View File

@@ -1,28 +1,66 @@
using FileTime.Core.ContentAccess;
using FileTime.Core.Models;
using FileTime.Providers.LocalAdmin;
using Microsoft.Extensions.Logging;
namespace FileTime.Providers.Local;
public class LocalItemMover : IItemMover<ILocalContentProvider>
{
public Task RenameAsync(ILocalContentProvider contentProvider, FullName fullName, FullName newPath)
private readonly IAdminContentAccessorFactory _adminContentAccessorFactory;
private readonly IAdminContentProvider _adminContentProvider;
private readonly ILogger<LocalItemMover> _logger;
public LocalItemMover(
IAdminContentAccessorFactory adminContentAccessorFactory,
IAdminContentProvider adminContentProvider,
ILogger<LocalItemMover> logger)
{
var source = contentProvider.GetNativePath(fullName);
var destination = contentProvider.GetNativePath(newPath);
if (File.Exists(source.Path))
_adminContentAccessorFactory = adminContentAccessorFactory;
_adminContentProvider = adminContentProvider;
_logger = logger;
}
public async Task RenameAsync(ILocalContentProvider contentProvider, FullName fullName, FullName newPath)
{
_logger.LogTrace("Start renaming item {FullName}", fullName);
try
{
File.Move(source.Path, destination.Path);
var source = contentProvider.GetNativePath(fullName);
var destination = contentProvider.GetNativePath(newPath);
if (File.Exists(source.Path))
{
_logger.LogTrace("File exists with path {Path}", fullName);
File.Move(source.Path, destination.Path);
}
else if (Directory.Exists(source.Path))
{
_logger.LogTrace("Directory exists with path {Path}", fullName);
Directory.Move(source.Path, destination.Path);
}
else
{
_logger.LogTrace("No file or directory exists with path {Path}", fullName);
throw new FileNotFoundException(source.Path);
}
}
else if (Directory.Exists(source.Path))
catch (Exception ex)
{
Directory.Move(source.Path, destination.Path);
_logger.LogDebug(ex, "Failed to rename item {From} to {To}", fullName, newPath);
if (!_adminContentAccessorFactory.IsAdminModeSupported
|| ex is not UnauthorizedAccessException and not IOException)
{
_logger.LogTrace(
"Admin mode is disabled or exception is not an access denied one, not trying to rename {Path} as admin",
fullName
);
throw;
}
var adminItemMover = await _adminContentAccessorFactory.CreateAdminItemMoverAsync();
await adminItemMover.RenameAsync(_adminContentProvider, fullName, newPath);
}
else
{
throw new FileNotFoundException(source.Path);
}
return Task.CompletedTask;
}
}