Local file system storage as default (#4386)

* Local file system storage as default

Checks whether or not a file storage has been set. If not, it sets the local file system storage as default.

* Ensure check gets run
This commit is contained in:
d11n
2022-12-12 12:28:24 +01:00
committed by GitHub
parent 484cf9d8a2
commit f2cb07ac95
3 changed files with 30 additions and 25 deletions

View File

@@ -19,6 +19,8 @@ using BTCPayServer.Plugins.PointOfSale.Models;
using BTCPayServer.Services; using BTCPayServer.Services;
using BTCPayServer.Services.Apps; using BTCPayServer.Services.Apps;
using BTCPayServer.Services.Stores; using BTCPayServer.Services.Stores;
using BTCPayServer.Storage.Models;
using BTCPayServer.Storage.Services.Providers.FileSystemStorage.Configuration;
using ExchangeSharp; using ExchangeSharp;
using Fido2NetLib.Objects; using Fido2NetLib.Objects;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
@@ -87,13 +89,15 @@ namespace BTCPayServer.Hosting
var settings = (await _Settings.GetSettingAsync<MigrationSettings>()); var settings = (await _Settings.GetSettingAsync<MigrationSettings>());
if (settings is null) 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 }; settings = new MigrationSettings() { MigratedInvoiceTextSearchPages = int.MaxValue, MigratedTransactionLabels = int.MaxValue };
foreach (var prop in settings.GetType().GetProperties().Where(p => p.CanWrite && p.PropertyType == typeof(bool))) foreach (var prop in settings.GetType().GetProperties().Where(p => p.CanWrite && p.PropertyType == typeof(bool)))
{ {
prop.SetValue(settings, true); prop.SetValue(settings, true);
} }
// Ensure these checks still get run
settings.CheckedFirstRun = false; settings.CheckedFirstRun = false;
settings.FileSystemStorageAsDefault = false;
await _Settings.UpdateSetting(settings); await _Settings.UpdateSetting(settings);
} }
@@ -222,6 +226,21 @@ namespace BTCPayServer.Hosting
settings.MigrateWalletColors = true; settings.MigrateWalletColors = true;
await _Settings.UpdateSetting(settings); 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) catch (Exception ex)
{ {

View File

@@ -34,5 +34,6 @@ namespace BTCPayServer.Services
public bool AddStoreToPayout { get; set; } public bool AddStoreToPayout { get; set; }
public bool MigrateEmailServerDisableTLSCerts { get; set; } public bool MigrateEmailServerDisableTLSCerts { get; set; }
public bool MigrateWalletColors { get; set; } public bool MigrateWalletColors { get; set; }
public bool FileSystemStorageAsDefault { get; set; }
} }
} }

View File

@@ -1,6 +1,5 @@
using System; using System;
using System.IO; using System.IO;
using System.Runtime.InteropServices.ComTypes;
using BTCPayServer.Abstractions.Contracts; using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Configuration; using BTCPayServer.Configuration;
using BTCPayServer.Storage.Services; using BTCPayServer.Storage.Services;
@@ -25,49 +24,35 @@ namespace BTCPayServer.Storage
serviceCollection.AddSingleton<StoredFileRepository>(); serviceCollection.AddSingleton<StoredFileRepository>();
serviceCollection.AddSingleton<FileService>(); serviceCollection.AddSingleton<FileService>();
serviceCollection.AddSingleton<IFileService>(provider => provider.GetRequiredService<FileService>()); serviceCollection.AddSingleton<IFileService>(provider => provider.GetRequiredService<FileService>());
// serviceCollection.AddSingleton<IStorageProviderService, AmazonS3FileProviderService>();
serviceCollection.AddSingleton<IStorageProviderService, AzureBlobStorageFileProviderService>(); serviceCollection.AddSingleton<IStorageProviderService, AzureBlobStorageFileProviderService>();
serviceCollection.AddSingleton<IStorageProviderService, FileSystemFileProviderService>(); serviceCollection.AddSingleton<IStorageProviderService, FileSystemFileProviderService>();
// serviceCollection.AddSingleton<IStorageProviderService, GoogleCloudStorageFileProviderService>();
} }
public static void UseProviderStorage(this IApplicationBuilder builder, IOptions<DataDirectories> datadirs) public static void UseProviderStorage(this IApplicationBuilder builder, IOptions<DataDirectories> datadirs)
{ {
try try
{ {
DirectoryInfo dirInfo; var dirInfo = Directory.Exists(datadirs.Value.StorageDir)
if (!Directory.Exists(datadirs.Value.StorageDir)) ? new DirectoryInfo(datadirs.Value.StorageDir)
{ : Directory.CreateDirectory(datadirs.Value.StorageDir);
dirInfo = Directory.CreateDirectory(datadirs.Value.StorageDir);
}
else
{
dirInfo = new DirectoryInfo(datadirs.Value.StorageDir);
}
if (!Directory.Exists(datadirs.Value.TempDir)) if (!Directory.Exists(datadirs.Value.TempDir))
{ {
Directory.CreateDirectory(datadirs.Value.TempDir); Directory.CreateDirectory(datadirs.Value.TempDir);
} }
DirectoryInfo tmpdirInfo; var tmpdirInfo = Directory.Exists(datadirs.Value.TempStorageDir)
if (!Directory.Exists(datadirs.Value.TempStorageDir)) ? new DirectoryInfo(datadirs.Value.TempStorageDir)
{ : Directory.CreateDirectory(datadirs.Value.TempStorageDir);
tmpdirInfo = Directory.CreateDirectory(datadirs.Value.TempStorageDir);
}
else
{
tmpdirInfo = new DirectoryInfo(datadirs.Value.TempStorageDir);
}
builder.UseStaticFiles(new StaticFileOptions() builder.UseStaticFiles(new StaticFileOptions
{ {
ServeUnknownFileTypes = true, ServeUnknownFileTypes = true,
RequestPath = new PathString($"/{FileSystemFileProviderService.LocalStorageDirectoryName}"), RequestPath = new PathString($"/{FileSystemFileProviderService.LocalStorageDirectoryName}"),
FileProvider = new PhysicalFileProvider(dirInfo.FullName), FileProvider = new PhysicalFileProvider(dirInfo.FullName),
OnPrepareResponse = HandleStaticFileResponse() OnPrepareResponse = HandleStaticFileResponse()
}); });
builder.UseStaticFiles(new StaticFileOptions() builder.UseStaticFiles(new StaticFileOptions
{ {
ServeUnknownFileTypes = true, ServeUnknownFileTypes = true,
RequestPath = new PathString($"/{FileSystemFileProviderService.LocalStorageDirectoryName}tmp"), RequestPath = new PathString($"/{FileSystemFileProviderService.LocalStorageDirectoryName}tmp"),
@@ -78,7 +63,7 @@ namespace BTCPayServer.Storage
} }
catch (Exception e) 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)");
} }
} }