Admin ItemDeleter, Logging
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="DynamicData" Version="7.14.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using FileTime.Core.ContentAccess;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Providers.LocalAdmin;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FileTime.Providers.Local;
|
||||
|
||||
@@ -8,50 +9,75 @@ public class LocalItemCreator : ItemCreatorBase<ILocalContentProvider>
|
||||
{
|
||||
private readonly IAdminContentAccessorFactory _adminContentAccessorFactory;
|
||||
private readonly IAdminContentProvider _adminContentProvider;
|
||||
private readonly ILogger<LocalItemCreator> _logger;
|
||||
|
||||
public LocalItemCreator(
|
||||
IAdminContentAccessorFactory adminContentAccessorFactory,
|
||||
IAdminContentProvider adminContentProvider)
|
||||
IAdminContentProvider adminContentProvider,
|
||||
ILogger<LocalItemCreator> logger)
|
||||
{
|
||||
_adminContentAccessorFactory = adminContentAccessorFactory;
|
||||
_adminContentProvider = adminContentProvider;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public override async Task CreateContainerAsync(ILocalContentProvider contentProvider, FullName fullName)
|
||||
{
|
||||
_logger.LogTrace("Start creating container {FullName}", fullName);
|
||||
var path = contentProvider.GetNativePath(fullName).Path;
|
||||
if (Directory.Exists(path)) return;
|
||||
if (Directory.Exists(path))
|
||||
{
|
||||
_logger.LogTrace("Container with path {Path} already exists", path);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_logger.LogTrace("Trying to create container with path {Path}", path);
|
||||
Directory.CreateDirectory(path);
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
catch (UnauthorizedAccessException e)
|
||||
{
|
||||
if (!_adminContentAccessorFactory.IsAdminModeSupported) throw;
|
||||
_logger.LogDebug(e, "Failed to create container with path {Path}", path);
|
||||
if (!_adminContentAccessorFactory.IsAdminModeSupported)
|
||||
{
|
||||
_logger.LogTrace("Admin mode is disabled, not trying to create {Path} as admin", path);
|
||||
throw;
|
||||
}
|
||||
|
||||
var adminContentAccessor = await _adminContentAccessorFactory.CreateAdminItemCreatorAsync();
|
||||
await adminContentAccessor.CreateContainerAsync(_adminContentProvider, fullName);
|
||||
var adminItemCreator = await _adminContentAccessorFactory.CreateAdminItemCreatorAsync();
|
||||
await adminItemCreator.CreateContainerAsync(_adminContentProvider, fullName);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task CreateElementAsync(ILocalContentProvider contentProvider, FullName fullName)
|
||||
{
|
||||
_logger.LogTrace("Start creating element {FullName}", fullName);
|
||||
var path = contentProvider.GetNativePath(fullName).Path;
|
||||
if (File.Exists(path)) return;
|
||||
|
||||
if (File.Exists(path))
|
||||
{
|
||||
_logger.LogTrace("Element with path {Path} already exists", path);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_logger.LogTrace("Trying to create element with path {Path}", path);
|
||||
await using (File.Create(path))
|
||||
{
|
||||
}
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
catch (UnauthorizedAccessException e)
|
||||
{
|
||||
if (!_adminContentAccessorFactory.IsAdminModeSupported) throw;
|
||||
_logger.LogDebug(e, "Failed to create element with path {Path}", path);
|
||||
if (!_adminContentAccessorFactory.IsAdminModeSupported)
|
||||
{
|
||||
_logger.LogTrace("Admin mode is disabled, not trying to create {Path} as admin", path);
|
||||
throw;
|
||||
}
|
||||
|
||||
var adminContentAccessor = await _adminContentAccessorFactory.CreateAdminItemCreatorAsync();
|
||||
await adminContentAccessor.CreateElementAsync(_adminContentProvider, fullName);
|
||||
var adminItemCreator = await _adminContentAccessorFactory.CreateAdminItemCreatorAsync();
|
||||
await adminItemCreator.CreateElementAsync(_adminContentProvider, fullName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,65 @@
|
||||
using FileTime.Core.ContentAccess;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Providers.LocalAdmin;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FileTime.Providers.Local;
|
||||
|
||||
public class LocalItemDeleter : IItemDeleter<ILocalContentProvider>
|
||||
{
|
||||
public Task DeleteAsync(ILocalContentProvider contentProvider, FullName fullName)
|
||||
private readonly IAdminContentAccessorFactory _adminContentAccessorFactory;
|
||||
private readonly IAdminContentProvider _adminContentProvider;
|
||||
private readonly ILogger<LocalItemDeleter> _logger;
|
||||
|
||||
public LocalItemDeleter(
|
||||
IAdminContentAccessorFactory adminContentAccessorFactory,
|
||||
IAdminContentProvider adminContentProvider,
|
||||
ILogger<LocalItemDeleter> logger)
|
||||
{
|
||||
_adminContentAccessorFactory = adminContentAccessorFactory;
|
||||
_adminContentProvider = adminContentProvider;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(ILocalContentProvider contentProvider, FullName fullName)
|
||||
{
|
||||
_logger.LogTrace("Start deleting item {FullName}", fullName);
|
||||
var nativePath = contentProvider.GetNativePath(fullName).Path;
|
||||
|
||||
if (File.Exists(nativePath))
|
||||
try
|
||||
{
|
||||
File.Delete(nativePath);
|
||||
if (File.Exists(nativePath))
|
||||
{
|
||||
_logger.LogTrace("File exists with path {Path}", nativePath);
|
||||
File.Delete(nativePath);
|
||||
}
|
||||
else if (Directory.Exists(nativePath))
|
||||
{
|
||||
_logger.LogTrace("Directory exists with path {Path}", nativePath);
|
||||
Directory.Delete(nativePath, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogTrace("No file or directory exists with path {Path}", nativePath);
|
||||
throw new FileNotFoundException(nativePath);
|
||||
}
|
||||
}
|
||||
else if (Directory.Exists(nativePath))
|
||||
catch (Exception e)
|
||||
{
|
||||
Directory.Delete(nativePath, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new FileNotFoundException(nativePath);
|
||||
}
|
||||
_logger.LogDebug(e, "Failed to delete item with path {Path}", nativePath);
|
||||
|
||||
return Task.CompletedTask;
|
||||
if (!_adminContentAccessorFactory.IsAdminModeSupported
|
||||
|| e is not UnauthorizedAccessException and not IOException)
|
||||
{
|
||||
_logger.LogTrace(
|
||||
"Admin mode is disabled or exception is not an access denied one, not trying to create {Path} as admin",
|
||||
nativePath
|
||||
);
|
||||
throw;
|
||||
}
|
||||
|
||||
var adminItemDeleter = await _adminContentAccessorFactory.CreateAdminItemDeleterAsync();
|
||||
await adminItemDeleter.DeleteAsync(_adminContentProvider, fullName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,4 +6,5 @@ public interface IAdminContentAccessorFactory
|
||||
{
|
||||
bool IsAdminModeSupported { get; }
|
||||
Task<IRemoteItemCreator> CreateAdminItemCreatorAsync();
|
||||
Task<IRemoteItemDeleter> CreateAdminItemDeleterAsync();
|
||||
}
|
||||
@@ -33,4 +33,18 @@ public class AdminContentAccessorFactory : IAdminContentAccessorFactory
|
||||
.GetRequiredService<IRemoteItemCreator>();
|
||||
return adminItemCreator;
|
||||
}
|
||||
|
||||
public async Task<IRemoteItemDeleter> CreateAdminItemDeleterAsync()
|
||||
{
|
||||
await _adminElevationManager.CreateAdminInstanceIfNecessaryAsync();
|
||||
var connection = await _adminElevationManager.CreateConnectionAsync();
|
||||
|
||||
Debug.Assert(connection != null);
|
||||
|
||||
var adminItemDeleter = _serviceProvider.GetInitableResolver(
|
||||
connection,
|
||||
_adminElevationManager.ProviderName)
|
||||
.GetRequiredService<IRemoteItemDeleter>();
|
||||
return adminItemDeleter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using FileTime.Core.ContentAccess;
|
||||
using FileTime.Server.Common;
|
||||
using InitableService;
|
||||
|
||||
namespace FileTime.Providers.Remote;
|
||||
|
||||
public interface IRemoteItemDeleter : IItemDeleter<IRemoteContentProvider>, IInitable<IRemoteConnection, string>
|
||||
{
|
||||
|
||||
}
|
||||
17
src/Providers/FileTime.Providers.Remote/RemoteItemDeleter.cs
Normal file
17
src/Providers/FileTime.Providers.Remote/RemoteItemDeleter.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Server.Common;
|
||||
|
||||
namespace FileTime.Providers.Remote;
|
||||
|
||||
public class RemoteItemDeleter : IRemoteItemDeleter
|
||||
{
|
||||
private IRemoteConnection _remoteConnection = null!;
|
||||
private string _remoteContentProviderId = null!;
|
||||
public void Init(IRemoteConnection remoteConnection, string remoteContentProviderId)
|
||||
{
|
||||
_remoteConnection = remoteConnection;
|
||||
_remoteContentProviderId = remoteContentProviderId;
|
||||
}
|
||||
public async Task DeleteAsync(IRemoteContentProvider contentProvider, FullName fullName)
|
||||
=> await _remoteConnection.DeleteItemAsync(_remoteContentProviderId, fullName);
|
||||
}
|
||||
@@ -10,6 +10,7 @@ public static class Startup
|
||||
{
|
||||
serviceCollection.TryAddSingleton<IRemoteContentProvider, RemoteContentProvider>();
|
||||
serviceCollection.TryAddTransient<IRemoteItemCreator, RemoteItemCreator>();
|
||||
serviceCollection.TryAddTransient<IRemoteItemDeleter, RemoteItemDeleter>();
|
||||
return serviceCollection;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user