L1SvrController.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using LJLib.Client;
  2. using LJLib.Net.SPI.Client;
  3. using LJLib.Net.SPI.Com;
  4. using LJProxy.Models;
  5. using LJProxy.Settings;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.Extensions.Options;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. using Newtonsoft.Json;
  13. using System.IO;
  14. using System.Text;
  15. using Newtonsoft.Json.Linq;
  16. using LJProxy.Services;
  17. namespace LJProxy.Controllers
  18. {
  19. [Route("api/[controller]")]
  20. [ApiController]
  21. public class L1SvrController : Controller
  22. {
  23. private static object _syncRoot = new object();
  24. public static AppSettings _appSettingModel;
  25. private LJClientPoolService _ljClient;
  26. public L1SvrController(LJClientPoolService ljClient)
  27. {
  28. _ljClient = ljClient;
  29. }
  30. [Route("svr/{apiName}")]
  31. [HttpPost]
  32. [HttpGet]
  33. public async Task<IActionResult> Svr(string apiName)
  34. {
  35. string requestBody;
  36. using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
  37. {
  38. requestBody = await reader.ReadToEndAsync();
  39. }
  40. //var files = Request.Form.Files;
  41. //if (files != null && files.Count > 0)
  42. //{
  43. // UploadFilesRequest requestObj = new UploadFilesRequest();
  44. // foreach (var file in files)
  45. // {
  46. // var path = $"{AppDomain.CurrentDomain.BaseDirectory}test\\{file.FileName}";
  47. // byte[] buffer = new byte[2048];
  48. // using (var fs = file.OpenReadStream())
  49. // {
  50. // using (var fsn = System.IO.File.Create(path))
  51. // {
  52. // while (true)
  53. // {
  54. // var size = fs.Read(buffer, 0, buffer.Length);
  55. // if (size == 0)
  56. // {
  57. // break;
  58. // }
  59. // fsn.Write(buffer, 0, size);
  60. // }
  61. // fsn.Flush();
  62. // }
  63. // }
  64. // }
  65. //}
  66. var uriBuilder = new UriBuilder
  67. {
  68. Scheme = Request.Scheme,
  69. Host = Request.Host.Host,
  70. Port = Request.Host.Port.GetValueOrDefault(80),
  71. };
  72. var gateway= uriBuilder.Uri;
  73. if (!string.IsNullOrEmpty(requestBody))
  74. {
  75. var reqObj = JObject.Parse(requestBody);
  76. reqObj["gateway"] = gateway;
  77. requestBody = JsonConvert.SerializeObject(reqObj);
  78. }
  79. var rslt = _ljClient.Pool.DoExcute(apiName, requestBody);
  80. return Content(rslt, "application/json");
  81. }
  82. [HttpPost]
  83. [Route("uploadfiles")]
  84. public IActionResult UploadFiles()
  85. {
  86. string token = string.Empty;
  87. if (Request.Headers.ContainsKey("Authorization"))
  88. {
  89. token = Request.Headers["Authorization"];
  90. }
  91. var files = Request.Form.Files;
  92. if (files != null && files.Count > 0)
  93. {
  94. UploadFilesRequest requestObj = new UploadFilesRequest();
  95. List<FileInfoModel> fileList = new List<FileInfoModel>();
  96. requestObj.token = token;
  97. foreach (var file in files)
  98. {
  99. FileInfoModel fileInfo = new FileInfoModel();
  100. fileInfo.FileName = file.FileName;
  101. using (var fs = file.OpenReadStream())
  102. {
  103. byte[] buffer = new byte[fs.Length];
  104. fs.Read(buffer, 0, buffer.Length);
  105. fileInfo.FileData = Convert.ToBase64String(buffer);
  106. }
  107. fileList.Add(fileInfo);
  108. }
  109. requestObj.FileList = fileList;
  110. var requestBody = JsonConvert.SerializeObject(requestObj);
  111. var rslt = _ljClient.Pool.DoExcute(requestObj.GetApiName(), requestBody);
  112. return Content(rslt, "application/json");
  113. }
  114. else
  115. {
  116. return NotFound();
  117. }
  118. }
  119. }
  120. }