94 lines
3.6 KiB
C#
94 lines
3.6 KiB
C#
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using AuthorBuddy.Web.Models;
|
|
using AuthorBuddy.Web.Services;
|
|
|
|
namespace AuthorBuddy.Web.Plugins;
|
|
|
|
public class WordPressPlugin : IExportPlugin
|
|
{
|
|
private readonly ISettingsService _settings;
|
|
private readonly IHttpClientFactory _httpFactory;
|
|
|
|
public string Id => "wordpress";
|
|
public string Name => "WordPress";
|
|
public string Description => "Erstellt einen neuen WordPress-Beitrag als Draft mit Titel, Inhalt und Stichwörtern.";
|
|
|
|
public WordPressPlugin(ISettingsService settings, IHttpClientFactory httpFactory)
|
|
{
|
|
_settings = settings;
|
|
_httpFactory = httpFactory;
|
|
}
|
|
|
|
public async Task<PluginResult> ExportAsync(ExportPluginContext context)
|
|
{
|
|
var json = await _settings.GetPluginConfigAsync(Id);
|
|
PluginConfig? cfg = null;
|
|
if (!string.IsNullOrEmpty(json))
|
|
cfg = JsonSerializer.Deserialize<PluginConfig>(json);
|
|
|
|
if (cfg == null)
|
|
return new PluginResult { Success = false, Message = "WordPress-Plugin nicht konfiguriert." };
|
|
|
|
cfg.Values.TryGetValue("url", out var wpUrl);
|
|
cfg.Values.TryGetValue("username", out var username);
|
|
cfg.Values.TryGetValue("password", out var password);
|
|
|
|
if (string.IsNullOrEmpty(wpUrl) || string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
|
|
return new PluginResult { Success = false, Message = "WordPress-URL, Benutzername oder App-Passwort fehlt." };
|
|
|
|
wpUrl = wpUrl.TrimEnd('/');
|
|
|
|
var tags = new List<string>();
|
|
if (!string.IsNullOrWhiteSpace(context.Tags))
|
|
tags.AddRange(context.Tags.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries));
|
|
|
|
var post = new Dictionary<string, object>
|
|
{
|
|
["title"] = context.Title,
|
|
["content"] = context.Content,
|
|
["status"] = context.Status ?? "draft"
|
|
};
|
|
|
|
if (tags.Count > 0)
|
|
post["tags"] = tags;
|
|
|
|
var body = JsonSerializer.Serialize(post);
|
|
var request = new HttpRequestMessage(HttpMethod.Post, $"{wpUrl}/wp/v2/posts")
|
|
{
|
|
Content = new StringContent(body, Encoding.UTF8, "application/json")
|
|
};
|
|
|
|
var auth = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}"));
|
|
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", auth);
|
|
|
|
try
|
|
{
|
|
var http = _httpFactory.CreateClient();
|
|
var response = await http.SendAsync(request);
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var resBody = await response.Content.ReadAsStringAsync();
|
|
using var doc = JsonDocument.Parse(resBody);
|
|
var id = doc.RootElement.TryGetProperty("id", out var idProp) ? idProp.GetInt32() : 0;
|
|
var link = doc.RootElement.TryGetProperty("link", out var linkProp) ? linkProp.GetString() : "";
|
|
|
|
return new PluginResult
|
|
{
|
|
Success = true,
|
|
Message = $"WordPress-Beitrag als Draft erstellt (ID: {id}).{(string.IsNullOrEmpty(link) ? "" : $" {link}")}"
|
|
};
|
|
}
|
|
|
|
var err = await response.Content.ReadAsStringAsync();
|
|
return new PluginResult { Success = false, Message = $"WordPress-Fehler ({response.StatusCode}): {Truncate(err, 200)}" };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new PluginResult { Success = false, Message = $"Verbindungsfehler: {ex.Message}" };
|
|
}
|
|
}
|
|
|
|
private static string Truncate(string s, int max) => s.Length <= max ? s : s[..max] + "…";
|
|
}
|