Files
OnDoc/API_DocTest/Form2.cs
Stefan Hutter 49ecd6624a update 20260308
2026-03-08 14:08:06 +01:00

177 lines
6.3 KiB
C#

using Newtonsoft.Json.Linq;
using Syncfusion.Windows.Forms.PdfViewer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.Design;
using System.Windows.Forms;
namespace API_DocTest
{
public partial class Form2 : Form
{
static string connectionstring = Properties.Settings.Default.ConnectionString;
static readonly string ApiUrl = Properties.Settings.Default.DogGenURI;
static readonly string token = Properties.Settings.Default.Token;
private string path = Properties.Settings.Default.PathUser.Replace("[userid]", Environment.UserName);
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
string lickey = "MzYzODg2NkAzMjM4MmUzMDJlMzBTOWljRmxNelA1d1VGOHpGR0lxQzB6UTAwKzIxK2VBNEhBZFp5alcxb1NVPQ==";
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(lickey);
if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); }
label1.Text = "JSON-Pfad: " + path;
Refresh_Filelist();
}
private void Refresh_Filelist()
{
listBox1.Items.Clear();
DirectoryInfo d = new DirectoryInfo(path); //Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.json"); //Getting Text files
string str = "";
foreach (FileInfo file in Files)
{
listBox1.Items.Add(file.Name);
}
}
private void button3_Click(object sender, EventArgs e)
{
if (radioButton1.Checked) { connectionstring = Properties.Settings.Default.ConnectionString; }
if (radioButton2.Checked) { connectionstring = Properties.Settings.Default.ConnectionStringPrd; }
string key = txtfilekey.Text.Trim();
if (key == "") { MessageBox.Show("ESS_Key fehlt");return; }
string json = null;
using (SqlConnection con = new SqlConnection(connectionstring))
using (SqlCommand cmd = new SqlCommand(
"SELECT JavaScriptObject FROM ProvDokuments WHERE ProvDokumentid = @key", con))
{
cmd.Parameters.AddWithValue("@key", key);
con.Open();
json = cmd.ExecuteScalar()?.ToString();
}
if (string.IsNullOrEmpty(json)) { MessageBox.Show("Eintrag mit dem Schlüssel '" + txtfilekey.Text + "' ist nicht vorhanden.","Laden",MessageBoxButtons.OK,MessageBoxIcon.Error);return; }
if (string.IsNullOrEmpty(txtfilename.Text.Trim()) || txtfilename.Text=="") { txtfilename.Text=txtfilekey.Text; }
System.IO.File.WriteAllText(path + txtfilename.Text + ".json", json);
Refresh_Filelist();
foreach(string s in listBox1.Items)
{
if (s == txtfilename.Text+".json") { listBox1.SelectedItem = s;
txtfilekey.Text = "";
txtfilename.Text = "";
return; }
}
txtfilekey.Text = "";
txtfilename.Text = "";
}
private void button5_Click(object sender, EventArgs e)
{
try
{
string filename = path + listBox1.SelectedItem.ToString();
byte[] byteArray = Generate(System.IO.File.ReadAllText(filename));
string temp_inBase64 = Convert.ToBase64String(byteArray);
var stream = new MemoryStream(Convert.FromBase64String(temp_inBase64));
if (stream.Length > 0)
{
this.pdfViewerControl1.Load(stream);
this.pdfViewerControl1.Visible = true;
pdfViewerControl1.ZoomMode = ZoomMode.FitWidth;
}
else
{
}
}
catch (Exception ex) { MessageBox.Show("Fehler beim Generieren." +ex.Message, "Generieren", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
public static byte[] Generate(string json)
{
try
{
using (var client = new HttpClient())
{
// 🔐 Bearer Token
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", token);
var content = new StringContent(
json,
Encoding.UTF8,
"application/json");
var response = client.PostAsync(ApiUrl, content).Result;
var responseText = response.Content.ReadAsStringAsync().Result;
if (!response.IsSuccessStatusCode)
{
// optional: Logging
return null;
}
var jsonResult = JObject.Parse(responseText);
var base64Pdf = jsonResult["file"]?.ToString();
if (string.IsNullOrEmpty(base64Pdf))
return null;
return Convert.FromBase64String(base64Pdf);
}
}
catch (Exception ex)
{
// Logging
return null;
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (File.Exists(path + listBox1.SelectedItem.ToString()))
{
System.IO.File.Delete(path + listBox1.SelectedItem.ToString());
Refresh_Filelist();
}
}
catch(Exception ex)
{
MessageBox.Show("Beim Löschen ist ein Fehler aufgetreten:" + ex.Message, "Löschen", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}