Files
BeWoPlaner/Data/Access/LocalFileStorageDAO.cs

84 lines
2.2 KiB
C#
Raw Permalink Normal View History

2025-06-03 12:24:30 +02:00
using BeWo.Data.Entities;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.Exceptions;
2025-06-02 11:16:35 +02:00
using BS.Shared.Interface;
2025-06-05 14:03:55 +02:00
using DevExpress.Mvvm.Native;
2025-06-02 11:16:35 +02:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2025-06-03 12:24:30 +02:00
using System.Runtime.Remoting.Messaging;
2025-06-05 14:03:55 +02:00
using System.Security.Cryptography;
2025-06-02 11:16:35 +02:00
using System.Text;
using System.Threading;
using System.Threading.Tasks;
2025-06-05 14:03:55 +02:00
using System.Web.UI.WebControls;
2025-06-03 12:24:30 +02:00
using Task = System.Threading.Tasks.Task;
2025-06-02 11:16:35 +02:00
namespace BeWo.Data.Access
{
2025-06-03 12:24:30 +02:00
public class LocalFileStorageDAO
2025-06-02 11:16:35 +02:00
{
private readonly string _rootPath;
public LocalFileStorageDAO(string rootPath)
{
_rootPath = rootPath;
}
2025-06-05 14:03:55 +02:00
private string GetFilePath(string filePath)
2025-06-02 11:16:35 +02:00
{
2025-06-05 14:03:55 +02:00
return Path.Combine(_rootPath, filePath);
2025-06-02 11:16:35 +02:00
}
2025-06-05 14:03:55 +02:00
private async Task SaveFileAsync(string pFilePath, string pFileName, Stream content, CancellationToken cancellationToken = default)
2025-06-03 12:24:30 +02:00
{
// Prüfe Namen
2025-06-05 14:03:55 +02:00
if (SecurityUtils.ContainsInvalidFilenameChars(pFileName))
2025-06-10 14:29:34 +02:00
throw new BeWoInvalidOperationException(AppError.FileNameInvalidChar);
2025-06-03 12:24:30 +02:00
if (Path.GetFileName(pFilePath) != pFileName)
2025-06-10 14:29:34 +02:00
throw new BeWoInvalidOperationException(AppError.FileNameNotEqFilePath);
2025-06-03 12:24:30 +02:00
// Erstelle File Ordner
2025-06-05 14:03:55 +02:00
var path = GetFilePath(pFilePath);
2025-06-02 11:16:35 +02:00
var directory = Path.GetDirectoryName(path);
Directory.CreateDirectory(directory);
2025-06-03 12:24:30 +02:00
// Speichere Datei
2025-06-02 11:16:35 +02:00
using (var stream = File.Create(path))
{
2025-06-03 12:24:30 +02:00
await content.CopyToAsync(stream, bufferSize: 81920, cancellationToken).ConfigureAwait(true);
}
2025-06-02 11:16:35 +02:00
}
2025-06-05 14:03:55 +02:00
public Task<bool> FileExistsAsync(string pFilePath, CancellationToken cancellationToken = default)
2025-06-02 11:16:35 +02:00
{
2025-06-05 14:03:55 +02:00
return Task.FromResult(File.Exists(GetFilePath(pFilePath)));
2025-06-02 11:16:35 +02:00
}
2025-06-05 14:03:55 +02:00
public Task<Stream> GetFileAsync(string pFilePath, CancellationToken cancellationToken = default)
2025-06-02 11:16:35 +02:00
{
2025-06-05 14:03:55 +02:00
var path = GetFilePath(pFilePath);
2025-06-02 11:16:35 +02:00
if (!File.Exists(path))
return Task.FromResult<Stream>(null);
Stream stream = File.OpenRead(path);
return Task.FromResult(stream);
}
2025-06-05 14:03:55 +02:00
public Task<bool> DeleteFileAsync(string pFilePath, CancellationToken cancellationToken = default)
2025-06-02 11:16:35 +02:00
{
2025-06-05 14:03:55 +02:00
var path = GetFilePath(pFilePath);
2025-06-02 11:16:35 +02:00
if (File.Exists(path))
{
File.Delete(path);
return Task.FromResult(true);
}
return Task.FromResult(false);
}
}
}