Fix some NRE in the FileService

This commit is contained in:
nicolas.dorier
2021-03-06 13:36:36 +09:00
parent cc4e46d212
commit 97dd10edc0
5 changed files with 24 additions and 12 deletions

View File

@@ -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<T> GetSettingAsync<T>(string name = null);
Task UpdateSetting<T>(T obj, string name = null);
Task<T> WaitSettingsChanged<T>(CancellationToken cancellationToken = default);
Task<T?> GetSettingAsync<T>(string? name = null) where T : class;
Task UpdateSetting<T>(T obj, string? name = null) where T : class;
Task<T> WaitSettingsChanged<T>(CancellationToken cancellationToken = default) where T : class;
}
}

View File

@@ -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<T> GetSettingAsync<T>(string name = null)
public async Task<T?> GetSettingAsync<T>(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<T>(data.Value);
}
}
public async Task UpdateSetting<T>(T obj, string name = null)
public async Task UpdateSetting<T>(T obj, string? name = null) where T : class
{
using (var ctx = _ContextFactory.CreateContext())
{
@@ -51,9 +52,9 @@ namespace BTCPayServer.Services
});
}
public SettingData UpdateSettingInContext<T>(ApplicationDbContext ctx, T obj, string name = null)
public SettingData UpdateSettingInContext<T>(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<T> WaitSettingsChanged<T>(CancellationToken cancellationToken = default)
public async Task<T> WaitSettingsChanged<T>(CancellationToken cancellationToken = default) where T : class
{
return (await _EventAggregator.WaitNext<SettingsChanged<T>>(cancellationToken)).Settings;
}

View File

@@ -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<JObject>(string.IsNullOrEmpty(ConfigurationStr) ? "{}" : ConfigurationStr);

View File

@@ -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<StoredFile> AddFile(IFormFile file, string userId)
{
var settings = await _SettingsRepository.GetSettingAsync<StorageSettings>();
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<string> GetFileUrl(Uri baseUri, string fileId)
public async Task<string?> GetFileUrl(Uri baseUri, string fileId)
{
var settings = await _SettingsRepository.GetSettingAsync<StorageSettings>();
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<string> GetTemporaryFileUrl(Uri baseUri, string fileId, DateTimeOffset expiry,
public async Task<string?> GetTemporaryFileUrl(Uri baseUri, string fileId, DateTimeOffset expiry,
bool isDownload)
{
var settings = await _SettingsRepository.GetSettingAsync<StorageSettings>();
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<StorageSettings>();
if (settings is null)
return;
var provider = GetProvider(settings);
var storedFile = await _FileRepository.GetFile(fileId);
if (string.IsNullOrEmpty(userId) ||

View File

@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Threading.Tasks;
using BTCPayServer.Data;