diff --git a/BTCPayServer.Abstractions/Contracts/ISettingsRepository.cs b/BTCPayServer.Abstractions/Contracts/ISettingsRepository.cs index 182443a3b..364ee2b43 100644 --- a/BTCPayServer.Abstractions/Contracts/ISettingsRepository.cs +++ b/BTCPayServer.Abstractions/Contracts/ISettingsRepository.cs @@ -1,3 +1,4 @@ +#nullable enable using System.Threading; using System.Threading.Tasks; @@ -5,8 +6,8 @@ namespace BTCPayServer.Abstractions.Contracts { public interface ISettingsRepository { - Task GetSettingAsync(string name = null); - Task UpdateSetting(T obj, string name = null); - Task WaitSettingsChanged(CancellationToken cancellationToken = default); + Task GetSettingAsync(string? name = null) where T : class; + Task UpdateSetting(T obj, string? name = null) where T : class; + Task WaitSettingsChanged(CancellationToken cancellationToken = default) where T : class; } } diff --git a/BTCPayServer/Services/SettingsRepository.cs b/BTCPayServer/Services/SettingsRepository.cs index 7f6772c08..5be3fbebb 100644 --- a/BTCPayServer/Services/SettingsRepository.cs +++ b/BTCPayServer/Services/SettingsRepository.cs @@ -1,3 +1,4 @@ +#nullable enable using System.Threading; using System.Threading.Tasks; using BTCPayServer.Abstractions.Contracts; @@ -19,9 +20,9 @@ namespace BTCPayServer.Services _EventAggregator = eventAggregator; } - public async Task GetSettingAsync(string name = null) + public async Task GetSettingAsync(string? name = null) where T : class { - name ??= typeof(T).FullName; + name ??= typeof(T).FullName ?? string.Empty; using (var ctx = _ContextFactory.CreateContext()) { var data = await ctx.Settings.Where(s => s.Id == name).FirstOrDefaultAsync(); @@ -30,7 +31,7 @@ namespace BTCPayServer.Services return Deserialize(data.Value); } } - public async Task UpdateSetting(T obj, string name = null) + public async Task UpdateSetting(T obj, string? name = null) where T : class { using (var ctx = _ContextFactory.CreateContext()) { @@ -51,9 +52,9 @@ namespace BTCPayServer.Services }); } - public SettingData UpdateSettingInContext(ApplicationDbContext ctx, T obj, string name = null) + public SettingData UpdateSettingInContext(ApplicationDbContext ctx, T obj, string? name = null) where T : class { - name ??= obj.GetType().FullName; + name ??= obj.GetType().FullName ?? string.Empty; var settings = new SettingData(); settings.Id = name; settings.Value = Serialize(obj); @@ -74,7 +75,7 @@ namespace BTCPayServer.Services return JsonConvert.SerializeObject(obj); } - public async Task WaitSettingsChanged(CancellationToken cancellationToken = default) + public async Task WaitSettingsChanged(CancellationToken cancellationToken = default) where T : class { return (await _EventAggregator.WaitNext>(cancellationToken)).Settings; } diff --git a/BTCPayServer/Storage/Models/StorageSettings.cs b/BTCPayServer/Storage/Models/StorageSettings.cs index 5d571c45e..18cf02499 100644 --- a/BTCPayServer/Storage/Models/StorageSettings.cs +++ b/BTCPayServer/Storage/Models/StorageSettings.cs @@ -9,7 +9,7 @@ namespace BTCPayServer.Storage.Models public StorageProvider Provider { get; set; } public string ConfigurationStr { get; set; } - [NotMapped] + [JsonIgnore] public JObject Configuration { get => JsonConvert.DeserializeObject(string.IsNullOrEmpty(ConfigurationStr) ? "{}" : ConfigurationStr); diff --git a/BTCPayServer/Storage/Services/FileService.cs b/BTCPayServer/Storage/Services/FileService.cs index 75cb16651..f532aaa24 100644 --- a/BTCPayServer/Storage/Services/FileService.cs +++ b/BTCPayServer/Storage/Services/FileService.cs @@ -1,3 +1,4 @@ +#nullable enable using System; using System.Collections.Generic; using System.Linq; @@ -27,6 +28,8 @@ namespace BTCPayServer.Storage.Services public async Task AddFile(IFormFile file, string userId) { var settings = await _SettingsRepository.GetSettingAsync(); + if (settings is null) + throw new InvalidOperationException("StoreSettings not configured"); var provider = GetProvider(settings); var storedFile = await provider.AddFile(file, settings); @@ -35,18 +38,22 @@ namespace BTCPayServer.Storage.Services return storedFile; } - public async Task GetFileUrl(Uri baseUri, string fileId) + public async Task GetFileUrl(Uri baseUri, string fileId) { var settings = await _SettingsRepository.GetSettingAsync(); + if (settings is null) + return null; var provider = GetProvider(settings); var storedFile = await _FileRepository.GetFile(fileId); return storedFile == null ? null : await provider.GetFileUrl(baseUri, storedFile, settings); } - public async Task GetTemporaryFileUrl(Uri baseUri, string fileId, DateTimeOffset expiry, + public async Task GetTemporaryFileUrl(Uri baseUri, string fileId, DateTimeOffset expiry, bool isDownload) { var settings = await _SettingsRepository.GetSettingAsync(); + if (settings is null) + return null; var provider = GetProvider(settings); var storedFile = await _FileRepository.GetFile(fileId); return storedFile == null ? null : await provider.GetTemporaryFileUrl(baseUri, storedFile, settings, expiry, isDownload); @@ -55,6 +62,8 @@ namespace BTCPayServer.Storage.Services public async Task RemoveFile(string fileId, string userId) { var settings = await _SettingsRepository.GetSettingAsync(); + if (settings is null) + return; var provider = GetProvider(settings); var storedFile = await _FileRepository.GetFile(fileId); if (string.IsNullOrEmpty(userId) || diff --git a/BTCPayServer/Storage/Services/Providers/IStorageProviderService.cs b/BTCPayServer/Storage/Services/Providers/IStorageProviderService.cs index 12d9b729c..29333122d 100644 --- a/BTCPayServer/Storage/Services/Providers/IStorageProviderService.cs +++ b/BTCPayServer/Storage/Services/Providers/IStorageProviderService.cs @@ -1,3 +1,4 @@ +#nullable enable using System; using System.Threading.Tasks; using BTCPayServer.Data;