LocalFileStorage

This commit is contained in:
2025-06-05 14:03:55 +02:00
parent 5e7080fd9d
commit ed4dc8fa75
24 changed files with 298 additions and 189 deletions

View File

@@ -3,14 +3,17 @@ using BS.Shared;
using BS.Shared.Core;
using BS.Shared.Exceptions;
using BS.Shared.Interface;
using DevExpress.Mvvm.Native;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using Task = System.Threading.Tasks.Task;
namespace BeWo.Data.Access
@@ -24,47 +27,29 @@ namespace BeWo.Data.Access
_rootPath = rootPath;
}
private string GetFilePath(string tenant, string filePath)
private string GetFilePath(string filePath)
{
return Path.Combine(_rootPath, tenant, filePath);
return Path.Combine(_rootPath, filePath);
}
public async Task<IStoredFile> SaveXRechnungPdfAsync(string tenant, IInvoiceBase invoicebase, Stream content)
public async Task SaveLogAsync(string pFileName, Stream content, CancellationToken cancellationToken = default)
{
var lTid = (int)TableID.InvoiceBase;
var lOid = invoicebase.Oid;
var lFileName = $"{lOid}.XRechnung.pdf";
var lFilePath = Path.Combine("logs", pFileName);
return await SaveFileAsync(tenant, BWPFeature.XRechnung, lFileName, "application/pdf", content, lTid, lOid).ConfigureAwait(true);
await SaveFileAsync(lFilePath, pFileName, content, cancellationToken).ConfigureAwait(true);
}
private async Task<IStoredFile> SaveFileAsync(string tenant, BWPFeature feature, string pFileName, string pContentType, Stream content, int? pBeWoObjectTid = null, long? pBeWoObjectOid = null, CancellationToken cancellationToken = default)
{
var lFilePath = feature.ToString();
return await SaveFileAsync(tenant, lFilePath, pFileName, pContentType, content, pBeWoObjectTid, pBeWoObjectOid, cancellationToken).ConfigureAwait(true);
}
private async Task<IStoredFile> SaveFileAsync(string tenant, string pFilePath, string pFileName, string pContentType, Stream content, int? pBeWoObjectTid = null, long? pBeWoObjectOid = null, CancellationToken cancellationToken = default)
private async Task SaveFileAsync(string pFilePath, string pFileName, Stream content, CancellationToken cancellationToken = default)
{
// Prüfe Namen
if (SecurityUtils.ContainsInvalidFilenameChars(pFilePath))
if (SecurityUtils.ContainsInvalidFilenameChars(pFileName))
throw new BeWoInvalidOperationException(AppError.LocalFileStorageFileNameInvalidChar);
if (Path.GetFileName(pFilePath) != pFileName)
throw new BeWoInvalidOperationException(AppError.LocalFileStorageFileNameNotEqFilePath);
// Erstelle DB Eintrag
var storedfile = new StoredFile();
storedfile.FileName = pFileName;
storedfile.FilePath = pFilePath;
storedfile.ContentType = pContentType;
storedfile.BeWoObjectTid = pBeWoObjectTid;
storedfile.BeWoObjectOid = pBeWoObjectOid;
// Erstelle File Ordner
var path = GetFilePath(tenant, pFilePath);
var path = GetFilePath(pFilePath);
var directory = Path.GetDirectoryName(path);
Directory.CreateDirectory(directory);
@@ -72,21 +57,17 @@ namespace BeWo.Data.Access
using (var stream = File.Create(path))
{
await content.CopyToAsync(stream, bufferSize: 81920, cancellationToken).ConfigureAwait(true);
storedfile.Size = content.Position;
storedfile.Checksum = SecurityUtils.GetChecksumBuffered(content);
}
return storedfile;
}
public Task<bool> FileExistsAsync(string tenant, IStoredFile storedFile, CancellationToken cancellationToken = default)
public Task<bool> FileExistsAsync(string pFilePath, CancellationToken cancellationToken = default)
{
return Task.FromResult(File.Exists(GetFilePath(tenant, storedFile.FilePath)));
return Task.FromResult(File.Exists(GetFilePath(pFilePath)));
}
public Task<Stream> GetFileAsync(string tenant, IStoredFile storedFile, CancellationToken cancellationToken = default)
public Task<Stream> GetFileAsync(string pFilePath, CancellationToken cancellationToken = default)
{
var path = GetFilePath(tenant, storedFile.FilePath);
var path = GetFilePath(pFilePath);
if (!File.Exists(path))
return Task.FromResult<Stream>(null);
@@ -94,9 +75,9 @@ namespace BeWo.Data.Access
return Task.FromResult(stream);
}
public Task<bool> DeleteFileAsync(string tenant, IStoredFile storedFile, CancellationToken cancellationToken = default)
public Task<bool> DeleteFileAsync(string pFilePath, CancellationToken cancellationToken = default)
{
var path = GetFilePath(tenant, storedFile.FilePath);
var path = GetFilePath(pFilePath);
if (File.Exists(path))
{
File.Delete(path);