67 lines
1.9 KiB
C#
67 lines
1.9 KiB
C#
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<bool> FileExistsAsync(string customerId, BWPFeature feature, string fileName, CancellationToken cancellationToken = default)
|
|
{
|
|
return Task.FromResult(File.Exists(GetFilePath(customerId, feature, fileName)));
|
|
}
|
|
|
|
public Task<Stream> GetFileAsync(string customerId, BWPFeature feature, string fileName, CancellationToken cancellationToken = default)
|
|
{
|
|
var path = GetFilePath(customerId, feature, fileName);
|
|
if (!File.Exists(path))
|
|
return Task.FromResult<Stream>(null);
|
|
|
|
Stream stream = File.OpenRead(path);
|
|
return Task.FromResult(stream);
|
|
}
|
|
|
|
public Task<bool> 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);
|
|
}
|
|
}
|
|
}
|