115 lines
3.7 KiB
C#
115 lines
3.7 KiB
C#
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.IO;
|
|
using BeWo.Data;
|
|
using BeWo.Data.Access;
|
|
using BeWo.Data.Entities;
|
|
using BS.Shared.Extensions;
|
|
using RestSharp.Extensions;
|
|
|
|
namespace BeWo.Service.Core
|
|
{
|
|
public static class FileAttachmentUtils
|
|
{
|
|
public static string GetFileRootDirectoryPath()
|
|
{
|
|
var fileRootDirectoryPath = ConfigurationManager.AppSettings.Get(BS.Shared.Core.SettingsKeys.FileRootDirectory);
|
|
|
|
return fileRootDirectoryPath;
|
|
}
|
|
|
|
public static string GetTenantFilePath()
|
|
{
|
|
var fileRootDirectoryPath = GetFileRootDirectoryPath();
|
|
var tenant = MultitenancyOperationContextExt.Current.Tenant;
|
|
|
|
return Path.Combine(fileRootDirectoryPath, tenant);
|
|
}
|
|
|
|
public static string GetFilePath(long fileAttachmentOid)
|
|
{
|
|
return Path.Combine(GetTenantFilePath(), fileAttachmentOid.ToString());
|
|
}
|
|
|
|
public static void CheckForFileRootDirectory()
|
|
{
|
|
var fileRootDirectoryPath = GetFileRootDirectoryPath();
|
|
|
|
var tenantPath = GetTenantFilePath();
|
|
|
|
if(!Directory.Exists(fileRootDirectoryPath))
|
|
{
|
|
Directory.CreateDirectory(fileRootDirectoryPath);
|
|
}
|
|
|
|
if(!Directory.Exists(tenantPath))
|
|
{
|
|
Directory.CreateDirectory(tenantPath);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Speichert die Binärdaten in einer Datei im FileRootDirectory. Die Oid wird als Dateiname benutzt. Es wird keine Endung gespeichert.
|
|
/// </summary>
|
|
/// <param name="fileAttachment">Das FileAttachment-Objekt mit dem byte-Array</param>
|
|
/// <param name="binaryData">Das byte-Array, das anstelle des Arrays vom FileAttachment-Objekt verwendet wird</param>
|
|
public static void SaveFileToFileDirectory(FileAttachment fileAttachment, byte[] binaryData = null)
|
|
{
|
|
if((fileAttachment.Data?.Length > 0 || binaryData?.Length > 0) && fileAttachment?.Oid != null)
|
|
{
|
|
CheckForFileRootDirectory();
|
|
|
|
var filePath = GetFilePath(fileAttachment.Oid.Value);
|
|
|
|
File.WriteAllBytes(filePath, binaryData ?? fileAttachment.Data);
|
|
|
|
if(File.Exists(filePath) && fileAttachment.Data != null)
|
|
{
|
|
fileAttachment.Data = null;
|
|
DAOFactory.GenericDAO.Update(fileAttachment);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static byte[] LoadFileFromDirectory(long? fileAttachmentOid)
|
|
{
|
|
if(fileAttachmentOid.HasValue)
|
|
{
|
|
var filePath = GetFilePath(fileAttachmentOid.Value);
|
|
|
|
if(File.Exists(filePath))
|
|
{
|
|
byte[] result;
|
|
|
|
using(var fileStream = new FileStream(filePath, FileMode.Open))
|
|
{
|
|
result = fileStream.ReadAsBytes();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static void DeleteFileFromFileDirectory(long? fileAttachmentOid)
|
|
{
|
|
if(fileAttachmentOid.HasValue)
|
|
{
|
|
var filePath = GetFilePath(fileAttachmentOid.Value);
|
|
|
|
if(File.Exists(filePath))
|
|
{
|
|
File.Delete(filePath);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void DeleteFilesFromFileDirectory(List<long> fileAttachmentOids)
|
|
{
|
|
fileAttachmentOids?.DoForEach(fileAttachmentOid => { DeleteFileFromFileDirectory(fileAttachmentOid); });
|
|
}
|
|
}
|
|
}
|