2019-05-24 08:44:23 +02:00
|
|
|
using System;
|
2019-04-22 09:41:20 +02:00
|
|
|
using System.IO;
|
2022-03-02 12:22:11 +01:00
|
|
|
using BTCPayServer.Abstractions.Contracts;
|
2019-04-22 09:41:20 +02:00
|
|
|
using BTCPayServer.Configuration;
|
|
|
|
using BTCPayServer.Storage.Services;
|
|
|
|
using BTCPayServer.Storage.Services.Providers;
|
|
|
|
using BTCPayServer.Storage.Services.Providers.AzureBlobStorage;
|
|
|
|
using BTCPayServer.Storage.Services.Providers.FileSystemStorage;
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2019-06-25 12:23:10 +02:00
|
|
|
using Microsoft.AspNetCore.StaticFiles;
|
2019-04-22 09:41:20 +02:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.FileProviders;
|
2019-05-24 08:44:23 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2021-01-06 15:51:13 +01:00
|
|
|
using Microsoft.Extensions.Options;
|
2019-05-24 08:44:23 +02:00
|
|
|
using NBitcoin.Logging;
|
2019-04-22 09:41:20 +02:00
|
|
|
|
|
|
|
namespace BTCPayServer.Storage
|
|
|
|
{
|
|
|
|
public static class StorageExtensions
|
|
|
|
{
|
|
|
|
public static void AddProviderStorage(this IServiceCollection serviceCollection)
|
|
|
|
{
|
|
|
|
serviceCollection.AddSingleton<StoredFileRepository>();
|
|
|
|
serviceCollection.AddSingleton<FileService>();
|
2022-03-02 12:22:11 +01:00
|
|
|
serviceCollection.AddSingleton<IFileService>(provider => provider.GetRequiredService<FileService>());
|
2019-04-22 09:41:20 +02:00
|
|
|
serviceCollection.AddSingleton<IStorageProviderService, AzureBlobStorageFileProviderService>();
|
|
|
|
serviceCollection.AddSingleton<IStorageProviderService, FileSystemFileProviderService>();
|
|
|
|
}
|
|
|
|
|
2021-01-06 15:51:13 +01:00
|
|
|
public static void UseProviderStorage(this IApplicationBuilder builder, IOptions<DataDirectories> datadirs)
|
2019-04-22 09:41:20 +02:00
|
|
|
{
|
2019-05-24 08:44:23 +02:00
|
|
|
try
|
2019-04-22 09:41:20 +02:00
|
|
|
{
|
2022-12-12 12:28:24 +01:00
|
|
|
var dirInfo = Directory.Exists(datadirs.Value.StorageDir)
|
|
|
|
? new DirectoryInfo(datadirs.Value.StorageDir)
|
|
|
|
: Directory.CreateDirectory(datadirs.Value.StorageDir);
|
2023-01-06 14:18:07 +01:00
|
|
|
|
2022-03-31 11:54:25 +02:00
|
|
|
if (!Directory.Exists(datadirs.Value.TempDir))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(datadirs.Value.TempDir);
|
|
|
|
}
|
2019-04-22 09:41:20 +02:00
|
|
|
|
2022-12-12 12:28:24 +01:00
|
|
|
builder.UseStaticFiles(new StaticFileOptions
|
2019-05-24 08:44:23 +02:00
|
|
|
{
|
|
|
|
ServeUnknownFileTypes = true,
|
|
|
|
RequestPath = new PathString($"/{FileSystemFileProviderService.LocalStorageDirectoryName}"),
|
|
|
|
FileProvider = new PhysicalFileProvider(dirInfo.FullName),
|
2019-06-25 12:23:10 +02:00
|
|
|
OnPrepareResponse = HandleStaticFileResponse()
|
2019-05-24 08:44:23 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2022-12-12 12:28:24 +01:00
|
|
|
Logs.Utils.LogError(e, "Could not initialize the Local File Storage system (for uploading and storing files locally)");
|
2019-05-24 08:44:23 +02:00
|
|
|
}
|
2019-04-22 09:41:20 +02:00
|
|
|
}
|
2019-06-25 12:23:10 +02:00
|
|
|
|
|
|
|
private static Action<StaticFileResponseContext> HandleStaticFileResponse()
|
|
|
|
{
|
|
|
|
return context =>
|
|
|
|
{
|
2023-08-03 20:48:42 +02:00
|
|
|
context.Context.Response.Headers["Content-Disposition"] = context.Context.Request.Query.ContainsKey("download")? "attachment" : "inline";
|
2023-02-13 15:04:15 +01:00
|
|
|
context.Context.Response.Headers["Content-Security-Policy"] = "script-src ;";
|
2023-02-14 09:03:12 +01:00
|
|
|
context.Context.Response.Headers["X-Content-Type-Options"] = "nosniff";
|
2019-06-25 12:23:10 +02:00
|
|
|
};
|
|
|
|
}
|
2019-04-22 09:41:20 +02:00
|
|
|
}
|
|
|
|
}
|