btcpayserver/BTCPayServer/Storage/Services/Providers/BaseTwentyTwentyStorageFileProviderServiceBase.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

77 lines
3.5 KiB
C#

using System;
using System.Threading.Tasks;
using BTCPayServer.Storage.Models;
using BTCPayServer.Storage.Services.Providers.Models;
using Microsoft.AspNetCore.Http;
using TwentyTwenty.Storage;
namespace BTCPayServer.Storage.Services.Providers
{
public abstract class
BaseTwentyTwentyStorageFileProviderServiceBase<TStorageConfiguration> : IStorageProviderService
where TStorageConfiguration : IBaseStorageConfiguration
{
public abstract StorageProvider StorageProvider();
public virtual async Task<StoredFile> AddFile(IFormFile file, StorageSettings configuration)
{
//respect https://www.microsoftpressstore.com/articles/article.aspx?p=2224058&seqNum=8 in naming
var storageFileName = $"{Guid.NewGuid()}-{file.FileName.ToLowerInvariant()}";
var providerConfiguration = GetProviderConfiguration(configuration);
var provider = await GetStorageProvider(providerConfiguration);
using (var fileStream = file.OpenReadStream())
{
await provider.SaveBlobStreamAsync(providerConfiguration.ContainerName, storageFileName, fileStream,
new BlobProperties()
{
ContentType = file.ContentType,
ContentDisposition = file.ContentDisposition,
Security = BlobSecurity.Public,
});
}
return new StoredFile()
{
Timestamp = DateTime.Now,
FileName = file.FileName,
StorageFileName = storageFileName
};
}
public virtual async Task<string> GetFileUrl(StoredFile storedFile, StorageSettings configuration)
{
var providerConfiguration = GetProviderConfiguration(configuration);
var provider = await GetStorageProvider(providerConfiguration);
return provider.GetBlobUrl(providerConfiguration.ContainerName, storedFile.StorageFileName);
}
public virtual async Task<string> GetTemporaryFileUrl(StoredFile storedFile, StorageSettings configuration,
DateTimeOffset expiry, bool isDownload, BlobUrlAccess access = BlobUrlAccess.Read)
{
var providerConfiguration = GetProviderConfiguration(configuration);
var provider = await GetStorageProvider(providerConfiguration);
if (isDownload)
{
var descriptor =
await provider.GetBlobDescriptorAsync(providerConfiguration.ContainerName,
storedFile.StorageFileName);
return provider.GetBlobSasUrl(providerConfiguration.ContainerName, storedFile.StorageFileName, expiry,
true, storedFile.FileName, descriptor.ContentType, access);
}
return provider.GetBlobSasUrl(providerConfiguration.ContainerName, storedFile.StorageFileName, expiry,
false, null, null, access);
}
public async Task RemoveFile(StoredFile storedFile, StorageSettings configuration)
{
var providerConfiguration = GetProviderConfiguration(configuration);
var provider = await GetStorageProvider(providerConfiguration);
await provider.DeleteBlobAsync(providerConfiguration.ContainerName, storedFile.StorageFileName);
}
public abstract TStorageConfiguration GetProviderConfiguration(StorageSettings configuration);
protected abstract Task<IStorageProvider> GetStorageProvider(TStorageConfiguration configuration);
}
}