BeWoFileExporter hinzugefügt
This commit is contained in:
BIN
BeWoFileExporter/.vs/BeWoFileExporter/v16/.suo
Normal file
BIN
BeWoFileExporter/.vs/BeWoFileExporter/v16/.suo
Normal file
Binary file not shown.
10
BeWoFileExporter/App.config
Normal file
10
BeWoFileExporter/App.config
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
|
||||
</startup>
|
||||
<appSettings>
|
||||
<add key="FileRootDirectory" value="\BWP-Files"/>
|
||||
<add key="MultitenancyDirectory" value="\MultitenancyForFileExport"/>
|
||||
</appSettings>
|
||||
</configuration>
|
||||
59
BeWoFileExporter/BeWoFileExporter.csproj
Normal file
59
BeWoFileExporter/BeWoFileExporter.csproj
Normal file
@@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{7352EC50-5812-44CE-B385-F52A3D907945}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<RootNamespace>BeWoFileExporter</RootNamespace>
|
||||
<AssemblyName>BeWoFileExporter</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<Deterministic>true</Deterministic>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="MySql.Data">
|
||||
<HintPath>C:\Users\lyndo\BeWo\Lib\MySql.Data.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Connection.cs" />
|
||||
<Compile Include="FileAttachment.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Utils.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
25
BeWoFileExporter/BeWoFileExporter.sln
Normal file
25
BeWoFileExporter/BeWoFileExporter.sln
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.31019.35
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeWoFileExporter", "BeWoFileExporter.csproj", "{7352EC50-5812-44CE-B385-F52A3D907945}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{7352EC50-5812-44CE-B385-F52A3D907945}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{7352EC50-5812-44CE-B385-F52A3D907945}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{7352EC50-5812-44CE-B385-F52A3D907945}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{7352EC50-5812-44CE-B385-F52A3D907945}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {EAD7FDD1-60AD-478D-8028-2832C296B010}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
26
BeWoFileExporter/Connection.cs
Normal file
26
BeWoFileExporter/Connection.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
namespace BeWoFileExporter
|
||||
{
|
||||
public class Connection
|
||||
{
|
||||
public string Server { get; set; }
|
||||
|
||||
public string Uid { get; set; }
|
||||
|
||||
public string Database { get; set; }
|
||||
|
||||
public string Password { get; set; }
|
||||
|
||||
public Connection(string server, string uid, string database, string password)
|
||||
{
|
||||
Server = server;
|
||||
Uid = uid;
|
||||
Database = database;
|
||||
Password = password;
|
||||
}
|
||||
|
||||
public string ToMySqlConnectionString()
|
||||
{
|
||||
return $"SERVER={Server};UID={Uid};PASSWORD={Password};DATABASE={Database};";
|
||||
}
|
||||
}
|
||||
}
|
||||
16
BeWoFileExporter/FileAttachment.cs
Normal file
16
BeWoFileExporter/FileAttachment.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace BeWoFileExporter
|
||||
{
|
||||
public class FileAttachment
|
||||
{
|
||||
public long Oid { get; set; }
|
||||
public string Tenant { get; set; }
|
||||
public byte[] Data { get; set; }
|
||||
|
||||
public FileAttachment(long oid, string tenant, byte[] data)
|
||||
{
|
||||
Oid = oid;
|
||||
Tenant = tenant;
|
||||
Data = data;
|
||||
}
|
||||
}
|
||||
}
|
||||
257
BeWoFileExporter/Program.cs
Normal file
257
BeWoFileExporter/Program.cs
Normal file
@@ -0,0 +1,257 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
|
||||
namespace BeWoFileExporter
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
private static MySqlConnection _MySqlConnection;
|
||||
|
||||
private static string BwpRootFileDirectory { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Sucht alle Datenbanken nach Blobs in der Tabelle FileAttachment ab, exportiert diese in das BWP-File-Verzeichnis und löscht den Blob aus der Datenbank.
|
||||
/// </summary>
|
||||
/// <param name="args">Enthält Kundennummern. Wenn keine Übergeben wird, werden alle Datenbanken auf dem Server durchgegangen.
|
||||
/// Hat die Position 0 den Wert 'm', wird nach der Kundennummer gefragt.</param>
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
var selectedTenants = new List<string>();
|
||||
|
||||
BwpRootFileDirectory = Utils.GetFileRootDirectoryPath();
|
||||
|
||||
var configPath = Utils.GetMultitenancyDirectoryPath();
|
||||
|
||||
//#if DEBUG
|
||||
// BwpRootFileDirectory = @"C:\BWP-Files";
|
||||
// configPath = @"C:\Users\lyndo\BeWo\Host\Multitenancy";
|
||||
//#endif
|
||||
|
||||
// Prüfen, ob die Kundennummer abgefragt werden soll:
|
||||
if(args.Length > 0 && args[0] == "m")
|
||||
{
|
||||
string tenants = null;
|
||||
|
||||
while(tenants == null)
|
||||
{
|
||||
Console.Write("Kundennummer (mehrere durch Komma trennen): ");
|
||||
tenants = Console.ReadLine();
|
||||
|
||||
if(tenants != null)
|
||||
{
|
||||
var tenantList = tenants.Split(',');
|
||||
selectedTenants = tenantList.ToList().Select(s => s.Trim()).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(args.Length > 0)
|
||||
{
|
||||
// Kann durch Kommata getrennt sein
|
||||
var tenants = args[0];
|
||||
if(tenants != null)
|
||||
{
|
||||
var tenantList = tenants.Split(',');
|
||||
selectedTenants = tenantList.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var configFileNames = selectedTenants.Select(tenant => $"{tenant}.config").ToList();
|
||||
|
||||
var configFiles = Directory.EnumerateFiles(configPath).Where(filePath =>
|
||||
{
|
||||
var fileName = Path.GetFileName(filePath);
|
||||
|
||||
var isSelected = !configFileNames.Any();
|
||||
if(configFileNames.Count > 0)
|
||||
{
|
||||
isSelected = configFileNames.Contains(fileName);
|
||||
}
|
||||
|
||||
return filePath.EndsWith(".config") && isSelected;
|
||||
}).ToList();
|
||||
|
||||
var connections = GetConnectionObjects(configFiles).ToList();
|
||||
|
||||
var datenbankplural = connections.Count != 1 ? "en" : string.Empty;
|
||||
|
||||
Console.WriteLine($"Exportiere Dateien aus {connections.Count} Datenbank{datenbankplural}\n");
|
||||
|
||||
foreach(var connection in connections)
|
||||
{
|
||||
_MySqlConnection = new MySqlConnection(connection.ToMySqlConnectionString());
|
||||
|
||||
ExtractFiles(connection);
|
||||
}
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
Console.WriteLine(exception);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(_MySqlConnection != null)
|
||||
{
|
||||
CloseConnection();
|
||||
}
|
||||
|
||||
Console.Read();
|
||||
}
|
||||
}
|
||||
|
||||
private static bool OpenConnection()
|
||||
{
|
||||
try
|
||||
{
|
||||
_MySqlConnection.Open();
|
||||
return true;
|
||||
}
|
||||
catch(MySqlException exception)
|
||||
{
|
||||
Console.WriteLine($"Fehler: {exception}\n");
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<Connection> GetConnectionObjects(IEnumerable<string> configFiles)
|
||||
{
|
||||
var connections = new List<Connection>();
|
||||
|
||||
foreach(var configFile in configFiles)
|
||||
{
|
||||
var connectionString = Utils.GetConnectionStringFromConfigFile(configFile)?.Trim();
|
||||
|
||||
if(connectionString != null &&
|
||||
connectionString.Contains("Server=") &&
|
||||
connectionString.Contains("Password=") &&
|
||||
connectionString.Contains("User ID=") &&
|
||||
connectionString.Contains("Initial Catalog="))
|
||||
{
|
||||
var server = Utils.GetConnectionStringValue("Server", connectionString);
|
||||
var password = Utils.GetConnectionStringValue("Password", connectionString);
|
||||
var uid = Utils.GetConnectionStringValue("User ID", connectionString);
|
||||
var database = Utils.GetConnectionStringValue("Initial Catalog", connectionString);
|
||||
|
||||
connections.Add(new Connection(server, uid, database, password));
|
||||
}
|
||||
}
|
||||
|
||||
return connections;
|
||||
}
|
||||
|
||||
private static void ExtractFiles(Connection connection)
|
||||
{
|
||||
_MySqlConnection = new MySqlConnection(connection.ToMySqlConnectionString());
|
||||
|
||||
try
|
||||
{
|
||||
if(OpenConnection())
|
||||
{
|
||||
LoadFileAttachmentData(connection.Database);
|
||||
}
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
Console.WriteLine(exception+ "\n");
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
private static void LoadFileAttachmentData(string connectionDatabase)
|
||||
{
|
||||
const string queryString = "SELECT Oid, Data FROM fileattachment WHERE Data IS NOT NULL;";
|
||||
|
||||
if(_MySqlConnection.State == ConnectionState.Open)
|
||||
{
|
||||
var mySqlCommand = new MySqlCommand(queryString, _MySqlConnection);
|
||||
|
||||
var reader = mySqlCommand.ExecuteReader();
|
||||
|
||||
var fileAttachments = new List<FileAttachment>();
|
||||
|
||||
while(reader.Read())
|
||||
{
|
||||
var data = reader["Data"];
|
||||
|
||||
var oid = reader["Oid"];
|
||||
|
||||
if(data == null || oid == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(data is byte[] byteArray && long.TryParse(oid.ToString(), out var fileAttachmentOid))
|
||||
{
|
||||
var tenant = connectionDatabase;
|
||||
|
||||
fileAttachments.Add(new FileAttachment(fileAttachmentOid, tenant, byteArray));
|
||||
}
|
||||
}
|
||||
|
||||
reader.Close();
|
||||
|
||||
var fileCount = fileAttachments.Count;
|
||||
|
||||
var word = fileCount > 1 ?
|
||||
$"{fileCount} Dateien werden exportiert" : fileCount == 0 ?
|
||||
"Keine Dateien exportiert" :
|
||||
"Eine Datei wird exportiert";
|
||||
|
||||
Console.WriteLine($"{connectionDatabase}: {word}\n");
|
||||
|
||||
SaveDataToDirectory(fileAttachments, connectionDatabase);
|
||||
}
|
||||
}
|
||||
|
||||
private static void SaveDataToDirectory(IReadOnlyCollection<FileAttachment> fileAttachments, string connectionDatabase)
|
||||
{
|
||||
if(_MySqlConnection.State == ConnectionState.Open && fileAttachments?.Count > 0)
|
||||
{
|
||||
Utils.CheckForFileRootDirectory(connectionDatabase, BwpRootFileDirectory);
|
||||
|
||||
var updateStringBuilder = new StringBuilder("UPDATE fileattachment SET Data = NULL WHERE Oid IN (");
|
||||
|
||||
var oidString = string.Empty;
|
||||
|
||||
foreach(var fileAttachment in fileAttachments)
|
||||
{
|
||||
File.WriteAllBytes(Utils.GetFilePath(connectionDatabase, fileAttachment.Oid.ToString()), fileAttachment.Data);
|
||||
oidString += fileAttachment.Oid + ",";
|
||||
}
|
||||
|
||||
updateStringBuilder.Append(oidString.TrimEnd(','));
|
||||
|
||||
updateStringBuilder.Append(");");
|
||||
|
||||
var updateCommand = new MySqlCommand(updateStringBuilder.ToString()) { Connection = _MySqlConnection };
|
||||
|
||||
updateCommand.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
private static void CloseConnection()
|
||||
{
|
||||
try
|
||||
{
|
||||
_MySqlConnection.Close();
|
||||
}
|
||||
catch(MySqlException exception)
|
||||
{
|
||||
Console.WriteLine(exception+ "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
36
BeWoFileExporter/Properties/AssemblyInfo.cs
Normal file
36
BeWoFileExporter/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("BeWoFileExporter")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("BeWoFileExporter")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2021")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("7352ec50-5812-44ce-b385-f52a3d907945")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
103
BeWoFileExporter/Utils.cs
Normal file
103
BeWoFileExporter/Utils.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System.Configuration;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
|
||||
namespace BeWoFileExporter
|
||||
{
|
||||
public static class Utils
|
||||
{
|
||||
public static string GetFileRootDirectoryPath()
|
||||
{
|
||||
var fileRootDirectoryPath = new AppSettingsReader().GetValue("FileRootDirectory", typeof(string)).ToString();
|
||||
|
||||
return fileRootDirectoryPath;
|
||||
}
|
||||
|
||||
public static string GetMultitenancyDirectoryPath()
|
||||
{
|
||||
var multitenancyDirectoryPath = new AppSettingsReader().GetValue("MultitenancyDirectory", typeof(string)).ToString();
|
||||
|
||||
if(!Directory.Exists(multitenancyDirectoryPath))
|
||||
{
|
||||
Directory.CreateDirectory(multitenancyDirectoryPath);
|
||||
}
|
||||
|
||||
return multitenancyDirectoryPath;
|
||||
}
|
||||
|
||||
public static void CheckForFileRootDirectory(string tenant, string fileRootDirectoryPath)
|
||||
{
|
||||
var tenantPath = Path.Combine(GetFileRootDirectoryPath(), tenant);
|
||||
|
||||
if(!Directory.Exists(fileRootDirectoryPath))
|
||||
{
|
||||
Directory.CreateDirectory(fileRootDirectoryPath);
|
||||
}
|
||||
|
||||
if(!Directory.Exists(tenantPath))
|
||||
{
|
||||
Directory.CreateDirectory(tenantPath);
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetFilePath(string tenant, string oid)
|
||||
{
|
||||
var rootDirectory = GetFileRootDirectoryPath();
|
||||
|
||||
return Path.Combine(rootDirectory, Path.Combine(tenant, oid));
|
||||
}
|
||||
|
||||
public static string GetConnectionStringValue(string variableName, string connectionString)
|
||||
{
|
||||
if(connectionString != null && connectionString.Contains(";"))
|
||||
{
|
||||
var split = connectionString.Split(';');
|
||||
|
||||
foreach(var keyValuePairString in split)
|
||||
{
|
||||
var keyToValue = keyValuePairString.Split('=');
|
||||
|
||||
if(keyToValue.Length == 2 && keyToValue[0].Trim() == variableName)
|
||||
{
|
||||
return keyToValue[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string GetConnectionStringFromConfigFile(string pathToConfigFile)
|
||||
{
|
||||
var xmlDocument = new XmlDocument();
|
||||
xmlDocument.Load(pathToConfigFile);
|
||||
|
||||
var configuration = xmlDocument.ChildNodes.Item(1);
|
||||
|
||||
if(configuration?.Name == "configuration")
|
||||
{
|
||||
var hibernateConfiguration = configuration.ChildNodes.Item(1);
|
||||
|
||||
if(hibernateConfiguration?.Name == "hibernate-configuration")
|
||||
{
|
||||
var sessionFactory = hibernateConfiguration.ChildNodes.Item(0);
|
||||
|
||||
if(sessionFactory?.Name == "session-factory")
|
||||
{
|
||||
var properties = sessionFactory.ChildNodes;
|
||||
|
||||
foreach(XmlNode property in properties)
|
||||
{
|
||||
if(property?.Attributes?["name"]?.Value == "connection.connection_string")
|
||||
{
|
||||
return property.InnerText;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user