mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-26 15:41:29 +01:00
91 lines
3.7 KiB
C#
91 lines
3.7 KiB
C#
#nullable enable
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using BTCPayServer.Abstractions.Contracts;
|
|
using BTCPayServer.Abstractions.Extensions;
|
|
using BTCPayServer.Services;
|
|
using BTCPayServer.Storage.Models;
|
|
using BTCPayServer.Storage.Services.Providers;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace BTCPayServer.Storage.Services
|
|
{
|
|
public class FileService : IFileService
|
|
{
|
|
private readonly StoredFileRepository _FileRepository;
|
|
private readonly IEnumerable<IStorageProviderService> _providers;
|
|
private readonly SettingsRepository _SettingsRepository;
|
|
|
|
public FileService(StoredFileRepository fileRepository, IEnumerable<IStorageProviderService> providers,
|
|
SettingsRepository settingsRepository)
|
|
{
|
|
_FileRepository = fileRepository;
|
|
_providers = providers;
|
|
_SettingsRepository = settingsRepository;
|
|
}
|
|
|
|
public async Task<bool> IsAvailable()
|
|
{
|
|
var settings = await _SettingsRepository.GetSettingAsync<StorageSettings>();
|
|
return settings is not null;
|
|
}
|
|
|
|
public async Task<IStoredFile> AddFile(IFormFile file, string userId)
|
|
{
|
|
var settings = await _SettingsRepository.GetSettingAsync<StorageSettings>();
|
|
if (settings is null)
|
|
throw new InvalidOperationException("StoreSettings not configured");
|
|
if (!file.FileName.IsValidFileName())
|
|
throw new InvalidOperationException("Invalid file name");
|
|
var provider = GetProvider(settings);
|
|
|
|
var storedFile = await provider.AddFile(file, settings);
|
|
storedFile.ApplicationUserId = userId;
|
|
await _FileRepository.AddFile(storedFile);
|
|
return storedFile;
|
|
}
|
|
|
|
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,
|
|
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);
|
|
}
|
|
|
|
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) ||
|
|
storedFile.ApplicationUserId.Equals(userId, StringComparison.InvariantCultureIgnoreCase))
|
|
{
|
|
await provider.RemoveFile(storedFile, settings);
|
|
await _FileRepository.RemoveFile(storedFile);
|
|
}
|
|
}
|
|
|
|
private IStorageProviderService GetProvider(StorageSettings storageSettings)
|
|
{
|
|
return _providers.First((service) => service.StorageProvider().Equals(storageSettings.Provider));
|
|
}
|
|
}
|
|
}
|