transactions
This commit is contained in:
@@ -5,41 +5,41 @@ using Microsoft.Extensions.Options;
|
||||
namespace back.persistance.blob;
|
||||
|
||||
public class FileSystemImageStorageService(
|
||||
IOptions<Databases> systemOptions,
|
||||
IOptions<Databases> systemOptions,
|
||||
IOptionsMonitor<DatabaseConfig> options,
|
||||
IMemoryCache memoryCache
|
||||
) : IBlobStorageService
|
||||
) : IBlobStorageService
|
||||
{
|
||||
private readonly string RootPath = systemOptions.Value.BaseDirectory ?? "";
|
||||
private readonly string RootPath = systemOptions.Value.BaseDirectory ?? "data";
|
||||
private readonly DatabaseConfig config = options.Get(DatabaseConfig.BlobStorage);
|
||||
private readonly IMemoryCache cache = memoryCache;
|
||||
|
||||
private string GetFullPath(string fileName)
|
||||
{
|
||||
// Ensure the directory exists
|
||||
var directory = Path.Join(RootPath, config.SystemContainer, fileName);
|
||||
var path = Path.Join(RootPath, config.SystemContainer, fileName);
|
||||
var directory = Path.GetDirectoryName(path);
|
||||
if (directory != null && !Directory.Exists(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
return fileName;
|
||||
return path;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(string fileName)
|
||||
public async Task Delete(string fileName)
|
||||
{
|
||||
var path = GetFullPath(fileName);
|
||||
if (cache.TryGetValue(path, out Stream cachedStream))
|
||||
{
|
||||
cachedStream.Dispose(); // Dispose the cached stream if it exists
|
||||
cache.Remove(path); // Remove from cache
|
||||
}
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
return; // No file to delete
|
||||
}
|
||||
try
|
||||
{
|
||||
File.Delete(path);
|
||||
var path = GetFullPath(fileName);
|
||||
if (cache.TryGetValue(path, out Stream? cachedStream))
|
||||
{
|
||||
cachedStream?.Dispose(); // Dispose the cached stream if it exists
|
||||
cache.Remove(path); // Remove from cache
|
||||
}
|
||||
if (File.Exists(path))
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -47,14 +47,14 @@ public class FileSystemImageStorageService(
|
||||
}
|
||||
}
|
||||
|
||||
public Task<Stream?> GetStreamAsync(string fileName)
|
||||
public async Task<Stream?> GetStream(string fileName)
|
||||
{
|
||||
var path = GetFullPath(fileName);
|
||||
if (File.Exists(path))
|
||||
{
|
||||
if (cache.TryGetValue(path, out Stream? cachedStream))
|
||||
{
|
||||
return Task.FromResult<Stream?>(cachedStream);
|
||||
return cachedStream;
|
||||
}
|
||||
// open the file stream for multiple reads and cache it for performance
|
||||
var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
@@ -63,14 +63,14 @@ public class FileSystemImageStorageService(
|
||||
.SetValue(fileStream)
|
||||
.SetSlidingExpiration(TimeSpan.FromMinutes(30)); // Cache for 30 minutes
|
||||
|
||||
return Task.FromResult<Stream?>(fileStream);
|
||||
return fileStream;
|
||||
}
|
||||
return Task.FromResult<Stream?>(null);
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<byte[]?> GetBytesAsync(string fileName)
|
||||
public async Task<byte[]?> GetBytes(string fileName)
|
||||
{
|
||||
var stream = await GetStreamAsync(fileName);
|
||||
var stream = await GetStream(fileName);
|
||||
if (stream != null)
|
||||
{
|
||||
using var memoryStream = new MemoryStream();
|
||||
@@ -80,25 +80,25 @@ public class FileSystemImageStorageService(
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task SaveAsync(Stream blobStream, string fileName)
|
||||
public async Task Save(Stream blobStream, string fileName)
|
||||
{
|
||||
var path = GetFullPath(fileName);
|
||||
if (cache.TryGetValue(path, out Stream? _) || File.Exists(path))
|
||||
{
|
||||
throw new InvalidOperationException($"File {fileName} already exists. Use Update for updating file info.");
|
||||
}
|
||||
using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write);
|
||||
using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read);
|
||||
await blobStream.CopyToAsync(fileStream);
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(Stream blobStream, string fileName)
|
||||
public async Task Update(Stream blobStream, string fileName)
|
||||
{
|
||||
var path = GetFullPath(fileName);
|
||||
if (File.Exists(path))
|
||||
{
|
||||
await DeleteAsync(fileName);
|
||||
await Delete(fileName);
|
||||
}
|
||||
await SaveAsync(blobStream, fileName);
|
||||
await Save(blobStream, fileName);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,11 +1,13 @@
|
||||
namespace back.persistance.blob;
|
||||
using DependencyInjector.Lifetimes;
|
||||
|
||||
public interface IBlobStorageService
|
||||
namespace back.persistance.blob;
|
||||
|
||||
public interface IBlobStorageService : ISingleton
|
||||
{
|
||||
Task SaveAsync(Stream blobStream, string fileName);
|
||||
Task<Stream?> GetStreamAsync(string fileName);
|
||||
Task<byte[]?> GetBytesAsync(string fileName);
|
||||
Task DeleteAsync(string fileName);
|
||||
Task UpdateAsync(Stream blobStream, string fileName);
|
||||
Task Save(Stream blobStream, string fileName);
|
||||
Task<Stream?> GetStream(string fileName);
|
||||
Task<byte[]?> GetBytes(string fileName);
|
||||
Task Delete(string fileName);
|
||||
Task Update(Stream blobStream, string fileName);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user