using BS.Shared; using BS.Shared.Interface; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace BeWo.Data.Access { public class LocalFileStorageDAO : IFileStorageDAO { private readonly string _rootPath; public LocalFileStorageDAO(string rootPath) { _rootPath = rootPath; } private string GetFilePath(string customerId, BWPFeature feature, string fileName) { return Path.Combine(_rootPath, customerId, feature.ToString(), fileName); } public async Task SaveFileAsync(string customerId, BWPFeature feature, string fileName, Stream content, CancellationToken cancellationToken = default) { var path = GetFilePath(customerId, feature, fileName); var directory = Path.GetDirectoryName(path); Directory.CreateDirectory(directory); using (var stream = File.Create(path)) { await content.CopyToAsync(stream, bufferSize: 81920, cancellationToken).ConfigureAwait(true); }; } public Task FileExistsAsync(string customerId, BWPFeature feature, string fileName, CancellationToken cancellationToken = default) { return Task.FromResult(File.Exists(GetFilePath(customerId, feature, fileName))); } public Task GetFileAsync(string customerId, BWPFeature feature, string fileName, CancellationToken cancellationToken = default) { var path = GetFilePath(customerId, feature, fileName); if (!File.Exists(path)) return Task.FromResult(null); Stream stream = File.OpenRead(path); return Task.FromResult(stream); } public Task DeleteFileAsync(string customerId, BWPFeature feature, string fileName, CancellationToken cancellationToken = default) { var path = GetFilePath(customerId, feature, fileName); if (File.Exists(path)) { File.Delete(path); return Task.FromResult(true); } return Task.FromResult(false); } } }