Update 20260123
This commit is contained in:
208
API_NetFramework/Controllers/JsonController.cs
Normal file
208
API_NetFramework/Controllers/JsonController.cs
Normal file
@@ -0,0 +1,208 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Http;
|
||||
using System.Web.Mvc;
|
||||
|
||||
using System.Configuration;
|
||||
using System.Data.SqlClient;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using SecuringWebApiUsingApiKey.Middleware;
|
||||
using System.Threading.Tasks;
|
||||
using Model;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Web.Http.Results;
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
|
||||
namespace OnDocAPI_NetFramework.Controllers
|
||||
{
|
||||
public class JsonRequestDto
|
||||
{
|
||||
public string Key { get; set; }
|
||||
public string Json { get; set; }
|
||||
}
|
||||
|
||||
[System.Web.Http.RoutePrefix("api/json")]
|
||||
public class JsonController : ApiController
|
||||
{
|
||||
|
||||
private readonly string _connectionString = StringCipher.Decrypt(ConfigurationManager.ConnectionStrings["DocTesterconnectionstring"].ConnectionString, "i%!k!7pab%bNLdA5hE4pkR4XaB%E^jB3d9tHuQ4pbF&BZjF7SB#WBWit5#HrbJiLrLVm");
|
||||
|
||||
// 🔹 JSON LESEN
|
||||
[System.Web.Http.HttpGet]
|
||||
[System.Web.Http.Route("load/{key}")]
|
||||
public IHttpActionResult LoadJson(string key)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
return BadRequest("Key fehlt");
|
||||
|
||||
string json;
|
||||
|
||||
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 (json == null)
|
||||
return NotFound();
|
||||
|
||||
return Ok(json);
|
||||
}
|
||||
|
||||
// 🔹 JSON SPEICHERN
|
||||
[System.Web.Http.HttpPost]
|
||||
[System.Web.Http.Route("save")]
|
||||
public IHttpActionResult SaveJson(JsonRequestDto dto)
|
||||
{
|
||||
if (dto == null || string.IsNullOrWhiteSpace(dto.Key))
|
||||
return BadRequest("Key fehlt");
|
||||
|
||||
using (SqlConnection con = new SqlConnection(_connectionString))
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
IF EXISTS (SELECT 1 FROM provdokuments WHERE provdokumentid = @key)
|
||||
UPDATE provdokuments SET JavaScriptObject = @json, geaendertam = GETDATE()
|
||||
WHERE provdokumentid = @key
|
||||
ELSE
|
||||
INSERT INTO provdokuments (provdokumentid,erstelltam,geaendertam, JavaScriptObject)
|
||||
VALUES (@key, getdate(),getdate(),@json)
|
||||
", con))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@key", dto.Key);
|
||||
cmd.Parameters.AddWithValue("@json", dto.Json);
|
||||
con.Open();
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
public class JsonRequestDto
|
||||
{
|
||||
public string Key { get; set; }
|
||||
public string Json { get; set; } // ← reiner JSON-String
|
||||
}
|
||||
public class PdfBase64Response
|
||||
{
|
||||
public string PdfBase64 { get; set; }
|
||||
}
|
||||
// 🔹 PDF GENERIEREN
|
||||
[System.Web.Http.HttpPost]
|
||||
[System.Web.Http.Route("GeneratePDF")]
|
||||
public async Task<HttpResponseMessage> GeneratePdf(JsonRequestDto dto)
|
||||
{
|
||||
if (dto == null || string.IsNullOrWhiteSpace(dto.Json))
|
||||
return new HttpResponseMessage(HttpStatusCode.BadRequest);
|
||||
|
||||
byte[] pdfBytes = await CallExternalPdfApi(dto.Json);
|
||||
|
||||
var response = new HttpResponseMessage(HttpStatusCode.OK)
|
||||
{
|
||||
Content = new ByteArrayContent(pdfBytes)
|
||||
};
|
||||
|
||||
|
||||
response.Content.Headers.ContentType =
|
||||
new MediaTypeHeaderValue("application/pdf");
|
||||
|
||||
response.Content.Headers.ContentDisposition =
|
||||
new ContentDispositionHeaderValue("inline")
|
||||
{
|
||||
FileName = $"{dto.Key}.pdf"
|
||||
};
|
||||
response.Content.Headers.ContentLength = pdfBytes.Length;
|
||||
if (System.IO.File.Exists(@"x:\file.pdf")) { System.IO.File.Delete(@"x:\file.pdf"); }
|
||||
System.IO.FileStream stream =
|
||||
|
||||
new FileStream(@"x:\file.pdf", FileMode.CreateNew);
|
||||
System.IO.BinaryWriter writer =
|
||||
new BinaryWriter(stream);
|
||||
writer.Write(pdfBytes, 0, pdfBytes.Length);
|
||||
writer.Close();
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private async Task<byte[]> CallExternalPdfApi(string json)
|
||||
{
|
||||
try
|
||||
{
|
||||
APIErrorSimple apireturn = new APIErrorSimple();
|
||||
APIOK apiok = new APIOK();
|
||||
;
|
||||
string jsonstring = json;
|
||||
|
||||
WebRequest request;
|
||||
|
||||
var data = Encoding.UTF8.GetBytes(jsonstring);
|
||||
string OwnHost = System.Configuration.ConfigurationManager.AppSettings["OwnHost"].ToString();
|
||||
string uri = OwnHost + "/API/CreateCLM";
|
||||
request = WebRequest.Create(uri);
|
||||
request.ContentLength = data.Length;
|
||||
request.ContentType = "application/json";
|
||||
request.Method = "POST";
|
||||
request.Headers["Authorization"] = "Bearer " + "pZkuG6l6ORCEckqQimPK58PO1A9EnkMtL5oCgRX9WiWnD4xeH7ikGzhWnTBy/vk8J4Iiz8gCSx9uFHA4+DvITG0roO97sk82d/0BCjVlwLWINpXlJfGYEF3X96AdoCQvb3ruwv/tVqEHsSU5aNfyxBAe+EhLTHQ8t7ysgJZWh98=";
|
||||
using (Stream requestStream = request.GetRequestStream())
|
||||
{
|
||||
requestStream.Write(data, 0, data.Length);
|
||||
requestStream.Close();
|
||||
|
||||
using (Stream responseStream = request.GetResponse().GetResponseStream())
|
||||
{
|
||||
using (var reader = new StreamReader(responseStream))
|
||||
{
|
||||
var response = reader.ReadToEnd();
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
apiok = JsonConvert.DeserializeObject<APIOK>(response);
|
||||
var jo = JObject.Parse(response.ToString());
|
||||
|
||||
return Convert.FromBase64String(apiok.file);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
apireturn = JsonConvert.DeserializeObject<APIErrorSimple>(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
catch {
|
||||
byte[] pdf;
|
||||
return null;}
|
||||
|
||||
|
||||
// var request = (HttpWebRequest)WebRequest.Create("https://api.example.com/generate-pdf");
|
||||
//request.Method = "POST";
|
||||
//request.ContentType = "application/json";
|
||||
|
||||
//byte[] payload = Encoding.UTF8.GetBytes(json);
|
||||
//request.ContentLength = payload.Length;
|
||||
|
||||
//using (var stream = request.GetRequestStream())
|
||||
// stream.Write(payload, 0, payload.Length);
|
||||
|
||||
//using (var response = (HttpWebResponse)request.GetResponse())
|
||||
//using (var rs = response.GetResponseStream())
|
||||
//using (var ms = new MemoryStream())
|
||||
//{
|
||||
// rs.CopyTo(ms);
|
||||
// return ms.ToArray();
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user