btcpayserver/BTCPayServer/Storage/Services/FileService.cs
Andrew Camilleri b184360eb7 Abstracted cloud storage - Amazon/Google/Azure/Local (#708)
* wip

* add in storage system

* ui fixes

* fix settings ui

* Add Files Crud UI

* add titles

* link files to users

* add migration

* set blob to public

* remove base 64 read code

* fix file query model init

* move view model to own file

* fix local root path

* use datadir for local storage

* move to services

* add direct file url

* try fix tests

* remove magic string

* remove other magic strings

* show error message on unsupported provider

* fix asp net version

* redirect to storage settings if provider is not supported

* start writing tests

* fix tests

* fix test again

* add some more to the tests

* more tests

* try making local provider work on tests

* fix formfile

* fix small issue with returning deleted file

* check if returned data is null for deleted file

* validate azure Container name

* more state fixes

* change azure test trait

* add tmp file url generator

* fix tests

* small clean

* disable amazon and  google


comment out unused code for now


comment out google/amazon
2019-04-22 16:41:20 +09:00

72 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Services;
using BTCPayServer.Storage.Models;
using BTCPayServer.Storage.Services.Providers;
using Microsoft.AspNetCore.Http;
namespace BTCPayServer.Storage.Services
{
public class FileService
{
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<StoredFile> AddFile(IFormFile file, string userId)
{
var settings = await _SettingsRepository.GetSettingAsync<StorageSettings>();
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(string fileId)
{
var settings = await _SettingsRepository.GetSettingAsync<StorageSettings>();
var provider = GetProvider(settings);
var storedFile = await _FileRepository.GetFile(fileId);
return storedFile == null ? null: await provider.GetFileUrl(storedFile, settings);
}
public async Task<string> GetTemporaryFileUrl(string fileId, DateTimeOffset expiry, bool isDownload)
{
var settings = await _SettingsRepository.GetSettingAsync<StorageSettings>();
var provider = GetProvider(settings);
var storedFile = await _FileRepository.GetFile(fileId);
return storedFile == null ? null: await provider.GetTemporaryFileUrl(storedFile, settings,expiry,isDownload);
}
public async Task RemoveFile(string fileId, string userId)
{
var settings = await _SettingsRepository.GetSettingAsync<StorageSettings>();
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.FirstOrDefault((service) => service.StorageProvider().Equals(storageSettings.Provider));
}
}
}