Refactor AdminContentProvider

This commit is contained in:
2023-08-24 10:37:04 +02:00
parent 5c62419f65
commit c2668d7270
27 changed files with 132 additions and 161 deletions

View File

@@ -9,6 +9,7 @@
<ItemGroup>
<ProjectReference Include="..\..\Core\FileTime.Core.Abstraction\FileTime.Core.Abstraction.csproj" />
<ProjectReference Include="..\..\Core\FileTime.Core.ContentAccess\FileTime.Core.ContentAccess.csproj" />
<ProjectReference Include="..\..\Server\FileTime.Server.Common.Abstractions\FileTime.Server.Common.Abstractions.csproj" />
<ProjectReference Include="..\FileTime.Providers.Remote.Abstractions\FileTime.Providers.Remote.Abstractions.csproj" />
</ItemGroup>

View File

@@ -2,18 +2,25 @@
using FileTime.Core.Enums;
using FileTime.Core.Models;
using FileTime.Core.Timeline;
using FileTime.Server.Common;
namespace FileTime.Providers.Remote;
public class RemoteContentProvider : ContentProviderBase, IRemoteContentProvider
public sealed class RemoteContentProvider : ContentProviderBase, IRemoteContentProvider
{
private readonly Func<Task<IRemoteConnection>> _remoteConnectionProvider;
public RemoteContentProvider(
ITimelessContentProvider timelessContentProvider,
Func<Task<IRemoteConnection>> remoteConnectionProvider,
string remoteName,
string name = "remote")
: base(name, timelessContentProvider)
{
_remoteConnectionProvider = remoteConnectionProvider;
}
public async Task<IRemoteConnection> GetRemoteConnectionAsync()
=> await _remoteConnectionProvider();
//TODO implement
public override Task<IItem> GetItemByNativePathAsync(NativePath nativePath, PointInTime pointInTime, bool forceResolve = false, AbsolutePathType forceResolvePathType = AbsolutePathType.Unknown, ItemInitializationSettings itemInitializationSettings = default) => throw new NotImplementedException();

View File

@@ -1,24 +1,25 @@
using FileTime.Core.ContentAccess;
using FileTime.Core.Models;
using FileTime.Server.Common;
using InitableService;
namespace FileTime.Providers.Remote;
public class RemoteContentWriter : IRemoteContentWriter
public class RemoteContentWriter : IContentWriter, IInitable<IRemoteContentProvider, string, NativePath, Guid>
{
private IRemoteConnection _remoteConnection = null!;
private IRemoteContentProvider _remoteContentProvider = null!;
private string _remoteContentProviderId = null!;
private NativePath _nativePath = null!;
private string _transactionId = null!;
private bool _isRemoteWriterInitialized;
public void Init(
IRemoteConnection remoteConnection,
IRemoteContentProvider remoteContentProvider,
string remoteContentProviderId,
NativePath nativePath,
Guid transactionId)
{
_remoteConnection = remoteConnection;
_remoteContentProvider = remoteContentProvider;
_remoteContentProviderId = remoteContentProviderId;
_nativePath = nativePath;
_transactionId = transactionId.ToString();
@@ -27,7 +28,7 @@ public class RemoteContentWriter : IRemoteContentWriter
public void Dispose()
{
if (!_isRemoteWriterInitialized) return;
_remoteConnection.CloseWriterAsync(_transactionId);
Task.Run(async () => await (await _remoteContentProvider.GetRemoteConnectionAsync()).CloseWriterAsync(_transactionId));
}
public int PreferredBufferSize => 10 * 1024 * 1024;
@@ -35,13 +36,13 @@ public class RemoteContentWriter : IRemoteContentWriter
public async Task WriteBytesAsync(byte[] data, int? index = null, CancellationToken cancellationToken = default)
{
if (!_isRemoteWriterInitialized) await InitializeRemoteWriter(_nativePath);
await _remoteConnection.WriteBytesAsync(_transactionId, data, index, cancellationToken);
await (await _remoteContentProvider.GetRemoteConnectionAsync()).WriteBytesAsync(_transactionId, data, index, cancellationToken);
}
public async Task FlushAsync(CancellationToken cancellationToken = default)
{
if (!_isRemoteWriterInitialized) return;
await _remoteConnection.FlushWriterAsync(_transactionId, cancellationToken);
await (await _remoteContentProvider.GetRemoteConnectionAsync()).FlushWriterAsync(_transactionId, cancellationToken);
}
public Stream AsStream() => new ContentAccessStream(this);
@@ -49,6 +50,6 @@ public class RemoteContentWriter : IRemoteContentWriter
private async Task InitializeRemoteWriter(NativePath nativePath)
{
_isRemoteWriterInitialized = true;
await _remoteConnection.InitializeRemoteWriter(_remoteContentProviderId, _transactionId, nativePath);
await (await _remoteContentProvider.GetRemoteConnectionAsync()).InitializeRemoteWriter(_remoteContentProviderId, _transactionId, nativePath);
}
}

View File

@@ -1,24 +1,26 @@
using FileTime.Core.ContentAccess;
using FileTime.Core.Models;
using FileTime.Server.Common;
using InitableService;
namespace FileTime.Providers.Remote;
public class RemoteItemCreator :
ItemCreatorBase<IRemoteContentProvider>,
IRemoteItemCreator
public class RemoteItemCreator :
ItemCreatorBase<IRemoteContentProvider>,
IInitable<IRemoteContentProvider, string>
{
private IRemoteConnection _remoteConnection = null!;
private IRemoteContentProvider _remoteContentProvider = null!;
private string _remoteContentProviderId = null!;
public void Init(IRemoteConnection remoteConnection, string remoteContentProviderId)
public void Init(IRemoteContentProvider remoteConnection, string remoteContentProviderId)
{
_remoteConnection = remoteConnection;
_remoteContentProvider = remoteConnection;
_remoteContentProviderId = remoteContentProviderId;
}
public override async Task CreateContainerAsync(IRemoteContentProvider contentProvider, FullName fullName)
=> await _remoteConnection.CreateContainerAsync(_remoteContentProviderId, fullName);
public override async Task CreateContainerAsync(IRemoteContentProvider contentProvider, FullName fullName)
=> await (await _remoteContentProvider.GetRemoteConnectionAsync()).CreateContainerAsync(_remoteContentProviderId, fullName);
public override async Task CreateElementAsync(IRemoteContentProvider contentProvider, FullName fullName)
=> await _remoteConnection.CreateElementAsync(_remoteContentProviderId, fullName);
public override async Task CreateElementAsync(IRemoteContentProvider contentProvider, FullName fullName)
=> await (await _remoteContentProvider.GetRemoteConnectionAsync()).CreateElementAsync(_remoteContentProviderId, fullName);
}

View File

@@ -1,17 +1,20 @@
using FileTime.Core.Models;
using FileTime.Server.Common;
using FileTime.Core.ContentAccess;
using FileTime.Core.Models;
using InitableService;
namespace FileTime.Providers.Remote;
public class RemoteItemDeleter : IRemoteItemDeleter
public class RemoteItemDeleter : IItemDeleter<IRemoteContentProvider>, IInitable<IRemoteContentProvider, string>
{
private IRemoteConnection _remoteConnection = null!;
private IRemoteContentProvider _remoteContentProvider = null!;
private string _remoteContentProviderId = null!;
public void Init(IRemoteConnection remoteConnection, string remoteContentProviderId)
public void Init(IRemoteContentProvider remoteConnection, string remoteContentProviderId)
{
_remoteConnection = remoteConnection;
_remoteContentProvider = remoteConnection;
_remoteContentProviderId = remoteContentProviderId;
}
public async Task DeleteAsync(IRemoteContentProvider contentProvider, FullName fullName)
=> await _remoteConnection.DeleteItemAsync(_remoteContentProviderId, fullName);
public async Task DeleteAsync(IRemoteContentProvider contentProvider, FullName fullName)
=> await (await _remoteContentProvider.GetRemoteConnectionAsync()).DeleteItemAsync(_remoteContentProviderId, fullName);
}

View File

@@ -1,19 +1,20 @@
using FileTime.Core.Models;
using FileTime.Server.Common;
using FileTime.Core.ContentAccess;
using FileTime.Core.Models;
using InitableService;
namespace FileTime.Providers.Remote;
public class RemoteItemMover : IRemoteItemMover
public class RemoteItemMover : IItemMover<IRemoteContentProvider>, IInitable<IRemoteContentProvider, string>
{
private IRemoteConnection _remoteConnection = null!;
private IRemoteContentProvider _remoteContentProvider = null!;
private string _remoteContentProviderId = null!;
public void Init(IRemoteConnection remoteConnection, string remoteContentProviderId)
public void Init(IRemoteContentProvider remoteConnection, string remoteContentProviderId)
{
_remoteConnection = remoteConnection;
_remoteContentProvider = remoteConnection;
_remoteContentProviderId = remoteContentProviderId;
}
public async Task RenameAsync(IRemoteContentProvider contentProvider, FullName fullName, FullName newPath)
=> await _remoteConnection.MoveItemAsync(_remoteContentProviderId, fullName, newPath);
=> await (await _remoteContentProvider.GetRemoteConnectionAsync()).MoveItemAsync(_remoteContentProviderId, fullName, newPath);
}

View File

@@ -8,10 +8,10 @@ public static class Startup
public static IServiceCollection AddRemoteProviderServices(this IServiceCollection serviceCollection)
{
serviceCollection.TryAddSingleton<IRemoteContentProvider, RemoteContentProvider>();
serviceCollection.TryAddTransient<IRemoteItemCreator, RemoteItemCreator>();
serviceCollection.TryAddTransient<IRemoteItemDeleter, RemoteItemDeleter>();
serviceCollection.TryAddTransient<IRemoteItemMover, RemoteItemMover>();
serviceCollection.TryAddTransient<IRemoteContentWriter, RemoteContentWriter>();
serviceCollection.TryAddTransient<RemoteItemCreator>();
serviceCollection.TryAddTransient<RemoteItemDeleter>();
serviceCollection.TryAddTransient<RemoteItemMover>();
serviceCollection.TryAddTransient<RemoteContentWriter>();
return serviceCollection;
}
}