Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local file system storage as default #4386

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion BTCPayServer/Hosting/MigrationStartupTask.cs
Expand Up @@ -19,6 +19,8 @@
using BTCPayServer.Services;
using BTCPayServer.Services.Apps;
using BTCPayServer.Services.Stores;
using BTCPayServer.Storage.Models;
using BTCPayServer.Storage.Services.Providers.FileSystemStorage.Configuration;
using ExchangeSharp;
using Fido2NetLib.Objects;
using Microsoft.AspNetCore.Identity;
Expand Down Expand Up @@ -87,13 +89,15 @@ public async Task ExecuteAsync(CancellationToken cancellationToken = default)
var settings = (await _Settings.GetSettingAsync<MigrationSettings>());
if (settings is null)
{
// If it is null, then it's the first run: let's skip all the migrations by migration flags to true
// If it is null, then it's the first run: let's skip all the migrations by setting flags to true
settings = new MigrationSettings() { MigratedInvoiceTextSearchPages = int.MaxValue, MigratedTransactionLabels = int.MaxValue };
foreach (var prop in settings.GetType().GetProperties().Where(p => p.CanWrite && p.PropertyType == typeof(bool)))
{
prop.SetValue(settings, true);
}
// Ensure these checks still get run
settings.CheckedFirstRun = false;
settings.FileSystemStorageAsDefault = false;
await _Settings.UpdateSetting(settings);
}

Expand Down Expand Up @@ -222,6 +226,21 @@ public async Task ExecuteAsync(CancellationToken cancellationToken = default)
settings.MigrateWalletColors = true;
await _Settings.UpdateSetting(settings);
}
if (!settings.FileSystemStorageAsDefault)
{
var storageSettings = await _Settings.GetSettingAsync<StorageSettings>();
if (storageSettings is null)
{
storageSettings = new StorageSettings
{
Provider = StorageProvider.FileSystem,
Configuration = JObject.FromObject(new FileSystemStorageConfiguration())
};
await _Settings.UpdateSetting(storageSettings);
}
settings.FileSystemStorageAsDefault = true;
await _Settings.UpdateSetting(settings);
}
}
catch (Exception ex)
{
Expand Down
1 change: 1 addition & 0 deletions BTCPayServer/Services/MigrationSettings.cs
Expand Up @@ -34,5 +34,6 @@ public override string ToString()
public bool AddStoreToPayout { get; set; }
public bool MigrateEmailServerDisableTLSCerts { get; set; }
public bool MigrateWalletColors { get; set; }
public bool FileSystemStorageAsDefault { get; set; }
}
}
33 changes: 9 additions & 24 deletions BTCPayServer/Storage/StorageExtensions.cs
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Runtime.InteropServices.ComTypes;
using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Configuration;
using BTCPayServer.Storage.Services;
Expand All @@ -25,49 +24,35 @@ public static void AddProviderStorage(this IServiceCollection serviceCollection)
serviceCollection.AddSingleton<StoredFileRepository>();
serviceCollection.AddSingleton<FileService>();
serviceCollection.AddSingleton<IFileService>(provider => provider.GetRequiredService<FileService>());
// serviceCollection.AddSingleton<IStorageProviderService, AmazonS3FileProviderService>();
serviceCollection.AddSingleton<IStorageProviderService, AzureBlobStorageFileProviderService>();
serviceCollection.AddSingleton<IStorageProviderService, FileSystemFileProviderService>();
// serviceCollection.AddSingleton<IStorageProviderService, GoogleCloudStorageFileProviderService>();
}

public static void UseProviderStorage(this IApplicationBuilder builder, IOptions<DataDirectories> datadirs)
{
try
{
DirectoryInfo dirInfo;
if (!Directory.Exists(datadirs.Value.StorageDir))
{
dirInfo = Directory.CreateDirectory(datadirs.Value.StorageDir);
}
else
{
dirInfo = new DirectoryInfo(datadirs.Value.StorageDir);
}
var dirInfo = Directory.Exists(datadirs.Value.StorageDir)
? new DirectoryInfo(datadirs.Value.StorageDir)
: Directory.CreateDirectory(datadirs.Value.StorageDir);

if (!Directory.Exists(datadirs.Value.TempDir))
{
Directory.CreateDirectory(datadirs.Value.TempDir);
}

DirectoryInfo tmpdirInfo;
if (!Directory.Exists(datadirs.Value.TempStorageDir))
{
tmpdirInfo = Directory.CreateDirectory(datadirs.Value.TempStorageDir);
}
else
{
tmpdirInfo = new DirectoryInfo(datadirs.Value.TempStorageDir);
}
var tmpdirInfo = Directory.Exists(datadirs.Value.TempStorageDir)
? new DirectoryInfo(datadirs.Value.TempStorageDir)
: Directory.CreateDirectory(datadirs.Value.TempStorageDir);
Comment on lines -38 to +46
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are just simplifications of the existing code.


builder.UseStaticFiles(new StaticFileOptions()
builder.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true,
RequestPath = new PathString($"/{FileSystemFileProviderService.LocalStorageDirectoryName}"),
FileProvider = new PhysicalFileProvider(dirInfo.FullName),
OnPrepareResponse = HandleStaticFileResponse()
});
builder.UseStaticFiles(new StaticFileOptions()
builder.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true,
RequestPath = new PathString($"/{FileSystemFileProviderService.LocalStorageDirectoryName}tmp"),
Expand All @@ -78,7 +63,7 @@ public static void UseProviderStorage(this IApplicationBuilder builder, IOptions
}
catch (Exception e)
{
Logs.Utils.LogError(e, $"Could not initialize the Local File Storage system(uploading and storing files locally)");
Logs.Utils.LogError(e, "Could not initialize the Local File Storage system (for uploading and storing files locally)");
}
}

Expand Down