transactions

This commit is contained in:
2025-08-24 14:18:20 +02:00
parent 1b2d95344a
commit 5777e351bf
107 changed files with 4940 additions and 1266 deletions

View File

@@ -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);
}
}