Kukks 2023-08-03 20:48:42 +02:00
parent ac64f5e395
commit 2538f3d8f6
No known key found for this signature in database
GPG Key ID: 8E5530D9D1C93097
4 changed files with 54 additions and 70 deletions

View File

@ -62,7 +62,7 @@ namespace BTCPayServer.Storage.Services.Providers.FileSystemStorage
var fullPath = Path.Combine(_datadirs.Value.TempStorageDir, name);
if (!File.Exists(fullPath))
{
File.Create(fullPath).Dispose();
await File.Create(fullPath).DisposeAsync();
}
await File.WriteAllTextAsync(Path.Combine(_datadirs.Value.TempStorageDir, name), JsonConvert.SerializeObject(localFileDescriptor));

View File

@ -0,0 +1,52 @@
using System;
using System.IO;
using System.Net.Mime;
using System.Threading.Tasks;
using BTCPayServer.Configuration;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
namespace BTCPayServer.Storage.Services.Providers.FileSystemStorage;
public class TemporaryLocalFileController : Controller
{
private readonly StoredFileRepository _storedFileRepository;
private readonly IOptions<DataDirectories> _dataDirectories;
public TemporaryLocalFileController(StoredFileRepository storedFileRepository,
IOptions<DataDirectories> dataDirectories)
{
_storedFileRepository = storedFileRepository;
_dataDirectories = dataDirectories;
}
[HttpGet($"~/{FileSystemFileProviderService.LocalStorageDirectoryName}tmp/{{tmpFileId}}")]
public async Task<IActionResult> GetTmpLocalFile(string tmpFileId)
{
var path = Path.Combine(_dataDirectories.Value.TempStorageDir, tmpFileId);
if (!System.IO.File.Exists(path))
{
return NotFound();
}
var text = await System.IO.File.ReadAllTextAsync(path);
var descriptor = JsonConvert.DeserializeObject<TemporaryLocalFileDescriptor>(text);
if (descriptor.Expiry < DateTime.UtcNow)
{
System.IO.File.Delete(path);
return NotFound();
}
var storedFile = _storedFileRepository.GetFile(descriptor.FileId).GetAwaiter().GetResult();
ControllerContext.HttpContext.Response.Headers["Content-Disposition"] =
ControllerContext.HttpContext.Request.Query.ContainsKey("download") ? "attachment" : "inline";
ControllerContext.HttpContext.Response.Headers["Content-Security-Policy"] = "script-src ;";
ControllerContext.HttpContext.Response.Headers["X-Content-Type-Options"] = "nosniff";
path = Path.Combine(_dataDirectories.Value.StorageDir, storedFile.StorageFileName);
var fileContent = await System.IO.File.ReadAllBytesAsync(path);
return File(fileContent, MediaTypeNames.Application.Octet, storedFile.FileName);
}
}

View File

@ -1,53 +0,0 @@
using System;
using System.IO;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.FileProviders.Physical;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
namespace BTCPayServer.Storage.Services.Providers.FileSystemStorage
{
public class TemporaryLocalFileProvider : IFileProvider
{
private readonly DirectoryInfo _fileRoot;
private readonly StoredFileRepository _storedFileRepository;
private readonly DirectoryInfo _root;
public TemporaryLocalFileProvider(DirectoryInfo tmpRoot, DirectoryInfo fileRoot, StoredFileRepository storedFileRepository)
{
_fileRoot = fileRoot;
_storedFileRepository = storedFileRepository;
_root = tmpRoot;
}
public IFileInfo GetFileInfo(string tmpFileId)
{
tmpFileId = tmpFileId.TrimStart('/', '\\');
var path = Path.Combine(_root.FullName, tmpFileId);
if (!File.Exists(path))
{
return new NotFoundFileInfo(tmpFileId);
}
var text = File.ReadAllText(path);
var descriptor = JsonConvert.DeserializeObject<TemporaryLocalFileDescriptor>(text);
if (descriptor.Expiry < DateTime.UtcNow)
{
File.Delete(path);
return new NotFoundFileInfo(tmpFileId);
}
var storedFile = _storedFileRepository.GetFile(descriptor.FileId).GetAwaiter().GetResult();
return new PhysicalFileInfo(new FileInfo(Path.Combine(_fileRoot.FullName, storedFile.StorageFileName)));
}
public IDirectoryContents GetDirectoryContents(string subpath)
{
throw new System.NotImplementedException();
}
public IChangeToken Watch(string filter)
{
throw new System.NotImplementedException();
}
}
}

View File

@ -41,10 +41,6 @@ namespace BTCPayServer.Storage
Directory.CreateDirectory(datadirs.Value.TempDir);
}
var tmpdirInfo = Directory.Exists(datadirs.Value.TempStorageDir)
? new DirectoryInfo(datadirs.Value.TempStorageDir)
: Directory.CreateDirectory(datadirs.Value.TempStorageDir);
builder.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true,
@ -52,14 +48,6 @@ namespace BTCPayServer.Storage
FileProvider = new PhysicalFileProvider(dirInfo.FullName),
OnPrepareResponse = HandleStaticFileResponse()
});
builder.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true,
RequestPath = new PathString($"/{FileSystemFileProviderService.LocalStorageDirectoryName}tmp"),
FileProvider = new TemporaryLocalFileProvider(tmpdirInfo, dirInfo,
builder.ApplicationServices.GetService<StoredFileRepository>()),
OnPrepareResponse = HandleStaticFileResponse()
});
}
catch (Exception e)
{
@ -71,10 +59,7 @@ namespace BTCPayServer.Storage
{
return context =>
{
if (context.Context.Request.Query.ContainsKey("download"))
{
context.Context.Response.Headers["Content-Disposition"] = "attachment";
}
context.Context.Response.Headers["Content-Disposition"] = context.Context.Request.Query.ContainsKey("download")? "attachment" : "inline";
context.Context.Response.Headers["Content-Security-Policy"] = "script-src ;";
context.Context.Response.Headers["X-Content-Type-Options"] = "nosniff";
};