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 { private readonly IOptions _datadirs; public FileSystemFileProviderService(IOptions datadirs) { _datadirs = datadirs; } public const string LocalStorageDirectoryName = "LocalStorage"; public override StorageProvider StorageProvider() { return Storage.Models.StorageProvider.FileSystem; } protected override Task GetStorageProvider(FileSystemStorageConfiguration configuration) { return Task.FromResult( new LocalStorageProvider(new DirectoryInfo(_datadirs.Value.StorageDir).FullName)); } public override async Task GetFileUrl(Uri baseUri, StoredFile storedFile, StorageSettings configuration) { var baseResult = await base.GetFileUrl(baseUri, storedFile, configuration); var url = new Uri(baseUri, LocalStorageDirectoryName); return baseResult.Replace(new DirectoryInfo(_datadirs.Value.StorageDir).FullName, url.AbsoluteUri, StringComparison.InvariantCultureIgnoreCase); } public override async Task 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; } } }