112 lines
3.6 KiB
C#
112 lines
3.6 KiB
C#
using BeWo.Data.Entities;
|
|
using BS.Shared;
|
|
using BS.Shared.Core;
|
|
using BS.Shared.Exceptions;
|
|
using BS.Shared.Interface;
|
|
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
|
|
{
|
|
public class LocalTenantFileStorageDAO
|
|
{
|
|
private readonly string _rootPath;
|
|
|
|
public LocalTenantFileStorageDAO(string rootPath)
|
|
{
|
|
_rootPath = rootPath;
|
|
}
|
|
|
|
private string GetFilePath(string tenant, string filePath)
|
|
{
|
|
return Path.Combine(_rootPath, tenant, filePath);
|
|
}
|
|
|
|
public async Task<IStoredFile> SaveXRechnungPdfAsync(string tenant, IInvoiceBase invoicebase, Stream content)
|
|
{
|
|
var lTid = (int)TableID.InvoiceBase;
|
|
var lOid = invoicebase.Oid;
|
|
var lFileName = $"{lOid}.XRechnung.pdf";
|
|
|
|
return await SaveFileAsync(tenant, BWPFeature.XRechnung, lFileName, "application/pdf", content, lTid, lOid).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)
|
|
{
|
|
// Prüfe Namen
|
|
if (SecurityUtils.ContainsInvalidFilenameChars(pFilePath))
|
|
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 directory = Path.GetDirectoryName(path);
|
|
Directory.CreateDirectory(directory);
|
|
|
|
// Speichere Datei
|
|
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)
|
|
{
|
|
return Task.FromResult(File.Exists(GetFilePath(tenant, storedFile.FilePath)));
|
|
}
|
|
|
|
public Task<Stream> GetFileAsync(string tenant, IStoredFile storedFile, CancellationToken cancellationToken = default)
|
|
{
|
|
var path = GetFilePath(tenant, storedFile.FilePath);
|
|
if (!File.Exists(path))
|
|
return Task.FromResult<Stream>(null);
|
|
|
|
Stream stream = File.OpenRead(path);
|
|
return Task.FromResult(stream);
|
|
}
|
|
|
|
public Task<bool> DeleteFileAsync(string tenant, IStoredFile storedFile, CancellationToken cancellationToken = default)
|
|
{
|
|
var path = GetFilePath(tenant, storedFile.FilePath);
|
|
if (File.Exists(path))
|
|
{
|
|
File.Delete(path);
|
|
return Task.FromResult(true);
|
|
}
|
|
|
|
return Task.FromResult(false);
|
|
}
|
|
}
|
|
}
|