Files
btcpayserver/BTCPayServer/Storage/Services/Providers/FileSystemStorage/FileSystemFileProviderService.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
3.0 KiB
C#

using System;
using System.IO;
using System.Threading.Tasks;
using BTCPayServer.Configuration;
using BTCPayServer.Services;
using BTCPayServer.Storage.Models;
using BTCPayServer.Storage.Services.Providers.FileSystemStorage.Configuration;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using TwentyTwenty.Storage;
using TwentyTwenty.Storage.Local;
namespace BTCPayServer.Storage.Services.Providers.FileSystemStorage
{
public class
FileSystemFileProviderService : BaseTwentyTwentyStorageFileProviderServiceBase<FileSystemStorageConfiguration>
{
private readonly BTCPayServerEnvironment _BtcPayServerEnvironment;
private readonly BTCPayServerOptions _Options;
private readonly IHttpContextAccessor _HttpContextAccessor;
public FileSystemFileProviderService(BTCPayServerEnvironment btcPayServerEnvironment,
BTCPayServerOptions options, IHttpContextAccessor httpContextAccessor)
{
_BtcPayServerEnvironment = btcPayServerEnvironment;
_Options = options;
_HttpContextAccessor = httpContextAccessor;
}
public const string LocalStorageDirectoryName = "LocalStorage";
public static string GetStorageDir(BTCPayServerOptions options)
{
return Path.Combine(options.DataDir, LocalStorageDirectoryName);
}
public override StorageProvider StorageProvider()
{
return Storage.Models.StorageProvider.FileSystem;
}
public override FileSystemStorageConfiguration GetProviderConfiguration(StorageSettings configuration)
{
return configuration.Configuration.ParseFileSystemStorageConfiguration();
}
protected override Task<IStorageProvider> GetStorageProvider(FileSystemStorageConfiguration configuration)
{
return Task.FromResult<IStorageProvider>(
new LocalStorageProvider(new DirectoryInfo(GetStorageDir(_Options)).FullName));
}
public override async Task<string> GetFileUrl(StoredFile storedFile, StorageSettings configuration)
{
var baseResult = await base.GetFileUrl(storedFile, configuration);
var url =
_HttpContextAccessor.HttpContext.Request.IsOnion()
? _BtcPayServerEnvironment.OnionUrl
: $"{_BtcPayServerEnvironment.ExpectedProtocol}://" +
$"{_BtcPayServerEnvironment.ExpectedHost}" +
$"{_Options.RootPath}{LocalStorageDirectoryName}";
return baseResult.Replace(new DirectoryInfo(GetStorageDir(_Options)).FullName, url,
StringComparison.InvariantCultureIgnoreCase);
}
public override Task<string> GetTemporaryFileUrl(StoredFile storedFile, StorageSettings configuration, DateTimeOffset expiry, bool isDownload,
BlobUrlAccess access = BlobUrlAccess.Read)
{
return GetFileUrl(storedFile, configuration);
}
}
}