mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-19 09:54:30 +01:00
This commit is contained in:
parent
ac64f5e395
commit
2538f3d8f6
@ -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));
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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";
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user