btcpayserver/BTCPayServer/Storage/Services/Providers/FileSystemStorage/FileSystemFileProviderService.cs
Umar Bolatov c9cfe5cc6e
Fix direct URL for local storage with custom root path (#2318)
* Fix direct URL for local storage with custom root path

* Remove "Context.Request.PathBase" when generating file URL display string
2021-03-01 22:43:57 +09:00

70 lines
2.9 KiB
C#

using System;
using System.IO;
using System.Threading.Tasks;
using BTCPayServer.Configuration;
using BTCPayServer.Data;
using BTCPayServer.Storage.Models;
using BTCPayServer.Storage.Services.Providers.FileSystemStorage.Configuration;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using TwentyTwenty.Storage;
using TwentyTwenty.Storage.Local;
namespace BTCPayServer.Storage.Services.Providers.FileSystemStorage
{
public class
FileSystemFileProviderService : BaseTwentyTwentyStorageFileProviderServiceBase<FileSystemStorageConfiguration>
{
private readonly IOptions<DataDirectories> _datadirs;
public FileSystemFileProviderService(IOptions<DataDirectories> datadirs)
{
_datadirs = datadirs;
}
public const string LocalStorageDirectoryName = "LocalStorage";
public override StorageProvider StorageProvider()
{
return Storage.Models.StorageProvider.FileSystem;
}
protected override Task<IStorageProvider> GetStorageProvider(FileSystemStorageConfiguration configuration)
{
return Task.FromResult<IStorageProvider>(
new LocalStorageProvider(new DirectoryInfo(_datadirs.Value.StorageDir).FullName));
}
public override async Task<string> GetFileUrl(Uri baseUri, StoredFile storedFile, StorageSettings configuration)
{
var baseResult = await base.GetFileUrl(baseUri, storedFile, configuration);
// Set the relative URL to the directory name if the root path is default, otherwise add root path before the directory name
var relativeUrl = baseUri.AbsolutePath == "/" ? LocalStorageDirectoryName : $"{baseUri.AbsolutePath}/{LocalStorageDirectoryName}";
var url = new Uri(baseUri, relativeUrl);
return baseResult.Replace(new DirectoryInfo(_datadirs.Value.StorageDir).FullName, url.AbsoluteUri,
StringComparison.InvariantCultureIgnoreCase);
}
public override async Task<string> GetTemporaryFileUrl(Uri baseUri, StoredFile storedFile,
StorageSettings configuration, DateTimeOffset expiry, bool isDownload,
BlobUrlAccess access = BlobUrlAccess.Read)
{
var localFileDescriptor = new TemporaryLocalFileDescriptor()
{
Expiry = expiry,
FileId = storedFile.Id,
IsDownload = isDownload
};
var name = Guid.NewGuid().ToString();
var fullPath = Path.Combine(_datadirs.Value.TempStorageDir, name);
if (!File.Exists(fullPath))
{
File.Create(fullPath).Dispose();
}
await File.WriteAllTextAsync(Path.Combine(_datadirs.Value.TempStorageDir, name), JsonConvert.SerializeObject(localFileDescriptor));
return new Uri(baseUri, $"{LocalStorageDirectoryName}tmp/{name}{(isDownload ? "?download" : string.Empty)}").AbsoluteUri;
}
}
}