This commit is contained in:
2026-04-17 07:08:04 +02:00
parent ca91af9682
commit 0d3bd47f7a
34 changed files with 17503 additions and 160 deletions
@@ -1,3 +1,4 @@
using Azure.Core;
using Azure.Identity;
using Microsoft.Graph;
@@ -8,10 +9,17 @@ public class SharePointUploadService : ISharePointUploadService
public async Task UploadAsync(string tenantId, string clientId, string clientSecret,
string siteUrl, string exportFolder, string land, string localFilePath)
{
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var normalizedTenantId = Normalize(tenantId);
var normalizedClientId = Normalize(clientId);
var normalizedClientSecret = Normalize(clientSecret);
var normalizedSiteUrl = Normalize(siteUrl);
var normalizedExportFolder = Normalize(exportFolder);
var normalizedLand = Normalize(land);
var credential = new ClientSecretCredential(normalizedTenantId, normalizedClientId, normalizedClientSecret);
var graphClient = new GraphServiceClient(credential, ["https://graph.microsoft.com/.default"]);
var uri = new Uri(siteUrl);
var uri = new Uri(normalizedSiteUrl);
var sitePath = uri.AbsolutePath;
var site = await graphClient.Sites[$"{uri.Host}:{sitePath}"].GetAsync();
@@ -23,8 +31,13 @@ public class SharePointUploadService : ISharePointUploadService
throw new InvalidOperationException("SharePoint Dokumentenbibliothek konnte nicht gefunden werden.");
var fileName = Path.GetFileName(localFilePath);
var folderPath = exportFolder.Trim('/').Trim();
var remotePath = $"{folderPath}/{land}/{fileName}";
var remotePath = string.Join("/",
new[]
{
normalizedExportFolder.Trim('/').Trim(),
normalizedLand.Trim('/').Trim(),
fileName
}.Where(segment => !string.IsNullOrWhiteSpace(segment)));
await using var stream = File.OpenRead(localFilePath);
await graphClient.Drives[drive.Id].Root.ItemWithPath(remotePath).Content.PutAsync(stream);
@@ -32,14 +45,53 @@ public class SharePointUploadService : ISharePointUploadService
public async Task TestConnectionAsync(string tenantId, string clientId, string clientSecret, string siteUrl)
{
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(credential, ["https://graph.microsoft.com/.default"]);
var normalizedTenantId = Normalize(tenantId);
var normalizedClientId = Normalize(clientId);
var normalizedClientSecret = Normalize(clientSecret);
var normalizedSiteUrl = Normalize(siteUrl);
var inputPreview = BuildInputPreview(normalizedTenantId, normalizedClientId, normalizedClientSecret, normalizedSiteUrl);
var uri = new Uri(siteUrl);
if (string.IsNullOrWhiteSpace(normalizedTenantId))
throw new InvalidOperationException($"Tenant ID fehlt. {inputPreview}");
if (string.IsNullOrWhiteSpace(normalizedClientId))
throw new InvalidOperationException($"Client ID fehlt. {inputPreview}");
if (string.IsNullOrWhiteSpace(normalizedClientSecret))
throw new InvalidOperationException($"Client Secret fehlt. {inputPreview}");
if (string.IsNullOrWhiteSpace(normalizedSiteUrl))
throw new InvalidOperationException($"Site URL fehlt. {inputPreview}");
var credential = new ClientSecretCredential(normalizedTenantId, normalizedClientId, normalizedClientSecret);
try
{
await credential.GetTokenAsync(
new TokenRequestContext(["https://graph.microsoft.com/.default"]),
CancellationToken.None);
}
catch (AuthenticationFailedException ex)
{
throw new InvalidOperationException(
$"ClientSecretCredential authentication failed: {ex.Message}{Environment.NewLine}{inputPreview}",
ex);
}
var graphClient = new GraphServiceClient(credential, ["https://graph.microsoft.com/.default"]);
var uri = new Uri(normalizedSiteUrl);
var sitePath = uri.AbsolutePath;
var site = await graphClient.Sites[$"{uri.Host}:{sitePath}"].GetAsync();
if (site?.Id is null)
throw new InvalidOperationException("SharePoint Site konnte nicht gefunden werden.");
throw new InvalidOperationException($"SharePoint Site konnte nicht gefunden werden. {inputPreview}");
}
private static string Normalize(string value) => value?.Trim() ?? string.Empty;
private static string BuildInputPreview(string tenantId, string clientId, string clientSecret, string siteUrl)
{
var maskedSecret = string.IsNullOrEmpty(clientSecret)
? "<leer>"
: $"{new string('*', Math.Min(clientSecret.Length, 8))} (len={clientSecret.Length})";
return $"Uebergeben: TenantId='{tenantId}', ClientId='{clientId}', ClientSecret={maskedSecret}, SiteUrl='{siteUrl}'";
}
}