Skip to content

Commit

Permalink
Refactor storage providers and update BackupController
Browse files Browse the repository at this point in the history
Refactored the storage provider interfaces and implementations:
- Added a new using directive for `Octockup.Server.Extensions` in `BackupController.cs`.
- Modified `GetProviders` in `BackupController.cs` to use `GetParametersKeys`.
- Updated `AddDbContext` in `ServiceCollectionExtensions.cs` to use `filename`.
- Added `ScpProvider` in `Program.cs`.
- Changed `FtpProvider` and `ScpProvider` to implement `IStorageProvider<BaseStorageParameters>`.
- Removed `Parameters` from `IStorageProvider` and added it to `IStorageProvider<TParams>`.
- Added `GetParametersKeys` extension method in `StorageProviderExtensions.cs`.
- Introduced `BaseStorageParameters` class to encapsulate common storage parameters.
  • Loading branch information
bvdcode committed Dec 21, 2024
1 parent dc9b1dd commit 8ff8f48
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Sources/Octockup.Server/Controllers/BackupController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Gridify.EntityFramework;
using Microsoft.AspNetCore.Mvc;
using Octockup.Server.Database;
using Octockup.Server.Extensions;
using Octockup.Server.Models.Dto;
using Octockup.Server.Providers.Storage;
using Microsoft.AspNetCore.Authorization;
Expand Down Expand Up @@ -45,7 +46,7 @@ public IEnumerable<StorageProviderInfo> GetProviders()
return _storageProviders.Select(p => new StorageProviderInfo()
{
Name = p.Name,
Parameters = p.Parameters
Parameters = p.GetParametersKeys()
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static IServiceCollection AddDbContext<TContext>(this IServiceCollection
string filename = FileSystemHelpers.GetFilePath(databaseFile);
return services
.AddDbContext<TContext>(options => options
.UseSqlite("Data Source=" + databaseFile)
.UseSqlite("Data Source=" + filename)
.UseLazyLoadingProxies());
}

Expand Down
19 changes: 19 additions & 0 deletions Sources/Octockup.Server/Extensions/StorageProviderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Octockup.Server.Providers.Storage;

namespace Octockup.Server.Extensions
{
public static class StorageProviderExtensions
{
public static IEnumerable<string> GetParametersKeys(this IStorageProvider storageProvider)
{
// storageProvider is IStorageProvider but actually is IStorageProvider<>, get generic type
var type = storageProvider
.GetType()
.GetInterfaces()
.First(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IStorageProvider<>))
.GetGenericArguments()
.First();
return type.GetProperties().Select(p => p.Name);
}
}
}
1 change: 1 addition & 0 deletions Sources/Octockup.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static void Main(string[] args)
builder.Services.AddControllers();
builder.Services
.AddStorageProvider<FtpProvider>()
.AddStorageProvider<ScpProvider>()
.AddHttpContextAccessor()
.AddValidatorsFromAssemblyContaining<Program>()
.AddFluentValidationAutoValidation()
Expand Down
11 changes: 11 additions & 0 deletions Sources/Octockup.Server/Providers/Storage/BaseStorageParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Octockup.Server.Providers.Storage
{
public class BaseStorageParameters
{
public string RemoteHost { get; set; } = string.Empty;
public int RemotePort { get; set; }
public string Username { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public string RemotePath { get; set; } = string.Empty;
}
}
11 changes: 3 additions & 8 deletions Sources/Octockup.Server/Providers/Storage/FtpProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@

namespace Octockup.Server.Providers.Storage
{
public class FtpProvider : IStorageProvider
public class FtpProvider : IStorageProvider<BaseStorageParameters>
{
public string Name => "FTP";

public IEnumerable<string> Parameters =>
[
"RemoteHost", "RemotePort", "RemotePath",
"Username", "Password"
];
public BaseStorageParameters Parameters { get; set; } = null!;

public IEnumerable<RemoteFileInfo> GetAllFiles()
{
throw new NotImplementedException();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

namespace Octockup.Server.Providers.Storage
{
public interface IStorageProvider<TParams> : IStorageProvider where TParams : class
{
TParams Parameters { get; set; }
}

public interface IStorageProvider
{
string Name { get; }
IEnumerable<string> Parameters { get; }
IEnumerable<RemoteFileInfo> GetAllFiles();
}
}
7 changes: 3 additions & 4 deletions Sources/Octockup.Server/Providers/Storage/ScpProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace Octockup.Server.Providers.Storage
{
public class ScpProvider : IStorageProvider
public class ScpProvider : IStorageProvider<BaseStorageParameters>
{
public string Name => throw new NotImplementedException();

public IEnumerable<string> Parameters => throw new NotImplementedException();
public string Name => "SCP";
public BaseStorageParameters Parameters { get; set; } = null!;

public IEnumerable<RemoteFileInfo> GetAllFiles()
{
Expand Down

0 comments on commit 8ff8f48

Please sign in to comment.